mysql将死锁信息记录到error log

由一个参数控制

mysql> show variables like '%deadlock%';
+----------------------------+-------+
| Variable_name              | Value |
+----------------------------+-------+
| innodb_print_all_deadlocks | OFF   |
+----------------------------+-------+
1 row in set (0.20 sec)


mysql> set innodb_print_all_deadlocks=1;
ERROR 1229 (HY000): Variable 'innodb_print_all_deadlocks' is a GLOBAL variable and should be set with SET GLOBAL


mysql> set global innodb_print_all_deadlocks=1;
Query OK, 0 rows affected (0.00 sec)




产生一个死锁:


会话1:


mysql> select * from t1;
+----+------+
| id | name |
+----+------+
|  1 | b    |
|  5 | c    |
+----+------+
2 rows in set (0.02 sec)


mysql> begin;
Query OK, 0 rows affected (0.00 sec)


mysql> update t1 set name='b1' where id=1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0




会话2:


mysql> select * from t1;
+----+------+
| id | name |
+----+------+
|  1 | b    |
|  5 | c    |
+----+------+
2 rows in set (0.00 sec)


mysql> begin;
Query OK, 0 rows affected (0.00 sec)


mysql> update t1 set name='c2' where id=5;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0


会话1:


mysql> update t1 set name='c1' where id=5; ----等待


会话2:


mysql> update t1 set name='b2' where id=1;
ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction


会话1:


mysql> update t1 set name='c1' where id=5;
Query OK, 1 row affected (12.90 sec)
Rows matched: 1  Changed: 1  Warnings: 0   ----------执行成功




此时的error log中有如下信息:


2016-09-13 17:29:45 7f63ce28e700InnoDB: transactions deadlock detected, dumping detailed information.
2016-09-13 17:29:45 7f63ce28e700
*** (1) TRANSACTION:
TRANSACTION 1521180, ACTIVE 38 sec starting index read
mysql tables in use 1, locked 1
LOCK WAIT 3 lock struct(s), heap size 360, 2 row lock(s), undo log entries 1
MySQL thread id 1, OS thread handle 0x7f63ce2bf700, query id 62 localhost root updating
update t1 set name='c1' where id=5-----------------------------------------------------------会话1执行这个sql的时候,由于锁被会话2持有,而等待。
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 37 page no 3 n bits 72 index `PRIMARY` of table `gw`.`t1` trx id 1521180 lock_mode X locks rec but not gap waiting
Record lock, heap no 3 PHYSICAL RECORD: n_fields 4; compact format; info bits 0
 0: len 4; hex 80000005; asc     ;;
 1: len 6; hex 00000017361d; asc     6 ;;
 2: len 7; hex 140000005e0c7b; asc     ^ {;;
 3: len 5; hex 6332202020; asc c2   ;;


*** (2) TRANSACTION:
TRANSACTION 1521181, ACTIVE 24 sec starting index read
mysql tables in use 1, locked 1
3 lock struct(s), heap size 360, 2 row lock(s), undo log entries 1
MySQL thread id 2, OS thread handle 0x7f63ce28e700, query id 63 localhost root updating
update t1 set name='b2' where id=1---------------------------------------------------------会话2执行个sql后产生了死锁
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 37 page no 3 n bits 72 index `PRIMARY` of table `gw`.`t1` trx id 1521181 lock_mode X locks rec but not gap
Record lock, heap no 3 PHYSICAL RECORD: n_fields 4; compact format; info bits 0
 0: len 4; hex 80000005; asc     ;;
 1: len 6; hex 00000017361d; asc     6 ;;
 2: len 7; hex 140000005e0c7b; asc     ^ {;;
 3: len 5; hex 6332202020; asc c2   ;;


*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 37 page no 3 n bits 72 index `PRIMARY` of table `gw`.`t1` trx id 1521181 lock_mode X locks rec but not gap waiting
Record lock, heap no 2 PHYSICAL RECORD: n_fields 4; compact format; info bits 0
 0: len 4; hex 80000001; asc     ;;
 1: len 6; hex 00000017361c; asc     6 ;;
 2: len 7; hex 13000000760a0f; asc     v  ;;
 3: len 5; hex 6231202020; asc b1   ;;


*** WE ROLL BACK TRANSACTION (2)

你可能感兴趣的:(mysql)