May 21, 2009

Mass killing of MySQL Connections

Posted by peter |

Every so often I run into situation when I need to kill a lot of connections on MySQL server - for example hundreds of instances of some bad query is running making server unusable. Many people have special scripts which can take the user, source host or query as a parameter and perform the action. There is also a way to do it just using MySQL with a few commands:

SQL:
  1. mysql> SELECT concat('KILL ',id,';') FROM information_schema.processlist WHERE user='root';
  2. +------------------------+
  3. | concat('KILL ',id,';') |
  4. +------------------------+
  5. | KILL 3101;             |
  6. | KILL 2946;             |
  7. +------------------------+
  8. 2 rows IN SET (0.00 sec)
  9.  
  10. mysql> SELECT concat('KILL ',id,';') FROM information_schema.processlist WHERE user='root' INTO OUTFILE '/tmp/a.txt';
  11. Query OK, 2 rows affected (0.00 sec)
  12.  
  13. mysql> source /tmp/a.txt;
  14. Query OK, 0 rows affected (0.00 sec)

In general this is very powerful approach which I use in a lot of cases to create set of SQL statements by SQL query and when execute it.

It would be nice and clean if MySQL would have some way to "eval" - execute the result set of the query as SQL commands. This would avoid requirement to use temporary file etc.

Related posts: :Should you have your swap file enabled while running MySQL ?::Are PHP persistent connections evil ?::Debugging sleeping connections with MySQL:
 

