转自:http://www.mike.org.cn/articles/batch-kill-mysql-connection/ php
方法一 html
经过information_schema.processlist表中的链接信息生成须要处理掉的MySQL链接的语句临时文件,而后执行临时文件中生成的指令。mysql
mysql> select concat('KILL ',id,';') from information_schema.processlist where user='root'; +------------------------+ | concat('KILL ',id,';') | +------------------------+ | KILL 3101; | | KILL 2946; | +------------------------+ 2 rows in set (0.00 sec) mysql>select concat('KILL ',id,';') from information_schema.processlist where user='root' into outfile '/tmp/a.txt'; Query OK, 2 rows affected (0.00 sec) mysql>source /tmp/a.txt; Query OK, 0 rows affected (0.00 sec)方法二 sql
杀掉当前全部的MySQL链接 ide
mysqladmin -uroot -p processlist|awk -F "|" '{print $2}'|xargs -n 1 mysqladmin -uroot -p kill工具
杀掉指定用户运行的链接,这里为Mike google
mysqladmin -uroot -p processlist|awk -F "|" '{if($3 == "Mike")print $2}'|xargs -n 1 mysqladmin -uroot -p killcode
方法三 orm
经过SHEL脚本实现 htm
#杀掉锁定的MySQL链接 for id in `mysqladmin processlist|grep -i locked|awk '{print $1}'` do mysqladmin kill ${id} done方法四
经过Maatkit工具集中提供的mk-kill命令进行
#杀掉超过60秒的sql mk-kill -busy-time 60 -kill #若是你想先不杀,先看看有哪些sql运行超过60秒 mk-kill -busy-time 60 -print #若是你想杀掉,同时输出杀掉了哪些进程 mk-kill -busy-time 60 -print –killmk-kill更多用法可参考:
http://www.maatkit.org/doc/mk-kill.html
http://www.sbear.cn/archives/426
Maatkit工具集的其它用法可参考:
http://code.google.com/p/maatkit/wiki/TableOfContents?tm=6
参考文档:
http://www.google.com http://www.orczhou.com/index.php/2010/10/kill-mysql-connectio-in-batch/ http://www.mysqlperformanceblog.com/2009/05/21/mass-killing-of-mysql-connections/