测试环境:MySQL 5.7.26
创建测试表:
mysql> create table t4(id int,name varchar(10),primary key(id),unique key(name));
Query OK, 0 rows affected (0.01 sec)
插入测试数据:
mysql> insert into t4 values(1,'aaa');
Query OK, 1 row affected (0.00 sec)
mysql> insert into t4 values(2,'bbb');
Query OK, 1 row affected (0.00 sec)
模拟死锁过程:
过程说明:
查看死锁日志:
LATEST DETECTED DEADLOCK
------------------------
2020-08-21 18:45:50 0x7f38f84d3700
*** (1) TRANSACTION:
TRANSACTION 26176862, ACTIVE 17 sec starting index read
mysql tables in use 1, locked 1
LOCK WAIT 2 lock struct(s), heap size 1136, 1 row lock(s)
MySQL thread id 208, OS thread handle 139883056465664, query id 3863533 localhost root updating
delete from t4 where name='aaa'
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 834 page no 4 n bits 72 index name of table `test`.`t4` trx id 26176862 lock_mode X locks rec but not gap waiting
Record lock, heap no 2 PHYSICAL RECORD: n_fields 2; compact format; info bits 32
0: len 3; hex 616161; asc aaa;;
1: len 4; hex 80000001; asc ;;
*** (2) TRANSACTION:
TRANSACTION 26176839, ACTIVE 28 sec inserting, thread declared inside InnoDB 5000
mysql tables in use 1, locked 1
4 lock struct(s), heap size 1136, 3 row lock(s), undo log entries 2
MySQL thread id 209, OS thread handle 139882660706048, query id 3863602 localhost root update
insert into t4 values(3,'aaa')
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 834 page no 4 n bits 72 index name of table `test`.`t4` trx id 26176839 lock_mode X locks rec but not gap
Record lock, heap no 2 PHYSICAL RECORD: n_fields 2; compact format; info bits 32
0: len 3; hex 616161; asc aaa;;
1: len 4; hex 80000001; asc ;;
*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 834 page no 4 n bits 72 index name of table `test`.`t4` trx id 26176839 lock mode S waiting
Record lock, heap no 2 PHYSICAL RECORD: n_fields 2; compact format; info bits 32
0: len 3; hex 616161; asc aaa;;
1: len 4; hex 80000001; asc ;;
*** WE ROLL BACK TRANSACTION (1)