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

后端 未结 23 1893
温柔的废话
温柔的废话 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:13

    Only for mariaDB

    It doesn't get simpler then this, Just execute this in mysql prompt.

    kill USER username;
    

    It will kill all process under provided username. because most of the people use same user for all purpose, it works!

    I have tested this on MariaDB not sure about mysql.

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

    The following will create a simple stored procedure that uses a cursor to kill all processes one by one except for the process currently being used:

    DROP PROCEDURE IF EXISTS kill_other_processes;
    
    DELIMITER $$
    
    CREATE PROCEDURE kill_other_processes()
    BEGIN
      DECLARE finished INT DEFAULT 0;
      DECLARE proc_id INT;
      DECLARE proc_id_cursor CURSOR FOR SELECT id FROM information_schema.processlist;
      DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = 1;
    
      OPEN proc_id_cursor;
      proc_id_cursor_loop: LOOP
        FETCH proc_id_cursor INTO proc_id;
    
        IF finished = 1 THEN 
          LEAVE proc_id_cursor_loop;
        END IF;
    
        IF proc_id <> CONNECTION_ID() THEN
          KILL proc_id;
        END IF;
    
      END LOOP proc_id_cursor_loop;
      CLOSE proc_id_cursor;
    END$$
    
    DELIMITER ;
    

    It can be run with SELECTs to show the processes before and after as follows:

    SELECT * FROM information_schema.processlist;
    CALL kill_other_processes();
    SELECT * FROM information_schema.processlist;
    
    0 讨论(0)
  • 2020-12-04 05:14

    If you don't have information_schema:

    mysql -e "show full processlist" | cut -f1 | sed -e 's/^/kill /' | sed -e 's/$/;/' ;  > /tmp/kill.txt
    mysql> . /tmp/kill.txt
    
    0 讨论(0)
  • 2020-12-04 05:14

    If you are using laravel then this is for you:

    $query = "SHOW FULL PROCESSLIST";
        $results = DB::select(DB::raw($query));
    
        foreach($results as $result){
            if($result->Command == "Sleep"){
                $sql="KILL ". $result->Id;
                DB::select(DB::raw($sql));
            }
        }
    

    Of-course, you should use this use Illuminate\Support\Facades\DB; after your namespace.

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

    I recently needed to do this and I came up with this

    -- GROUP_CONCAT turns all the rows into 1
    -- @q:= stores all the kill commands to a variable
    select @q:=GROUP_CONCAT(CONCAT('KILL ',ID) SEPARATOR ';')  
    FROM information_schema.processlist 
    -- If you don't need it, you can remove the WHERE command altogether
    WHERE user = 'user';
    -- Creates statement and execute it
    PREPARE stmt FROM @q;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
    

    That way, you don't need to store to file and run all queries with a single command.

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

    Sometimes I have some zombies mysql processes that can't be killed (using MAMP Pro).

    First get a list of all mysql processes:

    ps -ax | grep mysql
    

    Next kill every one of them with (replace processId with the first column in previous command result):

    kill -9 processId
    
    0 讨论(0)
提交回复
热议问题