Mysql uses kill command to solve deadlock problem of kills an executing sql statement

  • 2021-08-12 03:54:00
  • OfStack

When some statements are run using mysql, deadlocks are not reflected due to the large amount of data. At this time, you need kill to drop an query statement that is consuming resources. The syntax format of KILL command is as follows:


KILL [CONNECTION | QUERY] thread_id

Each connection to mysqld runs in a separate thread, you can use the SHOW PROCESSLIST statement to see which threads are running, and use the KILL thread_id statement to terminate one thread.

KILL allows optional CONNECTION or QUERY modifiers: KILL CONNECTION and KILL1 without modifiers: it terminates the connection associated with a given thread_id. KILL QUERY terminates the connection to the statement currently being executed, but leaves the connection intact.

If you have PROCESS permissions, you can view all threads. If you have super administrator privileges, you can terminate all threads and statements. Otherwise, you can only view and terminate your own threads and statements. You can also use the mysqladmin processlist and mysqladmin kill commands to check and terminate threads.

First log in to MySQL, then use: show processlist; View the status of each thread in the current mysql.


mysql> show processlist; 
+------+------+----------------------+----------------+---------+-------+-----------+---------------------  
| Id  | User | Host         | db       | Command | Time | State   | Info 
+------+------+----------------------+----------------+---------+-------+-----------+---------------------  
| 7028 | root | ucap-devgroup:53396 | platform    | Sleep  | 19553 |      | NULL  
| 8352 | root | ucap-devgroup:54794 | platform    | Sleep  | 4245 |      | NULL 
| 8353 | root | ucap-devgroup:54795 | platform    | Sleep  |   3 |      | NULL 
| 8358 | root | ucap-devgroup:62605 | platform    | query  | 4156 | updating | update t_shop set | 

The above shows the list of sql statements currently being executed, and find the id corresponding to the statement that consumes the most resources.

Then run the kill command in the following format:


kill id; 
-  Example : 
kill 8358 

Just kill it.


Related articles: