How do I kill all the processes in Mysql “show processlist”?

后端 未结 23 1894
温柔的废话
温柔的废话 2020-12-04 04:49

Because I see a lot of processes there, and the \"time\" column shows big values for all of them.

相关标签:
23条回答
  • 2020-12-04 05:23

    Mass killing operation saves time. Do it in MySql itself:

    Run these commands

    mysql> select concat('KILL ',id,';') from information_schema.processlist
    where user='root' and time > 200 into outfile '/tmp/a.txt';
    
    mysql> source /tmp/a.txt;
    

    Reference

    ---------edit------------
    

    if you do not want to store in file, store in a variable

    Just run in your command prompt

    > out1=$(mysql -B test -uroot -proot --disable-column-names  -e "select concat('KILL ',id,';') from information_schema.processlist where user='root' and time > 200;")
    
    > out2= $(mysql -B test -uroot -proot --disable-column-names  -e "$out1")
    
    0 讨论(0)
  • 2020-12-04 05:23

    Here is a solution that you can execute without relying on the operating system:

    STEP 1: Create a stored procedure.

    DROP PROCEDURE IF EXISTS  kill_user_processes$$ 
    
    CREATE PROCEDURE `kill_user_processes`(
      IN user_to_kill VARCHAR(255)
    )
    READS SQL DATA
    BEGIN
    
      DECLARE name_val VARCHAR(255);
      DECLARE no_more_rows BOOLEAN;
      DECLARE loop_cntr INT DEFAULT 0;
      DECLARE num_rows INT DEFAULT 0;
    
      DECLARE friends_cur CURSOR FOR
        SELECT CONCAT('KILL ',id,';') FROM information_schema.processlist WHERE USER=user_to_kill;
    
      DECLARE CONTINUE HANDLER FOR NOT FOUND
        SET no_more_rows = TRUE;
    
      OPEN friends_cur;
      SELECT FOUND_ROWS() INTO num_rows;
    
      the_loop: LOOP
    
        FETCH  friends_cur
        INTO   name_val;
    
        IF no_more_rows THEN
            CLOSE friends_cur;
            LEAVE the_loop;
        END IF;
    
    
     SET @s = name_val;
        PREPARE stmt FROM @s;
        EXECUTE stmt;
        DEALLOCATE PREPARE stmt;
    
        SELECT name_val;
    
        SET loop_cntr = loop_cntr + 1;
    
      END LOOP the_loop;
    
      SELECT num_rows, loop_cntr;
    
    END $$
    DELIMITER ;
    

    STEP 2: Call the stored procedure giving it the name of a database user whose processes you want to kill. You could rewrite the stored procedure to filter on some other criteria if you like.

    CALL kill_user_processes('devdba');

    0 讨论(0)
  • 2020-12-04 05:24

    You need to kill them one by one, MySQL does not have any massive kill command. You can script it in any language, for example in PHP you can use something like:

    $result = mysql_query("SHOW FULL PROCESSLIST");
    while ($row=mysql_fetch_array($result)) {
      $process_id=$row["Id"];
      if ($row["Time"] > 200 ) {
        $sql="KILL $process_id";
        mysql_query($sql);
      }
    }
    
    0 讨论(0)
  • 2020-12-04 05:25

    This snipped worked for me (MySQL server 5.5) to kill all MySQL processes :

    mysql -e "show full processlist;" -ss | awk '{print "KILL "$1";"}'| mysql
    
    0 讨论(0)
  • 2020-12-04 05:25

    The following worked great for me:

    echo "show processlist" | mysql | grep -v ^Id | awk '{print $1}' | xargs -i echo "KILL {}; | mysql"
    
    0 讨论(0)
  • 2020-12-04 05:26

    I have also searched how to parse through MySQL the command SHOW PROCESSLIST and ended with a one-liner in a Shell:

    mysqladmin processlist -u <USERNAME> -p<PASSWORD> | \
    awk '$2 ~ /^[0-9]/ {print "KILL "$2";"}' | \
    mysql -u <USERNAME> -p<PASSWORD>
    
    • mysqladmin processlist will print a table with the thread ids;
    • awk will parse from the second column only the numbers (thread ids) and generate MySQL KILL commands;
    • and finally the last call to mysql will execute the passed commands.

    You can run grep before the awk command to filter a particular database name.

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