mysql死锁检测和发现

mysql有死锁检测机制,发现死锁,会马上回滚一个事务,释放锁。

发现死锁很简单。
**SHOW ENGINE INNODB STATUS;((里面就有最新一条死锁的信息,包括造成死锁的两个事务执行了什么语句。

举个简单例子:
事务1:select * from article where id = 3035108785 for update;
事务2:select * from article where id = 2985377876 for update;
事务1:select * from article where id = 3035108785 for update;堵塞
事务2:select * from article where id = 2985377876 for update;
死锁,回滚:Deadlock found when trying to get lock; try restarting transaction

执行 SHOW ENGINE INNODB STATUS;
可以看到,ip,sql,锁详细信息。

------------------------
LATEST DETECTED DEADLOCK
------------------------
2019-05-22 19:25:07 0x1af0
*** (1) TRANSACTION:
TRANSACTION 5838, ACTIVE 1411 sec starting index read
mysql tables in use 1, locked 1
LOCK WAIT 3 lock struct(s), heap size 1136, 2 row lock(s)
MySQL thread id 18, OS thread handle 9372, query id 1266 localhost ::1 root statistics
select * from article where id = 3035108785 for update
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 60 page no 6 n bits 160 index PRIMARY of table `toutiao`.`article` trx id 5838 lock_mode X locks rec but not gap waiting
Record lock, heap no 4 PHYSICAL RECORD: n_fields 8; compact format; info bits 0
 0: len 8; hex 80000000b4e815b1; asc         ;;
 1: len 6; hex 000000001622; asc      ";;
 2: len 7; hex a2000001150130; asc       0;;
 3: len 30; hex 687474703a2f2f7777772e707561686f6d652e636f6d2f6262732f707561; asc http://www.puahome.com/bbs/pua; (total 45 bytes);
 4: len 30; hex e8b088e6818be788b1efbc8ce5a5b3e7949fe88ab1e794b7e7949fe79a84; asc                               ; (total 54 bytes);
 5: len 9; hex e59d8fe794b7e5ada9; asc          ;;
 6: len 14; hex 2ce5a99ae5a7bb2ce788b1e68385; asc ,      ,      ;;
 7: len 4; hex 531ed65d; asc S  ];;

*** (2) TRANSACTION:
TRANSACTION 5839, ACTIVE 1406 sec starting index read
mysql tables in use 1, locked 1
3 lock struct(s), heap size 1136, 4 row lock(s)
MySQL thread id 19, OS thread handle 6896, query id 1267 localhost ::1 root statistics
select * from article where id = 2985377876 for update
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 60 page no 6 n bits 160 index PRIMARY of table `toutiao`.`article` trx id 5839 lock_mode X locks rec but not gap
Record lock, heap no 4 PHYSICAL RECORD: n_fields 8; compact format; info bits 0
 0: len 8; hex 80000000b4e815b1; asc         ;;
 1: len 6; hex 000000001622; asc      ";;
 2: len 7; hex a2000001150130; asc       0;;
 3: len 30; hex 687474703a2f2f7777772e707561686f6d652e636f6d2f6262732f707561; asc http://www.puahome.com/bbs/pua; (total 45 bytes);
 4: len 30; hex e8b088e6818be788b1efbc8ce5a5b3e7949fe88ab1e794b7e7949fe79a84; asc                               ; (total 54 bytes);
 5: len 9; hex e59d8fe794b7e5ada9; asc          ;;
 6: len 14; hex 2ce5a99ae5a7bb2ce788b1e68385; asc ,      ,      ;;
 7: len 4; hex 531ed65d; asc S  ];;

*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 60 page no 6 n bits 160 index PRIMARY of table `toutiao`.`article` trx id 5839 lock_mode X locks rec but not gap waiting
Record lock, heap no 3 PHYSICAL RECORD: n_fields 8; compact format; info bits 0
 0: len 8; hex 80000000b1f14054; asc       @T;;
 1: len 6; hex 000000001622; asc      ";;
 2: len 7; hex a2000001150120; asc        ;;
 3: len 30; hex 687474703a2f2f746f757469616f2e636f6d2f67726f75702f3239383533; asc http://toutiao.com/group/29853; (total 36 bytes);
 4: len 30; hex 36e7a78de7baafe5b19ee6868be587bae69da5e79a84e7978520e4bda0e7; asc 6                             ; (total 38 bytes);
 5: len 9; hex e59083e5afb9e88daf; asc          ;;
 6: SQL NULL;
 7: len 4; hex 55d54c7e; asc U L~;;

*** WE ROLL BACK TRANSACTION (2)
------------
TRANSACTIONS
------------
Trx id counter 5840
Purge done for trx's n:o < 5820 undo n:o < 0 state: running but idle
History list length 20
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 283855927864832, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 283855927863960, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 5838, ACTIVE 1617 sec
3 lock struct(s), heap size 1136, 2 row lock(s)
MySQL thread id 18, OS thread handle 9372, query id 1266 localhost ::1 root
--------

你可能感兴趣的:(mysql死锁检测和发现)