批量杀死MySQL连接的几种方法

一:

  通过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.orczhou.com/index.php/2010/10/kill-mysql-connectio-in-batch/
 http://www.mysqlperformanceblog.com/2009/05/21/mass-killing-of-mysql-connections/
 
 
以上装载来源: https://www.cnblogs.com/niewd/p/13328031.html
posted @   MR__Wang  阅读(97)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
历史上的今天:
2018-08-29 远程升级云服务器系统 CentOS 6.x 至 CentOS 7.x
2018-08-29 解决 yum安装时报错 Error: Protected multilib versions: 报错
点击右上角即可分享
微信分享提示