• Skip to main content
  • Skip to primary sidebar

DBA Diaries

Thoughts and experiences of a DBA working with SQL Server and MySQL

How to Kill All MySQL Processes For a Specific User

June 17, 2014 by Andy Hayes Leave a Comment

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.

  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';

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

Related Posts:

  • How to fix "conversion failed when converting date and/or time from character string"
    How to fix "conversion failed when converting date…
  • sql grouping sets
    Using SQL GROUPING SETS for Multiple GROUP BY…

Filed Under: All Articles, MySQL Administration Tagged With: mysql

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 Google+

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Categories

  • All Articles (84)
  • Career Development (8)
  • MySQL Administration (18)
  • MySQL Performance (2)
  • SQL Server Administration (24)
  • SQL Server News (3)
  • SQL Server Performance (14)
  • SQL Server Security (3)
  • SQL Tips and Tricks (21)

Top 10 Popular Posts

  • Using sp_change_users_login to fix SQL Server orphaned users
  • MySQL SHOW USERS? – How to List All MySQL Users and Privileges
  • How to shrink tempdb
  • How to Transfer Logins to Another SQL Server or Instance
  • How to Delete Millions of Rows using T-SQL with Reduced Impact
  • T-SQL – How to Select Top N Rows for Each Group Using ROW_NUMBER()
  • New T-SQL features in SQL Server 2012 – OFFSET and FETCH
  • How to Kill All MySQL Processes For a Specific User
  • Using exec sp_who2 to help with SQL Server troubleshooting
  • How to fix “conversion failed when converting date and/or time from character string”

Recent Posts

  • How to fix “conversion failed when converting date and/or time from character string”
  • Using SQL GROUPING SETS for Multiple GROUP BY Queries in a Single Query
  • How to Setup MySQL Master Master Replication
  • How To Use SQL to Convert a STRING to an INT
  • How to set up MySQL Replication Tutorial

Search

Connect

  • Twitter
  • Facebook
  • RSS

About

  • Cookie Policy
  • Disclaimer
  • About
Copyright ©