In-depth explanation of Mysql deadlock viewing and deadlock removal

In-depth explanation of Mysql deadlock viewing and deadlock removal

Preface

I encountered a Mysql deadlock problem some time ago, so I sorted it out.

Problem description: Mysql modification statements do not seem to take effect, and an exception will pop up when using the Mysql GUI tool to edit the value of a field.

What is deadlock?

Before solving the MySQL deadlock problem, let's first understand what a deadlock is.

Deadlock refers to a phenomenon in which two or more processes wait for each other due to competition for resources during execution. Without external force, they will not be able to move forward. At this time, the system is said to be in a deadlock state or the system has a deadlock. These processes that are always waiting for each other are called deadlocked processes.

Deadlock manifestation

There are two specific manifestations of deadlock:

Mysql add and modify statements do not work properly. When you use the Mysql GUI tool to edit the value of a field, an exception occurs. How to avoid deadlock

The way to prevent deadlock is to avoid situations where deadlock conditions are met. To this end, we need to follow the following principles during the development process:

1. Try to avoid concurrent execution of statements that involve modifying data.

2. Each transaction is required to lock all the data to be used at one time, otherwise execution is not allowed.

3. A locking order is specified in advance, and all transactions must lock the data in this order. For example, the order in which different processes update objects within a transaction should be kept consistent as much as possible.

View Deadlocks

There are many ways to query whether there is a locked table in MySQL. Here we only introduce the most commonly used one.

1. View ongoing transactions

SELECT * FROM information_schema.INNODB_TRX

You can see that the transaction with process id 3175 is locked, and another transaction with id 3173 is being executed, but has not been committed.

2. View the locked transaction

SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCKS; 

3. View transactions waiting for locks

SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCK_WAITS; 

4. Check whether the table is locked

SHOW OPEN TABLES where In_use > 0; 

When a deadlock occurs, these methods can query information related to the current deadlock.

5. View the log of recent deadlocks

show engine innodb status 

Resolving deadlock

If you need to resolve the deadlock, there is a simple and brutal way, which is to find the process ID and kill it directly.

View the processes currently in progress

show processlist

// You can also use SELECT * FROM information_schema.INNODB_TRX;

The process id found by these two commands is the same.

Kill the process ID corresponding to the process

kill id

Verify (check if there is still a lock after killing)

SHOW OPEN TABLES where In_use > 0;

Reference Links

Mysql view table and unlock table

What is MySQL deadlock?

This is the end of this article about MySQL deadlock checking and deadlock removal. For more information about MySQL deadlock checking and deadlock removal, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • The normal method of MySQL deadlock check processing
  • Causes and solutions for MySQL deadlock
  • In-depth analysis of MySQL deadlock issues
  • MySQL deadlock routine: inconsistent batch insertion order under unique index
  • Analysis of a MySQL deadlock scenario example
  • A magical MySQL deadlock troubleshooting record
  • Analysis of Purge deadlock problem in MySQL database
  • Detailed explanation of distributed deadlock detection and elimination through SQL

<<:  Detailed explanation of several horizontal and vertical centering methods in HTML (Basics)

>>:  Create a movable stack widget function using flutter

Recommend

How to use Nginx to handle cross-domain Vue development environment

1. Demand The local test domain name is the same ...

Solution to Navicat Premier remote connection to MySQL error 10038

Remote connection to MySQL fails, there may be th...

Some common advanced SQL statements in MySQL

MySQL Advanced SQL Statements use kgc; create tab...

Summary of Linux sftp command usage

sftp is the abbreviation of Secure File Transfer ...

Mysql modify stored procedure related permissions issue

When using MySQL database, you often encounter su...

JavaScript implements select all and unselect all operations

This article shares the specific code for JavaScr...

MySQL account password modification method (summary)

Preface: In the daily use of the database, it is ...

Analyze how uniapp dynamically obtains the interface domain name

background The interface domain name is not hard-...

Detailed installation process and basic usage of MySQL under Windows

Table of contents 1. Download MySQL 2. Install My...

Summary of commonly used SQL statements for creating MySQL tables

Recently, I have been working on a project and ne...

Specific use of MySQL global locks and table-level locks

Table of contents Preface Global Lock Table lock ...

JS implements the dragging and placeholder functions of elements

This blog post is about a difficulty encountered ...

Diagram of the process of implementing direction proxy through nginx

This article mainly introduces the process of imp...