21 Comments »

  1. You are not the only one. I’ve thought that this would be handy several times.

    Create a session variable and then make it so that it can be parsed by the parser?

    Not terribly hard.

    -Brian

    Comment :: May 21, 2009 @ 3:08 pm

  2. Brian,

    Yes that is one possibility. Though I think it is nicer if result set can be executed line by line – the GROUP_CONCAT trick works but it is typically restricted in size.

    Comment :: May 21, 2009 @ 4:07 pm

  3. 3. Sergey

    Could you please update your post to point out that this feature is 5.1+?

    Comment :: May 21, 2009 @ 5:27 pm

  4. 4. Robert Wultsch

    I prefer the following as it will kill them in a multi threaded manner… (sometimes killing a single query can take a while)

    for i in $(mysql -uroot -pPASS -e ’show processlist’ | grep ’search_term’ | awk ‘{print $1}’); do
    mysql -uroot -pPASS -e “kill $i” &
    done

    Comment :: May 21, 2009 @ 6:27 pm

  5. 5. Robert Wultsch

    Peter,
    Couldn’t you use prepared statements as an eval?

    Comment :: May 21, 2009 @ 7:09 pm

  6. could also be a modifier to the client instead of a special sql construct (much like \G)

    Comment :: May 21, 2009 @ 9:52 pm

  7. Sergey,

    PROCESSLIST table exists in MySQL 5.1 as well as Percona patches MySQL 5.0 I actually used later for this test.

    Comment :: May 21, 2009 @ 9:56 pm

  8. Hi Peter,

    With INFORMATION_SCHEMA.PROCESSLIST one can create a stored procedure using server cursor to achieve this. The following procedure accepts a user name and kills all queries for that user.
    server cursors are a MySQL solution for ‘eval’.

    Shlomi

    DELIMITER $$

    DROP PROCEDURE IF EXISTS `test`.`kill_user_queries`$$
    CREATE PROCEDURE `test`.`kill_user_queries` (kill_user_name VARCHAR(16) CHARSET utf8)
    SQL SECURITY INVOKER
    BEGIN
    DECLARE query_id INT;
    DECLARE iteration_complete INT DEFAULT 0;
    DECLARE select_cursor CURSOR FOR SELECT id FROM INFORMATION_SCHEMA.PROCESSLIST WHERE user=kill_user_name;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET iteration_complete=1;

    OPEN select_cursor;
    cursor_loop: LOOP
    FETCH select_cursor INTO query_id;
    IF iteration_complete THEN
    LEAVE cursor_loop;
    END IF;
    KILL QUERY query_id;
    END LOOP;
    CLOSE select_cursor;

    END$$

    DELIMITER ;

    Comment :: May 21, 2009 @ 9:56 pm

  9. Robert,

    I think you can use prepared statements and user variables to execute 1 statement. I’d like to execute many statements and it is best if they are returned as multiple rows in result set.

    Comment :: May 21, 2009 @ 9:58 pm

  10. Shlomi,

    Thanks. Indeed I’d like some good set of useful Stored Procedures to be available for MySQL so we could just add them to any MySQL installation same as we do with Maatkit – there are many things which are relatively easy with stored procedures but you do not want to write it every time from scratch :)

    Comment :: May 21, 2009 @ 10:06 pm

  11. Peter,

    I agree. I think there are some general-purpose stored procedures kits around. There are also many useful queries+views by Roland Bouman that I know of.
    Would be nice to gather them all into a “utilities” schema.

    PS a prepared statement will not allow “KILL” or “OPTIMIZE” or “ANALYZE” queries. Just the normal SELECT/INSERT/UPDATE/…

    Comment :: May 21, 2009 @ 10:10 pm

  12. Shlomi,

    Yes… This is a design bug which is waiting to be fixed for years.

    Comment :: May 21, 2009 @ 10:24 pm

  13. 13. Chip Turner

    Check out mypgrep.py from the google mysql tools project on code.google.com. It basically is a supercharged process grep for mysql, including the ability to kill processes.

    Comment :: May 21, 2009 @ 11:59 pm

  14. A few years ago I wrote an article about how to use Vim with mysql to generate and execute SQL statements. It’s here: http://peterstuifzand.nl/external-programs-in-vim.html

    Comment :: May 22, 2009 @ 5:37 am

  15. Thanks Chip,

    The good thing with having it done on SQL level is you do not need to install everything – not every server running MySQL has working perl python on php installed

    Comment :: May 22, 2009 @ 9:48 am

  16. 16. Bryan

    If information_schema.processlist doesn’t exist on your version of MySQL, this works in a linux script:

    #!/bin/bash
    for each in `mysqladmin -u root -prootpwd processlist | awk ‘{print $2, $4, $8}’ | grep $dbname | grep $dbuser | awk ‘{print $1}’`;
    do mysqladmin -u root -prootpwd kill $each;
    done

    Comment :: May 27, 2009 @ 8:24 am

  17. Bryan,

    Which version of Percona patches are you running ? It exists in recent ones for sure.

    Thanks for command line version though.

    Comment :: May 27, 2009 @ 8:34 am

  18. 18. Bryan

    No Percona patches yet, unfortunately. We’re running the standard MySQL 5.0.77 on Centos.

    Comment :: May 27, 2009 @ 8:49 am

  19. I see,

    Standard MySQL is not what we call “our” MySQL :)

    Comment :: May 27, 2009 @ 9:04 am

  20. 20. Glynn Durham

    How about a technique like this:

    . File optimizeworld.sfs:

    # Optimize tables in the World database.
    SELECT ‘OPTIMIZE TABLE ‘, CONCAT(TABLE_SCHEMA, ‘.’, TABLE_NAME, ‘;’)
    FROM INFORMATION_SCHEMA.TABLES
    WHERE TABLE_SCHEMA = ‘World’;

    . To execute:

    shell> mysql –skip-column-names < optimizeworld.sfs | mysql

    Comment :: May 29, 2009 @ 3:39 am

  21. 21. Andrew Watson

    I do this:

    mysqladmin proc | grep Sleep | sort -r -n -k6 | awk {’print $1; ‘} | xargs mysqladmin kill

    or something to that effect…

    Comment :: May 29, 2009 @ 9:38 am

 

Subscribe without commenting

Trackbacks/Pingbacks