A site is up but unresponsive — every request takes 30 seconds, then times out. CPU is idle. Disk IO is idle. MySQL SHOW PROCESSLIST shows 80 connections, 70 of them in Waiting for table metadata lock. Somewhere in the list is the one query holding the lock. Killing it releases the queue and the site recovers in seconds. Killing the wrong one breaks the in-progress transaction and corrupts the application’s state. PROCESSLIST is the diagnostic, KILL is the tool, and picking the right target matters.
What’s happening
MySQL’s KILL command terminates a connection or a query, depending on which form you use:
KILL <connection_id>: closes the connection and rolls back any in-progress transaction.KILL QUERY <connection_id>: terminates the current statement but keeps the connection open. The client gets an error; the connection can be reused.
Most “kill the runaway query” cases want KILL QUERY — the connection survives, the application catches the error and retries. KILL (without QUERY) is for cases where the connection itself is the problem (a stuck DDL, a client that won’t go away).
SHOW PROCESSLIST lists every active connection. The columns that matter:
Id: connection ID, the target for KILL.User: the account that owns the connection (matchesacmecorp_*prefix).db: which database the connection is using.Command: what the connection is doing (Query,Sleep,Connect,Killed).Time: seconds since the current command started.State: more detail (Sending data,Waiting for table metadata lock,Locked).Info: the SQL being executed (truncated to 100 chars unless you useSHOW FULL PROCESSLIST).
How to verify
mysql -e "SHOW FULL PROCESSLIST" | head -30
mysql -e "SELECT * FROM information_schema.PROCESSLIST WHERE TIME > 30 AND COMMAND != 'Sleep' ORDER BY TIME DESC"
mysql -e "SELECT * FROM information_schema.INNODB_TRX ORDER BY trx_started"
mysql -e "SHOW ENGINE INNODB STATUS\G" | head -100
The first is the quick read. The second filters to non-sleep connections over 30 seconds — the candidates for KILL. The third lists open InnoDB transactions, including ones blocking others. SHOW ENGINE INNODB STATUS has a “LATEST DETECTED DEADLOCK” and “LIST OF TRANSACTIONS” section that maps locks to transactions.
The fix
- Identify the blocker, not just any long-running query. A query waiting for a lock is the victim; the holder is one earlier in the list. From
INNODB_TRX, sort bytrx_startedascending — the oldest transaction is usually the one holding the lock everyone else is waiting on. - Confirm the user and the SQL:
You should see a real query inSELECT id, user, db, command, time, state, info FROM information_schema.PROCESSLIST WHERE id = 12345info— if it’s empty or showsSleep, the connection is idle but holding open transactions. That’s a stuck application, not a runaway query. - For a runaway query, use KILL QUERY:
The query is terminated; the connection survives; the application receives an error and (if it’s well-behaved) retries.mysql -e "KILL QUERY 12345" - For a stuck connection holding open transactions, use plain KILL:
This rolls back any open transaction and closes the connection. The blocked queue should drain immediately.mysql -e "KILL 12345" - For automated detection, run a periodic check:
Kills any query running over 5 minutes. Excludes the event scheduler and replication. Be cautious with thresholds — a legitimate analytics export might run 10 minutes.mysql -e "SELECT id FROM information_schema.PROCESSLIST WHERE TIME > 300 AND COMMAND = 'Query' AND USER NOT IN ('event_scheduler','system user')" | xargs -r -n1 -I{} mysql -e "KILL QUERY {}" - For per-user thresholds, use
MAX_STATEMENT_TIME(MariaDB) orMAX_EXECUTION_TIME(MySQL 5.7+) so the server kills its own runaway queries:
Anything over 60 seconds gets ERR. Set per-user withSET GLOBAL max_statement_time = 60;ALTER USER ... MAX_STATEMENT_TIME 30. - Document patterns. If the same kind of query keeps showing up in the kill list, that’s an application issue — fix the query or add an index, don’t keep killing it.
For environments where long-query detection needs to be proactive rather than reactive, Stack Harbor’s environment management team adds Prometheus exporters for MySQL and alerts on mysql_global_status_threads_running and mysql_global_status_threads_waiting so the runaway is visible before customers notice.