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

Vue Virtual DOM Quick Start

Table of contents Virtual DOM What is virtual dom...

Writing a rock-paper-scissors game in JavaScript

This article shares the specific code for writing...

Detailed explanation of loop usage in javascript examples

I was bored and sorted out some simple exercises ...

Unbind SSH key pairs from one or more Linux instances

DetachKeyPair Unbind SSH key pairs from one or mo...

Solution to the ineffective global style of the mini program custom component

Table of contents Too long to read Component styl...

Solution for Docker container not recognizing fonts such as Songti

Problem background: When using docker to deploy t...

How to create a view on multiple tables in MySQL

In MySQL, create a view on two or more base table...

JavaScript implements single linked list process analysis

Preface: To store multiple elements, arrays are t...

Detailed explanation of CocosCreator message distribution mechanism

Overview This article begins to introduce content...

Html tips to make your code semantic

Html semantics seems to be a commonplace issue. G...

mysql5.6.8 source code installation process

Kernel: [root@opop ~]# cat /etc/centos-release Ce...

Overview of MySQL Statistics

MySQL executes SQL through the process of SQL par...

MySQL 8.0.13 decompression version installation graphic tutorial under Windows

This article shares with you the MySQL 8.0.13 ins...

Detailed explanation of the use of Join in Mysql

In the previous chapters, we have learned how to ...