zoukankan      html  css  js  c++  java
  • 批量 kill mysql 线程

    时常有一些烂sql跑在数据库里,我们要进行kill,避免影响拖垮数据库。

     

    mysql> show processlist;

    +----+------+---------------------+--------------------+---------+------+----------+------------------+
    | Id | User | Host | db | Command | Time | State | Info |
    +----+------+---------------------+--------------------+---------+------+----------+------------------+
    | 6 | root | 192.168.40.71:44018 | information_schema | Query | 0 | starting | show processlist |
    +----+------+---------------------+--------------------+---------+------+----------+------------------+
    1 row in set (0.00 sec)

    mysql> select * from processlist;
    +----+------+---------------------+--------------------+---------+------+-----------+---------------------------+
    | ID | USER | HOST | DB | COMMAND | TIME | STATE | INFO |
    +----+------+---------------------+--------------------+---------+------+-----------+---------------------------+
    | 6 | root | 192.168.40.71:44018 | information_schema | Query | 0 | executing | select * from processlist |
    +----+------+---------------------+--------------------+---------+------+-----------+---------------------------+
    1 row in set (0.00 sec)

    这两个命令本质是一样的。

    所以只要找到你符合你条件的线程id就可以kill了。

     

    mysql -uroot -S /tmp/mysql_20158.sock -sNe "select id from information_schema.processlist where COMMAND='Sleep' and TIME>60" | xargs -n 1 mysqladmin -uroot -S /tmp/mysql_20158.sock kill

    或者生成kill 命令,再自己手动执行:
    select concat('KILL ',id,';') from information_schema.processlist where COMMAND='Sleep' and TIME>60;


    或者用工具mt-kill pt-kill

    pt-kill --host=192.168.1.2 --user=root --password=000000 --port=3306 --busy-time 60 --match-command="query|Execute" --victim all --interval 1 --kill --daemonize --pid=/tmp/ptkill.pid --print --log=/home/pt-kill.log

    pt-kill --host=192.168.1.2 --user=root --password=000000 --port=3306 --busy-time 60 --match-state="Locked|Sending data" --victim all --interval 1 --kill --daemonize --pid=/tmp/ptkill.pid --print --log=/home/pt-kill.log

    pt-kill --host=192.168.1.2 --user=root --password=000000 --port=3306 --busy-time 60 --match-info="SELECT|DELETE" --victim all --interval 1 --kill --daemonize --pid=/tmp/ptkill.pid --print --log=/home/pt-kill.log

  • 相关阅读:
    LeetCode 1032. Stream of Characters
    LeetCode 872. Leaf-Similar Trees
    LeetCode 715. Range Module
    LeetCode 353. Design Snake Game
    LeetCode 509. Fibonacci Number
    LeetCode 632. Smallest Range Covering Elements from K Lists
    LeetCode 963. Minimum Area Rectangle II
    LeetCode 939. Minimum Area Rectangle
    LeetCode 727. Minimum Window Subsequence
    LeetCode 844. Backspace String Compare
  • 原文地址:https://www.cnblogs.com/zejin2008/p/8601232.html
Copyright © 2011-2022 走看看