mysql kill 清理线程_批量 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值