137

The MySQL database hangs, due to some queries.

How can I find the processes and kill them?

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Ashish Dadhich
  • 4,737
  • 4
  • 17
  • 25

4 Answers4

249

Here is the solution:

  1. Login to DB;
  2. Run a command show full processlist;to get the process id with status and query itself which causes the database hanging;
  3. Select the process id and run a command KILL <pid>; to kill that process.

Sometimes it is not enough to kill each process manually. So, for that we've to go with some trick:

  1. Login to MySQL;
  2. Run a query Select concat('KILL ',id,';') from information_schema.processlist where user='user'; to print all processes with KILL command;
  3. Copy the query result, paste and remove a pipe | sign, copy and paste all again into the query console. HIT ENTER. BooM it's done.
Kate Orlova
  • 3,225
  • 5
  • 11
  • 35
Ashish Dadhich
  • 4,737
  • 4
  • 17
  • 25
  • 10
    That query is great! For anyone looking at this with light MySQL experience you can also filer the query by `Host`, `db`, `Command`, `Time`, `State`, or `Info`: `SELECT concat('KILL ',id,';') from information_schema.processlist where Command='Sleep';` or `SELECT concat('KILL ',id,';') from information_schema.processlist where Time>'300';` – KareemElashmawy Feb 08 '18 at 21:01
  • 8
    Thanks for the query! I used an enhanced version that could save some manual editing time: `select group_concat(concat('KILL ',id,';') separator ' ')` so they all land on one line that can be copy-pasted – architectonic Aug 07 '18 at 10:43
  • 1
    The query is ok, but you can end up killing your own process. I would review the list of process ids before rerunning the "kill all". – Patrick.SE Feb 12 '19 at 15:20
  • 3
    Tweaking @architectonic 's comment to get me something immediately easier to handle off the clipboard (even with the pesky opening and closing double quotes) ```SELECT group_concat(concat('KILL ',id,';') SEPARATOR ' \n') AS KILL_EVERYTHING FROM information_schema.processlist;``` – leerssej Jul 10 '19 at 19:08
44
select GROUP_CONCAT(stat SEPARATOR ' ') from (select concat('KILL ',id,';') as stat from information_schema.processlist) as stats;

Then copy and paste the result back into the terminal. Something like:

KILL 2871; KILL 2879; KILL 2874; KILL 2872; KILL 2866;
whistling_marmot
  • 3,561
  • 3
  • 25
  • 39
20

You can do something like this to check if any mysql process is running or not:

ps aux | grep mysqld
ps aux | grep mysql

Then if it is running you can killall by using(depending on what all processes are running currently):

killall -9 mysql
killall -9 mysqld
killall -9 mysqld_safe    
Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
  • 8
    The OP is asking about processes running within the MySQL server. – reinierpost Jul 08 '19 at 11:16
  • 9
    Although not what was asked for, it helped as this is the first item google returned when searching how to kill all mysqld processes on linux ;) – IncredibleHat Aug 30 '19 at 18:44
  • On a related note, one should always try with `kill -15` and resort to `kill -9` only if process continues to run. with `kill -15` you are giving process a chance for graceful termination. – sateesh Aug 04 '23 at 09:15
2

On RDS:

SELECT
  concat('CALL mysql.rds_kill(',id,');')
  FROM information_schema.processlist
  ORDER BY time;
Moshe
  • 4,635
  • 6
  • 32
  • 57