批量殺死MySQL連接的幾種方法

轉自:http://www.mike.org.cn/articles/batch-kill-mysql-connection/

方法一

通過information_schema.processlist表中的連接信息生成需要處理掉的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)

方法二

殺掉當前所有的MySQL連接

mysqladmin -uroot -p processlist|awk -F "|" '{print $2}'|xargs -n 1 mysqladmin -uroot -p kill

殺掉指定用戶運行的連接,這裏爲Mike

mysqladmin -uroot -p processlist|awk -F "|" '{if($3 == "Mike")print $2}'|xargs -n 1 mysqladmin -uroot -p kill

方法三

通過SHEL腳本實現

#殺掉鎖定的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 –kill

mk-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/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章