这篇文章主要介绍了批量杀死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杀掉指定用户运行的连接,这里为Mikemysqladmin -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秒的sqlmk-kill -busy-time 60 -kill#如果你想先不杀,先看看有哪些sql运行超过60秒mk-kill -busy-time 60 -print#如果你想杀掉,同时输出杀掉了哪些进程mk-kill -busy-time 60 -print –kill
MySQL杀会话常用方法
最新推荐文章于 2024-07-03 09:15:00 发布
本文详细介绍了四种杀死MySQL连接的方法:通过information_schema.processlist生成并执行 kill 命令、使用mysqladmin结合awk和xargs、Shell脚本循环处理以及使用Maatkit工具的mk-kill命令。这些方法可用于管理和优化数据库连接,尤其是处理锁定连接或长运行查询。

1432

被折叠的 条评论
为什么被折叠?



