Normally, when a deadlock occurs, the connection with the smallest weight will be killed and rolled back. However, in order to find statements to optimize, enable deadlock and record deadlock information. #step 1: Window 1mysql> start transaction; mysql> update aa set name='aaa' where id = 1; #step 2: Window 2mysql> start transaction; mysql> update bb set name='bbb' where id = 1; #step 3: Window 1mysql> update bb set name='bbb'; #step 4: Window 3#Whether to submit automaticallymysql> show variables like 'autocommit'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | autocommit | ON | +---------------+-------+ #View the current connectionmysql> show processlist; mysql> show full processlist; mysql> SELECT * FROM INFORMATION_SCHEMA.PROCESSLIST; +----+------+-----------+------+---------+------+-------+-----------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------+------+---------+------+-------+-----------------+ | 4 | root | localhost | test | Sleep | 244 | | NULL | | 5 | root | localhost | test | Sleep | 111 | | NULL | | 6 | root | localhost | NULL | Query | 0 | init | show processlist | +----+------+-----------+------+---------+------+-------+-----------------+ #View the currently locked transaction (cannot be found after the lock request times out) mysql> SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCKS; +------------------+-------------+-----------+-----------+-------------+-----------------+------------+-----------+----------+----------------+ | lock_id | lock_trx_id | lock_mode | lock_type | lock_table | lock_index | lock_space | lock_page | lock_rec | lock_data | +------------------+-------------+-----------+-----------+-------------+-----------------+------------+-----------+----------+----------------+ | 130718495:65:3:4 | 130718495 | X | RECORD | `test`.`bb` | GEN_CLUST_INDEX | 65 | 3 | 4 | 0x000000000300 | | 130718496:65:3:4 | 130718496 | X | RECORD | `test`.`bb` | GEN_CLUST_INDEX | 65 | 3 | 4 | 0x000000000300 | +------------------+-------------+-----------+-----------+-------------+-----------------+------------+-----------+----------+----------------+ #View the transactions currently waiting for locks (cannot be found after the lock request times out) mysql> SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCK_WAITS; +-------------------+-------------------+-----------------+------------------+ | requesting_trx_id | requested_lock_id | blocking_trx_id | blocking_lock_id | +-------------------+-------------------+-----------------+------------------+ | 130718499 | 130718499:65:3:4 | 130718500 | 130718500:65:3:4 | +-------------------+-------------------+-----------------+------------------+ #View the current uncommitted transaction (if the deadlock wait times out, the transaction may not be closed yet) mysql> SELECT * FROM INFORMATION_SCHEMA.INNODB_TRX; +--------------------------------------------------------------------------------------------------------+ | trx_id | trx_state | trx_started | trx_requested_lock_id | trx_wait_started | trx_weight | +-----------+-----------+---------------------+-----------------------+---------------------+------------+ | 130718500 | RUNNING | 2018-03-12 09:28:10 | NULL | NULL | 3 | | 130718499 | LOCK WAIT | 2018-03-12 09:27:59 | 130718499:65:3:4 | 2018-03-12 09:32:48 | 5 | ========================================================================================================== | trx_mysql_thread_id | trx_query | trx_operation_state | trx_tables_in_use | +---------------------+---------------------------------------+---------------------+-------------------+ | 4 | NULL | NULL | 0 | | 5 | update bb set name='bbb' | starting index read | 1 | ========================================================================================================= | trx_tables_locked | trx_lock_structs | trx_lock_memory_bytes | trx_rows_locked | trx_rows_modified | +-------------------+------------------+-----------------------+-------------------+-------------------+ | 0 | 2 | 360 | 3 | 1 | | 1 | 4 | 1184 | 4 | 1 | =========================================================================================================================== | trx_concurrency_tickets | trx_isolation_level | trx_unique_checks | trx_foreign_key_checks | trx_last_foreign_key_error | +-------------------------+---------------------+-------------------+------------------------+----------------------------+ | 0 | REPEATABLE READ | 1 | 1 | NULL | | 0 | REPEATABLE READ | 1 | 1 | NULL | =========================================================================================================================== | trx_adaptive_hash_latched | trx_adaptive_hash_timeout | trx_is_read_only | trx_autocommit_non_locking | +---------------------------+---------------------------+------------------+----------------------------+ | 0 | 10000 | 0 | 0 | | 0 | 10000 | 0 | 0 | +---------------------------+---------------------------+------------------+----------------------------+ #View the tables being accessed mysql> show OPEN TABLES where In_use > 0; +----------+-------+--------+-------------+ | Database | Table | In_use | Name_locked | +----------+-------+--------+-------------+ | test | bb | 1 | 0 | +----------+-------+--------+-------------+ #step 3: Window 1 (If the lock request in step 3 takes too long, the lock will time out and the execution will be terminated) mysql> update bb set name='bbb'; ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction #Before the lock request of "window one" times out, execute step 5 to cause a deadlock. Then the execution of the connection "window two" is terminated, and "window one" is successfully executed. #step 5: Window twomysql> update aa set name='aa'; ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction View the most recent deadlock situation #View the most recent deadlock situationmysql> SHOW ENGINE INNODB STATUS\G; ............... ------------------------ LATEST DETECTED DEADLOCK ------------------------ 2018-03-12 11:01:06 7ffb4993a700 #Occurrence time*** (1) TRANSACTION: #Transaction 1 TRANSACTION 130718515, ACTIVE 19 sec starting index read mysql tables in use 1, locked 1 #table being accessed LOCK WAIT 4 lock struct(s), heap size 1184, 4 row lock(s), undo log entries 1 #number of rows affected MySQL thread id 5, OS thread handle 0x7ffb498f8700, query id 205 localhost root updating #thread/connection host/userupdate bb set name='bb' #request statement*** (1) WAITING FOR THIS LOCK TO BE GRANTED: #waiting for the following resources (lock position and lock mode) RECORD LOCKS space id 65 page no 3 n bits 72 index `GEN_CLUST_INDEX` of table `test`.`bb` trx id 130718515 lock_mode X waiting Record lock, heap no 5 PHYSICAL RECORD: n_fields 5; compact format; info bits 0 0: len 6; hex 000000000300; asc ;; 1: len 6; hex 000007ca9b34; asc 4;; 2: len 7; hex 1f000002092075; asc u;; 3: len 4; hex 80000001; asc ;; 4: len 2; hex 6262; asc bb;; *** (2) TRANSACTION: #Transaction 2 TRANSACTION 130718516, ACTIVE 14 sec starting index read mysql tables in use 1, locked 1 4 lock struct(s), heap size 1184, 4 row lock(s), undo log entries 1 MySQL thread id 4, OS thread handle 0x7ffb4993a700, query id 206 localhost root updating update aa set name='aa' #Request statement*** (2) HOLDS THE LOCK(S): #Holding lock resources RECORD LOCKS space id 65 page no 3 n bits 72 index `GEN_CLUST_INDEX` of table `test`.`bb` trx id 130718516 lock_mode X Record lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0 0: len 8; hex 73757072656d756d; asc supremum;; Record lock, heap no 3 PHYSICAL RECORD: n_fields 5; compact format; info bits 0 0: len 6; hex 000000000301; asc ;; 1: len 6; hex 000007ca9b17; asc ;; 2: len 7; hex 9000000144011e; asc D ;; 3: len 4; hex 80000002; asc ;; 4: len 2; hex 6262; asc bb;; Record lock, heap no 5 PHYSICAL RECORD: n_fields 5; compact format; info bits 0 0: len 6; hex 000000000300; asc ;; 1: len 6; hex 000007ca9b34; asc 4;; 2: len 7; hex 1f000002092075; asc u;; 3: len 4; hex 80000001; asc ;; 4: len 2; hex 6262; asc bb;; *** (2) WAITING FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 64 page no 3 n bits 80 index `GEN_CLUST_INDEX` of table `test`.`aa` trx id 130718516 lock_mode X waiting Record lock, heap no 7 PHYSICAL RECORD: n_fields 5; compact format; info bits 0 0: len 6; hex 000000000200; asc ;; 1: len 6; hex 000007ca9b33; asc 3;; 2: len 7; hex 1e000001d53057; asc 0W;; 3: len 4; hex 80000001; asc ;; 4: len 2; hex 6161; asc aa;; *** WE ROLL BACK TRANSACTION (2) ............... #Deadlock records only record the most recent deadlock information. To save each deadlock information to the error log, enable the following parameters: mysql> show variables like 'innodb_print_all_deadlocks'; +----------------------------+-------+ | Variable_name | Value | +----------------------------+-------+ | innodb_print_all_deadlocks | OFF | +----------------------------+-------+ #If the resource cannot be requested in [step 3: window 1] above, the lock wait timeout will occur after the default of 50 seconds. mysql> show variables like 'innodb_lock_wait_timeout'; +--------------------------+-------+ | Variable_name | Value | +--------------------------+-------+ | innodb_lock_wait_timeout | 50 | +--------------------------+-------+ ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction #Set the global variable lock waiting timeout to 60 seconds (new connection takes effect) #mysql> set session innodb_lock_wait_timeout=50; mysql> set global innodb_lock_wait_timeout=60; #In the above test, when a statement in a transaction times out, only the statement is rolled back, and the integrity of the transaction is destroyed. To rollback this transaction, enable the following parameters: mysql> show variables like 'innodb_rollback_on_timeout'; +----------------------------+-------+ | Variable_name | Value | +----------------------------+-------+ | innodb_rollback_on_timeout | OFF | +----------------------------+-------+ The final parameter settings are as follows: (Restart the service and reconnect the test) [mysqld] log-error=/var/log/mysqld3306.log innodb_lock_wait_timeout=60 #Lock request timeout (seconds) innodb_rollback_on_timeout = 1 #If a lock request of a statement in a transaction times out, the entire transaction will be rolled back innodb_print_all_deadlocks = 1 #All deadlocks are saved to the error log #If you delete the blocked session manually, delete the one with Command='Sleep', no State, no Info, and the smallest trx_weight. show processlist; SELECT trx_mysql_thread_id,trx_state,trx_started,trx_weight FROM INFORMATION_SCHEMA.INNODB_TRX; Summarize This is the end of this article about MySQL deadlock check and processing. For more relevant MySQL deadlock check and processing content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Vue3 compilation process-source code analysis
>>: How to update, package, and upload Docker containers to Alibaba Cloud
one. First of all, you have to package it in idea...
This article shares the specific code of vue+echa...
<br />This tag can be used to directly add a...
What is em? em refers to the font height, and the ...
The docker image id is unique and can physically ...
Six steps to install MySQL (only the installation...
I have never been able to figure out whether the ...
Uninstall MariaDB CentOS7 installs MariaDB instea...
This article mainly introduces 6 solutions to the...
We all know that the commonly used positioning me...
Tab bar: Click different tabs to display differen...
1. CDN It is the most commonly used acceleration ...
Table of contents Preface Stored Procedure: 1. Cr...
Use JS to complete a simple calculator for your r...
<frameset></frameset> is familiar to e...