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 MySQL processes and 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 all MySQL connections 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).
Run this:
SELECT * FROM processlist\G;
*************************** 1. row *************************** ID: 1 USER: root HOST: localhost:60049 DB: information_schema COMMAND: Query 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.
- 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.
- 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';
Edit the file and remove the top line that has been created by the output as it will be an invalid SQL statement. In my example it’s “run_this” as the column header.
You can now run this file into MySQL which will execute all the statements within to kill all MySQL processes as identified in your export.
SOURCE /tmp/kill_process.txt
Or you can import it to kill multiple MySQL queries at once.
mysql -uroot -p{yourpassword} < /tmp/kill_process.txt
Leave a Reply