Shell Script to auto kill mysql sleep processes

后端 未结 5 697
别跟我提以往
别跟我提以往 2021-02-07 19:29

How We Kill mysql sleep processes Like:

+------+-----------+-----------+------------------------+---------+------+----------------+-----------------------------------         


        
相关标签:
5条回答
  • 2021-02-07 19:32

    A easy way:

    for i in `mysql -e "show processlist" | awk '/Sleep/ {print $1}'` ; do mysql -e "KILL $i;"; done
    
    0 讨论(0)
  • 2021-02-07 19:33

    I made it.

    Create kill_sleep.sh file

    mysql -u<user> -p<password> -h<host> -e "select concat('KILL ',id,';')  into outfile '/tmp/sleep_processes.txt' from information_schema.processlist where Command = 'Sleep'"
    mysql -u<user> -p<password> -h<host> -e "source /tmp/sleep_processes.txt;"
    rm -rf /tmp/sleep_processes.txt
    

    And set kill_sleep.sh to cron job .

    0 讨论(0)
  • 2021-02-07 19:42

    Percona Tools:

    pt-kill --match-command Sleep --idle-time 100 --victims all  --interval 30 --kill
    

    This will find all connections that are "Sleep" state and idle for 100 seconds or more and kill them. --interval 30 will make it keep do this every 30 seconds. So you can open a screen -S ptkill then in that screen run the above command, then ctrl-A, D to detach and exit the terminal and it will just keep running cleaning up your connections.

    https://www.percona.com/doc/percona-toolkit/2.1/pt-kill.html

    0 讨论(0)
  • 2021-02-07 19:47

    Vishal's answer works well if you're running the command on the MySQL server, but it won't work if you're connecting to the server remotely or if you don't have permission to run SOURCE or SELECT ... INTO OUTFILE (eg. Amazon's RDS). It's possible to rewrite it not to rely on those features though, and then it'll work anywhere:

    mysql -h<host> -u<user> -p -e "SELECT CONCAT('KILL ',id,';') FROM information_schema.processlist WHERE Command = 'Sleep'" > sleep.txt
    cat sleep.txt | xargs -I% mysql -h<host> -u<user> -p -e "%"
    
    0 讨论(0)
  • 2021-02-07 19:56

    The syntax is:

    KILL thread_id
    

    In your case:

      mysql > KILL 3057
    

    But in order to delete all the sleep processes,one command cant be used, you need to loop through whole processlist,after taking all the processes in tmp table and looping through it:

    select concat('KILL ',id,';') from information_schema.processlist where Command='Sleep';
    
    select concat('KILL ',id,';') from information_schema.processlist where Command='Sleep' into outfile '/tmp/a.txt';
    

    Referred from here

    0 讨论(0)
提交回复
热议问题