Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MySQL

แสดง Deadlock Logs ใน MySQL ด้วย innodb_print_all_deadlocks

เปิดการบันทึก Deadlock ทั้งหมดด้วย innodb_print_all_deadlocks แล้วดูด้วย SHOW ENGINE INNODB STATUS

ขั้นตอนที่ 1: เปิดการบันทึก Deadlock

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

ขั้นตอนที่ 2: ดูสถานะ InnoDB (รวม Deadlock logs)

mysql> SHOW ENGINE INNODB STATUS;

ผลลัพธ์ (ส่วน Deadlock)

+--------+------+-------------------------------------------------------------------+
| Type   | Name | Status                                                            |
+--------+------+-------------------------------------------------------------------+
| InnoDB |      | =====================================
2019-05-03 16:44:53 0xf9c INNODB MONITOR OUTPUT
...
------------------------
LATEST DETECTED DEADLOCK
------------------------
2019-05-03 16:44:53 0xf9c
*** (1) TRANSACTION:
TRANSACTION 12345, ACTIVE 0 sec fetching rows
mysql tables in use 1, locked 1
LOCK WAIT 2 lock struct(s), heap size 1136, 1 row lock(s)
MySQL thread id 10, OS thread handle 0x1234, query id 100 localhost root
UPDATE t SET c = c + 1 WHERE id = 1
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 10 page no 3 n bits 72 index PRIMARY of table `test`.`t`
trx id 12345 lock_mode X locks rec but not gap waiting
Record lock, heap no 2 PHYSICAL RECORD: n_fields 3; compact format; info bits 0
 0: len 4; hex 80000001; asc     ;;
 1: len 6; hex 000000000302; asc       ;;
 2: len 4; hex 8000000a; asc     ;;

*** (2) TRANSACTION:
TRANSACTION 12346, ACTIVE 0 sec fetching rows
...
*** WE ROLL BACK TRANSACTION (1)
...
------------
END OF INNODB MONITOR OUTPUT
============================= |

ส่วนที่สำคัญ: LATEST DETECTED DEADLOCK แสดงรายละเอียด Transaction ทั้งสองที่ Deadlock, Query ที่รัน, Lock ที่รอ, และ Transaction ไหนถูก Rollback

หมายเหตุ

  • innodb_print_all_deadlocks ต้องการสิทธิ์ SUPER
  • มีผลต่อ Performance เล็กน้อย (เขียน log ทุกครั้งที่ deadlock)
  • ตั้งค่าตลอดกาล: เพิ่ม innodb_print_all_deadlocks=1 ใน my.cnf