• 批量杀死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)
    
    
    
    如果结果太多的话,需要保存为文件中
    注意,文件中 第一行会有 concat('KILL ',id,';') 语句,需要注释掉,才能 source 进去
    mysql -uroot -p -e "select concat('KILL ',id,';') from information_schema.processlist where user='root'" > /tmp/a.sql
     
     
    方法二:
    杀掉当前所有的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 -printkill
    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/
  • 相关阅读:
    P1351 联合权值
    c++ 贪心讲解大礼包
    取石子 找规律
    树 dfs暴力判环 题意转化
    P2519 [HAOI2011]problem a
    P1640 [SCOI2010]连续攻击游戏 二分图最大匹配 匈牙利算法
    P2756 飞行员配对方案问题 二分图匹配 匈牙利算法
    cogs 49. 跳马问题 DFS dp
    cogs 2. 旅行计划 dijkstra+打印路径小技巧
    cogs 1440. [NOIP2013]积木大赛 贪心水题
  • 原文地址:https://www.cnblogs.com/l10n/p/9401113.html
Copyright © 2020-2023  润新知