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

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

    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');

提交回复
热议问题