Skip to content

Detecting and killing long-running queries on a cPanel MySQL host without taking the site down

A 200-second query holding a table lock blocks every other write. KILL is the right tool; SHOW PROCESSLIST is how you decide which one to kill.

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 (matches acmecorp_* 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 use SHOW 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

  1. 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 by trx_started ascending — the oldest transaction is usually the one holding the lock everyone else is waiting on.
  2. Confirm the user and the SQL:
    SELECT id, user, db, command, time, state, info FROM information_schema.PROCESSLIST WHERE id = 12345
    You should see a real query in info — if it’s empty or shows Sleep, the connection is idle but holding open transactions. That’s a stuck application, not a runaway query.
  3. For a runaway query, use KILL QUERY:
    mysql -e "KILL QUERY 12345"
    The query is terminated; the connection survives; the application receives an error and (if it’s well-behaved) retries.
  4. For a stuck connection holding open transactions, use plain KILL:
    mysql -e "KILL 12345"
    This rolls back any open transaction and closes the connection. The blocked queue should drain immediately.
  5. For automated detection, run a periodic check:
    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 {}"
    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.
  6. For per-user thresholds, use MAX_STATEMENT_TIME (MariaDB) or MAX_EXECUTION_TIME (MySQL 5.7+) so the server kills its own runaway queries:
    SET GLOBAL max_statement_time = 60;
    Anything over 60 seconds gets ERR. Set per-user with ALTER USER ... MAX_STATEMENT_TIME 30.
  7. 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.