博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
批量杀死MySQL连接的几种方法
阅读量:6973 次
发布时间:2019-06-27

本文共 1492 字,大约阅读时间需要 4 分钟。

法一:
  通过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/liang545621/p/9401113.html

你可能感兴趣的文章
hdu 3786 寻找直系亲属
查看>>
模仿淘宝吸顶条(定时器)
查看>>
Project Euler 29 Distinct powers( 大整数质因数分解做法 + 普通做法 )
查看>>
Eclipse优化集合,Eclipse优化速度,解决Ctrl+C、Ctrl+V卡
查看>>
JavaScript中的this基本问题<转>
查看>>
杭电2040亲和数
查看>>
深入剖析Tomcat(How Tomcat Works)
查看>>
通过月份得到本月有几天周末
查看>>
在PHP语言中使用JSON和将json还原成数组
查看>>
博客园非官方月刊-2014年12月刊
查看>>
电商仓储控制超卖的策略
查看>>
windows系统安装MongoDB
查看>>
[转]Peer-to-Peer Communication Across Network Address Translators
查看>>
C++临时变量的生命周期
查看>>
Remove Element
查看>>
高淇Struts2.0教程之视频笔记(7)
查看>>
自适应SimpsonSimpson积分
查看>>
初学WebGL引擎-BabylonJS:第2篇-基础模型体验
查看>>
Python的垃圾回收机制以及引用计数
查看>>
C语言经典实例1: 类型长度与类型转换
查看>>