pt-kill 是一个优秀的kill MySQL连接的一个工具,是percona toolkit的一部分,在因为空闲连接较多导致超过最大连接数、某个有问题的sql导致mysql负载很高时,都需要将一些连接kill掉,这个工具主要就是这个用途。
参数
–busy-time
运行时间
–idle-time
空闲时间
–victims
所有匹配的连接,对应有最久的连接
–interval
间隔时间,默认30s,有点长,可以根据实际情况来调节
打印出来kill掉的连接
–match-command
匹配当前连接的命令
Query
Sleep
Binlog Dump
Connect
Delayed insert
Execute
Fetch
Init DB
Kill
Prepare
Processlist
Quit
Reset stmt
Table Dump
–match-state
匹配当前连接的状态
Locked
login
copy to tmp table
Copying to tmp table
Copying to tmp table on disk
Creating tmp table
executing
Reading from net
Sending data
Sorting for order
Sorting result
Table lock
Updating
–match-info
使用正则表达式匹配符合的sql
–match-db –match-user –match-host
见名知意
常用用法
杀掉空闲链接
pt-kill –match-command Sleep –idle-time 5 –host –port –interval –print –kill –victims all
杀掉运行时间超过5s的链接
pt-kill –match-command Query –busy-time 5 –host –port –interval –print –kill –victims all
杀掉匹配某个规则的正在运行的sql
pt-kill –match-command Query –busy-time 5 –host –port –interval –print –kill –victims all –match-info
杀掉正在进行filesort的sql
pt-kill –match-command Query –match-state “Sorting result” busy-time 5 –host –port –interval –print –kill –victims all
杀掉正在Copying to tmp table的sql
pt-kill –match-command Query –match-state “Copying to tmp table” busy-time 5 –host –port –interval –print –kill –victims all