Kill the process of all tables in MySQL

 A colleague called me to tell me that the user database was down. I got up and looked at the process list.

mysql>show processlist;

There are several screens, there are not a thousand or hundreds, the query statement locks the table, quickly find the thread_id of the first Locked, and execute it in the mysql shell.

mysql>kill thread_id;

Killing the first process that locks the table still does not improve. Since it does not improve, let's find a way to kill all the processes that lock the table. The simple script is as follows.

#!/bin/bash
mysql -u root -e "show processlist" | grep -i "Locked" >> locked_log.txt

for line in `cat locked_log.txt | awk '{print $1}'`
do 
   echo "kill $line;" >> kill_thread_id.sql
done

Now the content of kill_thread_id.sql looks like this

kill 66402982;
kill 66402983;
kill 66402986;
kill 66402991;
.....

Well, we can kill all the processes that lock the table by executing it in the mysql shell.

mysql>source kill_thread_id.sql

Of course, it can also be done in one line

for id in `mysqladmin processlist | grep -i locked | awk '{print $1}'`
do
   mysqladmin kill ${id}
done

for id in `mysqladmin -unextxxx -p'Nextexxx' -h xxx.x8.xx4.xx  processlist |awk '{print $2}' |grep -v '[a-Z]'`
do
     mysqladmin -unextxxx -p'Nextexxx' -h xxx.98.xx4.xx kill ${id}
done

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325847606&siteId=291194637