MySQL死鎖日志是什么意思呢?在MySQL中,死鎖是指兩個(gè)或多個(gè)事務(wù)在互相等待對(duì)方所持有的鎖資源時(shí),導(dǎo)致所有事務(wù)無(wú)法繼續(xù)執(zhí)行的情況。
當(dāng)發(fā)生死鎖的情況時(shí),MySQL會(huì)自動(dòng)記錄死鎖事件時(shí)的相關(guān)信息,包括死鎖發(fā)生的時(shí)間、涉及的事務(wù)ID和鎖信息等。這些信息被稱為死鎖日志。
死鎖日志對(duì)于診斷和解決死鎖問(wèn)題非常有用。通過(guò)分析死鎖日志,可以找到導(dǎo)致死鎖的原因,進(jìn)而對(duì)相關(guān)應(yīng)用程序作出調(diào)整,以避免類(lèi)似的死鎖事件再次發(fā)生。
------------------------ LATEST DETECTED DEADLOCK ------------------------ 2019-09-01 15:06:18 0x7f7280c1a700 *** (1) TRANSACTION: TRANSACTION 4496826212, ACTIVE 0 sec inserting mysql tables in use 1, locked 1 LOCK WAIT 4 lock struct(s), heap size 1184, 3 row lock(s), undo log entries 2 MySQL thread id 786597, OS thread handle 139760000173824, query id 132390812 localhost root update insert into test2 (id,cnt) values (1,1) *** (1) WAITING FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 126069 page no 3 n bits 72 index `PRIMARY` of table `test`.`test2` trx id 4496826212 lock_mode X locks rec but not gap waiting Record lock, heap no 3 PHYSICAL RECORD: n_fields 2; compact format; info bits 0 0: len 8; hex 73757072656d7570; asc supremup;; 1: len 4; hex 80000001; asc ;; *** (2) TRANSACTION: TRANSACTION 4496826761, ACTIVE 0 sec inserting mysql tables in use 1, locked 1 4 lock struct(s), heap size 1184, 3 row lock(s), undo log entries 2 MySQL thread id 786570, OS thread handle 139787712196864, query id 132390881 localhost root update insert into test2 (id,cnt) values(2,2) *** (2) HOLDS THE LOCK(S): RECORD LOCKS space id 126069 page no 3 n bits 72 index `PRIMARY` of table `test`.`test2` trx id 4496826761 lock mode S locks rec but not gap Record lock, heap no 3 PHYSICAL RECORD: n_fields 2; compact format; info bits 0 0: len 8; hex 73757072656d7570; asc supremup;; 1: len 4; hex 80000001; asc ;; *** (2) WAITING FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 126069 page no 3 n bits 72 index `PRIMARY` of table `test`.`test2` trx id 4496826761 lock_mode X locks rec but not gap waiting Record lock, heap no 3 PHYSICAL RECORD: n_fields 2; compact format; info bits 0 0: len 8; hex 73757072656d7570; asc supremup;; 1: len 4; hex 80000001; asc ;; *** WE ROLL BACK TRANSACTIONS (2)
上面是一個(gè)典型的死鎖日志的示例。通過(guò)分析該日志,可以看到有兩個(gè)事務(wù)(TRANSACTION 4496826212和TRANSACTION 4496826761)在同時(shí)插入數(shù)據(jù),并且都等待對(duì)方釋放鎖資源。由此導(dǎo)致死鎖的發(fā)生。最后MySQL強(qiáng)行終止了其中一個(gè)事務(wù),來(lái)解決死鎖問(wèn)題。
上一篇mysql死鎖排查