Mysql uses the kill command to solve the deadlock problem (kill a certain SQL statement being executed)

Mysql uses the kill command to solve the deadlock problem (kill a certain SQL statement being executed)

When using MySQL to run certain statements, a deadlock may occur due to the large amount of data, and no response will be given. At this time, you need to kill a query statement that is consuming resources. The syntax format of the 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 the KILL thread_id statement to kill a thread.

KILL allows optional CONNECTION or QUERY modifiers: KILL CONNECTION is the same as KILL with no modifiers: it terminates the connection associated with the given thread_id. KILL QUERY terminates the statement currently executing on the connection, but leaves the connection intact.

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

First log in to MySQL, and then use: show processlist; to 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 finds the id corresponding to the statement that consumes the most resources.

Then run the kill command. The command format is as follows:

kill id; 
- Example: 
kill 8358

Just kill it.

You may also be interested in:
  • mysql show processlist displays the mysql query process
  • Reasons why MySQL kill cannot kill threads
  • How to view and kill processes in MYSQL

<<:  How to use glog log library in Linux environment

>>:  Which loop is the fastest in JavaScript?

Recommend

React handwriting tab switching problem

Parent File import React, { useState } from '...

Web data storage: Cookie, UserData, SessionStorage, WebSqlDatabase

Cookie It is a standard way to save the state of ...

js to achieve drag and drop sorting details

Table of contents 1. Introduction 2. Implementati...

Let's talk about the Vue life cycle in detail

Table of contents Preface 1. Life cycle in Vue2 I...

About nginx to implement jira reverse proxy

Summary: Configure nginx reverse proxy jira and i...

Detailed steps to install mysql5.7.18 on Mac

1. Tools We need two tools now: MySQL server (mys...

HTML drag and drop function implementation code

Based on Vue The core idea of ​​this function is ...

MySQL detailed explanation of isolation level operation process (cmd)

Read uncommitted example operation process - Read...

JavaScript implements AI tic-tac-toe game through the maximum and minimum algorithm

Without further ado, let’s run the screenshot dir...

50 lines of code to implement Webpack component usage statistics

background Recently, a leader wanted us to build ...

How to use history redirection in React Router

In react-router, the jump in the component can be...

Introduction to Jenkins and how to deploy Jenkins with Docker

1. Related concepts 1.1 Jenkins Concepts: Jenkins...

js and jquery to achieve tab status bar switching effect

Today we will make a simple case, using js and jq...