Development and databases

Kill running MySQL queries

Reviewed on 6 min read MySQL queries

Sometimes an SQL query takes much longer than expected, because of an application bug or because of how heavy it is. If it also locks the table, the other queries are left waiting and the website hangs. In that case you can stop the query manually.

There are two ways to stop it:

  • KILL QUERY id stops only the query and leaves the connection open. This is the recommended option.
  • KILL id (and mysqladmin kill id) closes the whole connection, which rolls back any transaction it had in progress.

From the terminal

Connect to the account over SSH. If you're experienced, use your computer's terminal; if not, the terminal built into cPanel is simpler.

With SQL queries

Log in to MySQL with a user that has permissions on the database:

mysql -u usuario_mysql -p

It will ask you for the password. Don't type it in the command itself (-pcontraseña), because it would stay in the terminal history.

List the open processes:

SHOW FULL PROCESSLIST;

You'll see a table with each connection, its user, database, state, the seconds it has been running (Time) and the query (Info):

Output of SHOW FULL PROCESSLIST with the running queries

Rows with Command set to Sleep are idle connections with no query running: you usually don't need to touch them. The one you're looking for usually has a high Time and a state such as Sending data, Locked or Waiting for table metadata lock.

Take the value from the Id column of the query you want to stop and run:

KILL QUERY 12345;

Run SHOW FULL PROCESSLIST; again and check that it no longer appears. If it's still there with the state Killed, it's rolling back changes: wait a few seconds. If it still doesn't close, close the whole connection with KILL 12345;.

Stop all of a user's queries

If there are many queries piled up from the same user, this query generates the commands to stop them all. Replace USUARIO_MYSQL with the user running them:

SELECT GROUP_CONCAT(CONCAT('KILL QUERY ', ID, ';') SEPARATOR ' ')
FROM information_schema.PROCESSLIST
WHERE USER = 'USUARIO_MYSQL'
  AND COMMAND <> 'Sleep'
  AND ID <> CONNECTION_ID();

Copy the result (something like KILL QUERY 101; KILL QUERY 102; ...), paste it and run it. The condition ID <> CONNECTION_ID() stops you from killing your own session, and COMMAND <> 'Sleep' leaves out idle connections.

To stop only the queries that contain a piece of text, for example those acting on a particular table, add one more filter:

  AND INFO LIKE '%wp_options%'

On the hosting you'll only see, and be able to stop, the queries of your own MySQL users.

With mysqladmin

From the terminal you can also use mysqladmin without logging in to MySQL. List the processes:

mysqladmin -u usuario_mysql -p processlist

And close the one you want using its identifier:

mysqladmin -u usuario_mysql -p kill 12345

You can give several, separated by commas: kill 101,102,103. Bear in mind that mysqladmin kill closes the whole connection, not just the query.

To close all the queries containing a piece of text (the name of a table, of a database...) in one go, combine the list with awk:

mysqladmin -u usuario_mysql -p processlist | awk -F'|' '/TEXTO_A_FILTRAR/ && $6 !~ /Sleep/ {gsub(/ /,"",$2); print $2}' | paste -sd, - | xargs -r mysqladmin -u usuario_mysql -p kill

The command asks for the password twice: once to list and once to close. Before running it, run only the first part (up to paste) to see which identifiers it's going to close.

From phpMyAdmin

Open phpMyAdmin, select the database and open the SQL tab. Type:

SHOW FULL PROCESSLIST;

phpMyAdmin shows the process table with a Kill link next to each one to close it with a click:

Process list in phpMyAdmin with the option to stop each query

You also have the same list under Status > Processes. The phpMyAdmin link closes the whole connection; if you only want to stop the query, type KILL QUERY id; in the SQL tab.

If it happens again

Stopping the query clears the blockage, but not its cause. If the same query often hangs, the table is usually missing an index or there's a plugin or scheduled task running it too many times. Look at the text in the Info column to find out what generates it.

More details in the MySQL documentation on KILL.

You may also find useful

Something wrong or out of date? Tell us and we will review it.