批量 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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM