批量 kill mysql 线程

时常有一些烂sql跑在数据库里,我们要进行kill,避免影响拖垮数据库。

 

mysql> show processlist;

+----+------+---------------------+--------------------+---------+------+----------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+---------------------+--------------------+---------+------+----------+------------------+
| 6 | root | 192.168.40.71:44018 | information_schema | Query | 0 | starting | show processlist |
+----+------+---------------------+--------------------+---------+------+----------+------------------+
1 row in set (0.00 sec)

mysql> select * from processlist;
+----+------+---------------------+--------------------+---------+------+-----------+---------------------------+
| ID | USER | HOST | DB | COMMAND | TIME | STATE | INFO |
+----+------+---------------------+--------------------+---------+------+-----------+---------------------------+
| 6 | root | 192.168.40.71:44018 | information_schema | Query | 0 | executing | select * from processlist |
+----+------+---------------------+--------------------+---------+------+-----------+---------------------------+
1 row in set (0.00 sec)

这两个命令本质是一样的。

所以只要找到你符合你条件的线程id就可以kill了。

 

mysql -uroot -S /tmp/mysql_20158.sock -sNe "select id from information_schema.processlist where COMMAND='Sleep' and TIME>60" | xargs -n 1 mysqladmin -uroot -S /tmp/mysql_20158.sock kill

或者生成kill 命令,再自己手动执行:
select concat('KILL ',id,';') from information_schema.processlist where COMMAND='Sleep' and TIME>60;


或者用工具mt-kill pt-kill

pt-kill --host=192.168.1.2 --user=root --password=000000 --port=3306 --busy-time 60 --match-command="query|Execute" --victim all --interval 1 --kill --daemonize --pid=/tmp/ptkill.pid --print --log=/home/pt-kill.log

pt-kill --host=192.168.1.2 --user=root --password=000000 --port=3306 --busy-time 60 --match-state="Locked|Sending data" --victim all --interval 1 --kill --daemonize --pid=/tmp/ptkill.pid --print --log=/home/pt-kill.log

pt-kill --host=192.168.1.2 --user=root --password=000000 --port=3306 --busy-time 60 --match-info="SELECT|DELETE" --victim all --interval 1 --kill --daemonize --pid=/tmp/ptkill.pid --print --log=/home/pt-kill.log

posted @ 2018-03-19 12:17  泽锦  阅读(2952)  评论(0编辑  收藏  举报