How-to

Kill a Runaway MySQL Query or Connection

One bad query can hold locks and stall a whole site. MySQL lets you end that query, or its connection, without touching the server process.

How do I see what is running?

SHOW FULL PROCESSLIST;
SELECT id, user, host, db, command, time, state, LEFT(info, 120) AS query
FROM information_schema.processlist
WHERE command <> 'Sleep' ORDER BY time DESC LIMIT 15;

The second form filters out idle connections and sorts by how long each statement has run, so the worst offender is on top. On MySQL 8, performance_schema.processlist offers the same data without the global locks the old processlist took; prefer it on busy servers. From the shell, mysqladmin -u root processlist or sudo mysqladmin processlist on Ubuntu gives a quick look.

Should I end the query or the whole connection?

StatementStopsConnection
KILL QUERY 51234The running statementStays open
KILL 51234 or KILL CONNECTION 51234Statement and sessionClosed

Start with KILL QUERY. The application keeps its connection, gets an error for that one statement and carries on. Close the whole connection when it holds an open transaction with locks other sessions are waiting on, or when the client itself is misbehaving.

Why does a killed query still show up?

Because the server must undo what it changed. A killed UPDATE or DELETE on InnoDB rolls back every row it touched, and the processlist shows the state "Killed" or "rolling back" until that finishes. A large statement can take as long to roll back as it ran. Do not restart the server to speed it up; crash recovery performs the same rollback at start-up, with the site down in the meantime. Check progress with SELECT trx_id, trx_state, trx_rows_modified FROM information_schema.innodb_trx;.

How do I find what is blocking everyone?

Look for lock waits. On MySQL 8, the sys schema has a ready view:

SELECT waiting_pid, waiting_query, blocking_pid, blocking_query, wait_age
FROM sys.innodb_lock_waits;

The blocking_pid is usually a session with an open transaction that forgot to commit, often an admin console left idle in the middle of a transaction. Killing that one connection releases dozens of waiting queries. On MariaDB, join information_schema.innodb_lock_waits with innodb_trx for the same information.

How do I kill many at once?

Generate the statements, check them, then run them:

SELECT CONCAT('KILL QUERY ', id, ';') FROM information_schema.processlist
WHERE user = 'report' AND command = 'Query' AND time > 300;

Paste the output back into the client after reading it. The Percona Toolkit's pt-kill automates this with rules, for example killing any SELECT from a reporting user that runs longer than five minutes.

How do I stop long queries automatically?

  • MySQL: SET GLOBAL max_execution_time = 30000; limits read-only SELECT statements to 30 seconds. Per query, add the hint /*+ MAX_EXECUTION_TIME(5000) */.
  • MariaDB: SET GLOBAL max_statement_time = 30; applies to all statements, in seconds.
  • Give reporting tools their own account and point them at a replica, so analytics never compete with checkout queries.

Persist global settings in the server configuration file so they survive a restart.

Should I ever kill the mysqld process itself?

Only as a last resort, and never with kill -9 as a first move. If the server is unresponsive, try systemctl stop mysql (mariadb or mysqld depending on the system) and give it time; InnoDB flushes and shuts down cleanly. A forced kill is survivable thanks to crash recovery, but recovery takes longer and replicas may need checking. General process handling is in Kill a process.

How do I find the queries worth fixing?

Turn on the slow query log with long_query_time = 1, then summarize it with pt-query-digest or mysqldumpslow. Most runaway queries come from a missing index; EXPLAIN on the query shows whether it scans whole tables. Giving MySQL enough memory helps too, as covered in MySQL servers.

Keep Reading

Kill a process

Stop processes the right way: find them, send TERM before KILL, use systemctl for services, and handle zombies and stuck D-state tasks.

Open

Remove a MySQL user

Drop MySQL or MariaDB accounts safely: find every host variant, check live sessions and stored-object definers, lock first, then drop.

Open

MySQL servers

Give InnoDB a buffer pool that holds your working set, NVMe for the redo log and no hypervisor between query and disk.

Open

MySQL users and privileges

Create MySQL 8 accounts with caching_sha2 passwords, least-privilege grants, roles, lockout rules and dynamic privileges for admin tasks.

Open

FAQ

Quick Answers

Find its id in SHOW FULL PROCESSLIST and run KILL QUERY with that id.

No. KILL QUERY stops only the statement; plain KILL closes the connection too.

InnoDB is rolling back its changes. Large statements can take a long time to roll back.

Use max_execution_time on MySQL for SELECT statements or max_statement_time on MariaDB.

No. KILL the query; a restart performs the same rollback during recovery with the site down.