How to Kill All MySQL Processes For a Specific User

So here is a scenario, you have a number of poorly performing MySQL database queries which are consuming resources on your server. Users are complaining and you need to do something fast. Having viewed the output of SHOW FULL PROCESSLIST, you can see that they are coming from one user and have been running for a long time.

You need a way to kill all of these MySQL processes quickly! Running kill (process identifier) for each connection manually will not cut the mustard. It will take too long and you need to fix this problem now!

So having identified the user where these connections are coming from, thankfully with the help of a little bit of SQL and the information_schema.processes table, you can kill off the MySQL processes in one fast sweep and alleviate the load on your database server. Here’s how:

Use the information_schema.processlist table to identify the problem threads

An incredibly useful table, it will show you all the processes against your MySQL instance enabling you to identify queries at a user and database level and importantly what query they are executing (INFO column) and for how long (TIME column).

SELECT * FROM processlist\G;

*************************** 1. row ***************************
     ID: 1
   USER: root
   HOST: localhost:60049
     DB: information_schema
   TIME: 0
  STATE: executing
   INFO: select * from processlist

This output is for demonstration purposes but lets imagine I had many of these queries in the list.

Use CONCAT to kill many MySQL processes

SELECT CONCAT('KILL ',id,';') AS run_this FROM information_schema.processlist WHERE user='root' AND info = 'SELECT * FROM processlist';

The resulting output would like this

| run_this               |
| KILL 1;                |
| KILL 2;                |
| KILL 3;                |
| KILL 4;                |

You now have a list of the threads you want to kill off. What is the best way to now execute this? A couple of options spring to mind. I’m sure there are other ways to do this too.

  1. You could copy and paste this list into a text editor, remove the pipe and plus symbols from the output and then execute the list against MySQL.
  2. You could also add an extra step to the CONCAT query which copies the statements to a file and then run that file back through MySQL.

Lets look at option 2. You will write a file to a location which MySQL has permission to read and write from.

SELECT CONCAT('KILL ',id,';') AS run_this FROM information_schema.processlist WHERE user='root' AND info = 'SELECT * FROM processlist' INTO OUTFILE '/tmp/kill_process.txt';

You can now run this file back through MySQL to execute all KILL commands in one batch.

SOURCE /tmp/kill_process.txt

It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInPin on PinterestBuffer this pageShare on RedditShare on StumbleUpon
About Andy Hayes

Andy Hayes is a DBA working with SQL Server since version 7.0. He has a wonderful wife and two beautiful children. He loves database technology, playing cricket, and blogging. He is passionate about sharing his experiences as a DBA and learning more to further his understanding and knowledge. You can follow me on Twitter, check out my Facebook page or follow me on

Speak Your Mind