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

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

    I'd combine bash and mysql:

    for i in $(mysql -Ne "select id from information_schema.processlist where user like 'foo%user' and time > 300;"); do
      mysql -e "kill ${i}"
    done
    
    0 讨论(0)
  • 2020-12-04 05:29

    login to Mysql as admin:

     mysql -uroot -ppassword;
    

    And than run command:

    mysql> show processlist;
    

    You will get something like below :

    +----+-------------+--------------------+----------+---------+------+-------+------------------+
    | Id | User        | Host               | db       | Command | Time | State | Info             |
    +----+-------------+--------------------+----------+---------+------+-------+------------------+
    | 49 | application | 192.168.44.1:51718 | XXXXXXXX | Sleep   |  183 |       | NULL             ||
    | 55 | application | 192.168.44.1:51769 | XXXXXXXX | Sleep   |  148 |       | NULL             |
    | 56 | application | 192.168.44.1:51770 | XXXXXXXX | Sleep   |  148 |       | NULL             |
    | 57 | application | 192.168.44.1:51771 | XXXXXXXX | Sleep   |  148 |       | NULL             |
    | 58 | application | 192.168.44.1:51968 | XXXXXXXX | Sleep   |   11 |       | NULL             |
    | 59 | root        | localhost          | NULL     | Query   |    0 | NULL  | show processlist |
    +----+-------------+--------------------+----------+---------+------+-------+------------------+
    

    You will see complete details of different connections. Now you can kill the sleeping connection as below:

    mysql> kill 52;
    Query OK, 0 rows affected (0.00 sec)
    
    0 讨论(0)
  • 2020-12-04 05:30

    An easy way would be to restart the mysql server.. Open "services.msc" in windows Run, select Mysql from the list. Right click and stop the service. Then Start again and all the processes would have been killed except the one (the default reserved connection)

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

    for python language, you can do like this

    import pymysql
    
    connection = pymysql.connect(host='localhost',
                                 user='root',
                                 db='mysql',
                                 cursorclass=pymysql.cursors.DictCursor)
    
    with connection.cursor() as cursor:
        cursor.execute('SHOW PROCESSLIST')
        for item in cursor.fetchall():
            if item.get('Time') > 200:
                _id = item.get('Id')
                print('kill %s' % item)
                cursor.execute('kill %s', _id)
        connection.close()
    
    0 讨论(0)
  • 2020-12-04 05:33

    Or... in shell...

    service mysql restart
    

    Yeah, I know, I'm lazy, but it can be handy too.

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

    KILL ALL SELECT QUERIES

    select concat('KILL ',id,';') 
    from information_schema.processlist 
    where user='root' 
      and INFO like 'SELECT%' into outfile '/tmp/a.txt'; 
    source /tmp/a.txt;
    
    0 讨论(0)
提交回复
热议问题