mysql死鎖(dead lock)與鎖等待(lock wait)的出現(xiàn)解決
很多人都分不清死鎖和鎖等待的區(qū)別,也有不同IT口的人叫法的差異。在運(yùn)維側(cè):
死鎖最明顯的特征是會(huì)自動(dòng)解開(kāi),是需要我們?nèi)ナ潞蠼鉀Q邏輯缺陷。
鎖等待則是業(yè)務(wù)卡住了(一般是某個(gè)大事務(wù)還在執(zhí)行,或有事務(wù)沒(méi)提交),需要?dú)⒌舫钟墟i的進(jìn)城讓業(yè)務(wù)正常進(jìn)行
做幾個(gè)實(shí)驗(yàn)詳細(xì)演示一下。
表結(jié)構(gòu)及數(shù)據(jù)情況
mysql> desc ttt; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | x | int(11) | NO | PRI | NULL | | | y | datetime | YES | | NULL | | | z | varchar(10) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 3 rows in set (0.00 sec) mysql> select * from ttt; +---+---------------------+------+ | x | y | z | +---+---------------------+------+ | 1 | 2023-05-30 23:50:13 | 123 | | 2 | 2023-04-26 17:58:18 | av3 | | 3 | 2023-05-30 22:52:35 | at | | 4 | 2023-04-26 17:58:29 | attt | | 5 | 2023-05-30 22:52:55 | zxz | +---+---------------------+------+ 5 rows in set (0.00 sec)
死鎖(dead lock)
上文說(shuō)了,死鎖會(huì)自動(dòng)解除,這里主要展示一下怎么追查。這里就不演示具體執(zhí)行順序了
前臺(tái)報(bào)錯(cuò)
session1,這個(gè)先持有
mysql> begin; Query OK, 0 rows affected (0.00 sec) mysql> update ttt set y=now() where x=1; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> update ttt set y=now() where x=3; Query OK, 1 row affected (7.04 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql>
session2,這個(gè)來(lái)跳出錯(cuò)誤
mysql> begin; Query OK, 0 rows affected (0.00 sec) mysql> update ttt set y=now() where x=3; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> update ttt set y=now() where x=1; ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction mysql>
事后追查
innodb status的last dead lock只會(huì)記錄上一次,建議是使用innodb_print_all_deadlocks參數(shù),將記錄打印到error.log中,該參數(shù)默認(rèn)為off,可以動(dòng)態(tài)修改
mysql> show variables like '%innodb_print_all_deadlocks%'; +----------------------------+-------+ | Variable_name | Value | +----------------------------+-------+ | innodb_print_all_deadlocks | OFF | +----------------------------+-------+ 1 row in set (0.00 sec) mysql> set global innodb_print_all_deadlocks=on; Query OK, 0 rows affected (0.00 sec) mysql> show variables like '%innodb_print_all_deadlocks%'; +----------------------------+-------+ | Variable_name | Value | +----------------------------+-------+ | innodb_print_all_deadlocks | ON | +----------------------------+-------+ 1 row in set (0.00 sec)
innodb status
show engine innodb status; ********* ------------------------ LATEST DETECTED DEADLOCK ------------------------ 2023-05-30 22:42:09 0x7f9fd41ba700 *** (1) TRANSACTION: TRANSACTION 5454, ACTIVE 26 sec starting index read mysql tables in use 1, locked 1 LOCK WAIT 3 lock struct(s), heap size 1136, 2 row lock(s), undo log entries 1 MySQL thread id 18, OS thread handle 140324434298624, query id 440 localhost root updating update ttt set y=now() where x=3 *** (1) WAITING FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 23 page no 3 n bits 80 index PRIMARY of table `ddd`.`ttt` trx id 5454 lock_mode X locks rec but not gap waiting Record lock, heap no 10 PHYSICAL RECORD: n_fields 5; compact format; info bits 0 0: len 4; hex 80000003; asc ;; 1: len 6; hex 00000000154f; asc O;; 2: len 7; hex 390000014e0110; asc 9 N ;; 3: len 5; hex 99b03d6a76; asc =jv;; 4: len 2; hex 6174; asc at;; *** (2) TRANSACTION: TRANSACTION 5455, ACTIVE 15 sec starting index read mysql tables in use 1, locked 1 3 lock struct(s), heap size 1136, 2 row lock(s), undo log entries 1 MySQL thread id 20, OS thread handle 140324435109632, query id 441 localhost root updating update ttt set y=now() where x=1 *** (2) HOLDS THE LOCK(S): RECORD LOCKS space id 23 page no 3 n bits 80 index PRIMARY of table `ddd`.`ttt` trx id 5455 lock_mode X locks rec but not gap Record lock, heap no 10 PHYSICAL RECORD: n_fields 5; compact format; info bits 0 0: len 4; hex 80000003; asc ;; 1: len 6; hex 00000000154f; asc O;; 2: len 7; hex 390000014e0110; asc 9 N ;; 3: len 5; hex 99b03d6a76; asc =jv;; 4: len 2; hex 6174; asc at;; *** (2) WAITING FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 23 page no 3 n bits 80 index PRIMARY of table `ddd`.`ttt` trx id 5455 lock_mode X locks rec but not gap waiting Record lock, heap no 7 PHYSICAL RECORD: n_fields 5; compact format; info bits 0 0: len 4; hex 80000001; asc ;; 1: len 6; hex 00000000154e; asc N;; 2: len 7; hex 380000014d0110; asc 8 M ;; 3: len 5; hex 99b03d6a6b; asc =jk;; 4: len 3; hex 313233; asc 123;; *** WE ROLL BACK TRANSACTION (2) ********
error.log
[root@mysql57-1 data]# tail -100f mysql57-1.err 2023-05-25T07:42:37.677851Z 14 [Note] Access denied for user 'root'@'localhost' (using password: YES) 2023-05-25T07:44:15.360390Z 15 [Note] Access denied for user 'roo'@'localhost' (using password: YES) 2023-05-25T07:44:28.477560Z 16 [Note] Access denied for user 'roo'@'localhost' (using password: YES) 2023-05-30T14:53:00.101403Z 22 [Note] InnoDB: Transactions deadlock detected, dumping detailed information. 2023-05-30T14:53:00.101475Z 22 [Note] InnoDB: *** (1) TRANSACTION: TRANSACTION 5458, ACTIVE 25 sec starting index read mysql tables in use 1, locked 1 LOCK WAIT 3 lock struct(s), heap size 1136, 2 row lock(s), undo log entries 1 MySQL thread id 21, OS thread handle 140324434298624, query id 473 localhost root updating update ttt set y=now() where x=5 2023-05-30T14:53:00.101516Z 22 [Note] InnoDB: *** (1) WAITING FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 23 page no 3 n bits 80 index PRIMARY of table `ddd`.`ttt` trx id 5458 lock_mode X locks rec but not gap waiting Record lock, heap no 6 PHYSICAL RECORD: n_fields 5; compact format; info bits 0 0: len 4; hex 80000005; asc ;; 1: len 6; hex 000000001553; asc S;; 2: len 7; hex 3b00000130036d; asc ; 0 m;; 3: len 5; hex 99b03d6d32; asc =m2;; 4: len 3; hex 7a787a; asc zxz;; 2023-05-30T14:53:00.101718Z 22 [Note] InnoDB: *** (2) TRANSACTION: TRANSACTION 5459, ACTIVE 10 sec starting index read mysql tables in use 1, locked 1 3 lock struct(s), heap size 1136, 2 row lock(s), undo log entries 1 MySQL thread id 22, OS thread handle 140324435109632, query id 474 localhost root updating update ttt set y=now() where x=3 2023-05-30T14:53:00.101748Z 22 [Note] InnoDB: *** (2) HOLDS THE LOCK(S): RECORD LOCKS space id 23 page no 3 n bits 80 index PRIMARY of table `ddd`.`ttt` trx id 5459 lock_mode X locks rec but not gap Record lock, heap no 6 PHYSICAL RECORD: n_fields 5; compact format; info bits 0 0: len 4; hex 80000005; asc ;; 1: len 6; hex 000000001553; asc S;; 2: len 7; hex 3b00000130036d; asc ; 0 m;; 3: len 5; hex 99b03d6d32; asc =m2;; 4: len 3; hex 7a787a; asc zxz;; 2023-05-30T14:53:00.101912Z 22 [Note] InnoDB: *** (2) WAITING FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 23 page no 3 n bits 80 index PRIMARY of table `ddd`.`ttt` trx id 5459 lock_mode X locks rec but not gap waiting Record lock, heap no 10 PHYSICAL RECORD: n_fields 5; compact format; info bits 0 0: len 4; hex 80000003; asc ;; 1: len 6; hex 000000001552; asc R;; 2: len 7; hex 3a0000012e03d1; asc : . ;; 3: len 5; hex 99b03d6d23; asc =m#;; 4: len 2; hex 6174; asc at;; 2023-05-30T14:53:00.102084Z 22 [Note] InnoDB: *** WE ROLL BACK TRANSACTION (2)
鎖等待(lock wait)
1個(gè)參數(shù)
innodb_lock_wait_timeout
行鎖等待的時(shí)間,如果超過(guò)這個(gè)時(shí)間,session2(后發(fā)起那個(gè))會(huì)自動(dòng)跳出
session1
mysql> begin; Query OK, 0 rows affected (0.00 sec) mysql> update ttt set y=now() where x=1; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0
session2
mysql> update ttt set y=now() where x=1; ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
怎么處理
省略構(gòu)造鎖
通過(guò)information_schema.innodb_lock_waits視圖獲得鎖等待的關(guān)系
mysql> select * from information_schema.innodb_lock_waits; +-------------------+-------------------+-----------------+------------------+ | requesting_trx_id | requested_lock_id | blocking_trx_id | blocking_lock_id | +-------------------+-------------------+-----------------+------------------+ | 5467 | 5467:23:3:7 | 5466 | 5466:23:3:7 | +-------------------+-------------------+-----------------+------------------+ 1 row in set, 1 warning (0.00 sec)
再結(jié)合information_schema.innodb_trx視圖得到語(yǔ)句和線(xiàn)程ID
這里提供一個(gè)sql
SELECT r.trx_id waiting_trx_id,
r.trx_mysql_thread_id waiting_thread,
r.trx_query waiting_query,
b.trx_id blocking_trx_id,
b.trx_mysql_thread_id blocking_thread,
b.trx_query blocking_query,
b.trx_autocommit_non_locking
FROM information_schema.innodb_lock_waits w
INNER JOIN information_schema.innodb_trx b ON
b.trx_id = w.blocking_trx_id
INNER JOIN information_schema.innodb_trx r ON
r.trx_id = w.requesting_trx_id;
mysql> SELECT r.trx_id waiting_trx_id,
-> r.trx_mysql_thread_id waiting_thread,
-> r.trx_query waiting_query,
-> b.trx_id blocking_trx_id,
-> b.trx_mysql_thread_id blocking_thread,
-> b.trx_query blocking_query,
-> b.trx_autocommit_non_locking
-> FROM information_schema.innodb_lock_waits w
-> INNER JOIN information_schema.innodb_trx b ON
-> b.trx_id = w.blocking_trx_id
-> INNER JOIN information_schema.innodb_trx r ON
-> r.trx_id = w.requesting_trx_id;
+----------------+----------------+----------------------------------+-----------------+-----------------+----------------+----------------------------+
| waiting_trx_id | waiting_thread | waiting_query | blocking_trx_id | blocking_thread | blocking_query | trx_autocommit_non_locking |
+----------------+----------------+----------------------------------+-----------------+-----------------+----------------+----------------------------+
| 5470 | 22 | update ttt set y=now() where x=1 | 5466 | 26 | NULL | 0 |
+----------------+----------------+----------------------------------+-----------------+-----------------+----------------+----------------------------+
1 row in set, 1 warning (0.00 sec)
再對(duì)blocking thread的狀態(tài)進(jìn)行確認(rèn),然后kill
mysql> select * from information_schema.PROCESSLIST where id=26; +----+------+-----------+------+---------+------+-------+------+ | ID | USER | HOST | DB | COMMAND | TIME | STATE | INFO | +----+------+-----------+------+---------+------+-------+------+ | 26 | root | localhost | ddd | Sleep | 1221 | | NULL | +----+------+-----------+------+---------+------+-------+------+ 1 row in set (0.00 sec)
殺線(xiàn)程
mysql> kill 26; Query OK, 0 rows affected (0.00 sec)
級(jí)聯(lián)鎖或大量鎖
這里構(gòu)造一個(gè)多個(gè)爭(zhēng)用的情況。
session1,線(xiàn)程id為27,這個(gè)先發(fā)起
mysql> begin; Query OK, 0 rows affected (0.00 sec) mysql> update ttt set y=now() where x=1; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0
session,線(xiàn)程id為22,第二個(gè)發(fā)起
mysql> update ttt set y=now() where x=1;
session3,線(xiàn)程id為28,最后一個(gè)發(fā)起
mysql> update ttt set y=now();
鎖關(guān)系情況
mysql> select * from information_schema.innodb_lock_waits; +-------------------+-------------------+-----------------+------------------+ | requesting_trx_id | requested_lock_id | blocking_trx_id | blocking_lock_id | +-------------------+-------------------+-----------------+------------------+ | 5479 | 5479:23:3:7 | 5478 | 5478:23:3:7 | | 5479 | 5479:23:3:7 | 5473 | 5473:23:3:7 | | 5478 | 5478:23:3:7 | 5473 | 5473:23:3:7 | +-------------------+-------------------+-----------------+------------------+ 3 rows in set, 1 warning (0.01 sec)
查詢(xún)sql的展示情況
mysql> SELECT r.trx_id waiting_trx_id, r.trx_mysql_thread_id waiting_thread, r.trx_query waiting_query, b.trx_id blocking_trx_id, b.trx_mysql_thread_id blocking_thread, b.trx_query blocking_query, b.trx_autocommit_non_locking FROM information_schema.innodb_lock_waits w INNER JOIN information_schema.innodb_trx b ON b.trx_id = w.blocking_trx_id INNER JOIN information_schema.innodb_trx r ON r.trx_id = w.requesting_trx_id; +----------------+----------------+----------------------------------+-----------------+-----------------+----------------------------------+----------------------------+ | waiting_trx_id | waiting_thread | waiting_query | blocking_trx_id | blocking_thread | blocking_query | trx_autocommit_non_locking | +----------------+----------------+----------------------------------+-----------------+-----------------+----------------------------------+----------------------------+ | 5479 | 28 | update ttt set y=now() | 5478 | 22 | update ttt set y=now() where x=1 | 0 | | 5479 | 28 | update ttt set y=now() | 5473 | 27 | NULL | 0 | | 5478 | 22 | update ttt set y=now() where x=1 | 5473 | 27 | NULL | 0 | +----------------+----------------+----------------------------------+-----------------+-----------------+----------------------------------+----------------------------+
這個(gè)時(shí)候一般的做法是先找到最多的那個(gè)blocking_thread
確認(rèn)他的進(jìn)程狀態(tài)后來(lái)考慮是否殺
如果他是活動(dòng)的thread,在干活,就可以刪掉釋放資源。
如果他是非活動(dòng),就要看他這個(gè)thread的blocking_thread,找到活動(dòng)持有資源的,來(lái)殺掉。
當(dāng)然也有執(zhí)行了完了不提交的情況,innodb_trx的trx_autocommit_non_locking列的值就是為了標(biāo)記是否是提交了。0表示沒(méi)有提交。
到此這篇關(guān)于mysql死鎖(dead lock)與鎖等待(lock wait)的出現(xiàn)解決的文章就介紹到這了,更多相關(guān)mysql死鎖與鎖等待內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mysql 數(shù)據(jù)庫(kù)中設(shè)備實(shí)時(shí)狀態(tài)表水平分表
在使用 Mysql 數(shù)據(jù)庫(kù)存儲(chǔ)設(shè)備上報(bào)日志時(shí),存在一張?jiān)O(shè)備實(shí)時(shí)狀態(tài)表,隨著時(shí)間推移,數(shù)據(jù)量變得十分龐大,為了更好地管理和查詢(xún)數(shù)據(jù),提高數(shù)據(jù)庫(kù)性能,需要對(duì)該表進(jìn)行水平分表操作,下面就來(lái)介紹一下如何實(shí)現(xiàn)2025-06-06
Navicat自動(dòng)備份MySQL數(shù)據(jù)的流程步驟
對(duì)于從事IT開(kāi)發(fā)的工程師,數(shù)據(jù)備份我想大家并不陌生,這件工程太重要了!對(duì)于比較重要的數(shù)據(jù),我們希望能定期備份,每天備份1次或多次,或者是每周備份1次或多次,所以本文給大家介紹了Navicat自動(dòng)備份MySQL數(shù)據(jù)的流程步驟,需要的朋友可以參考下2024-12-12
MySQL配置文件my.cnf中文詳解附mysql性能優(yōu)化方法分享
Mysql參數(shù)優(yōu)化對(duì)于新手來(lái)講,是比較難懂的東西,其實(shí)這個(gè)參數(shù)優(yōu)化,是個(gè)很復(fù)雜的東西,對(duì)于不同的網(wǎng)站,及其在線(xiàn)量,訪(fǎng)問(wèn)量,帖子數(shù)量,網(wǎng)絡(luò)情況,以及機(jī)器硬件配置都有關(guān)系,優(yōu)化不可能一次性完成,需要不斷的觀察以及調(diào)試,才有可能得到最佳效果。2011-09-09
Windows XP系統(tǒng)安裝MySQL5.5.28圖解教程
很多朋友在winxp系統(tǒng)中開(kāi)發(fā)php等,需要安裝mysql數(shù)據(jù)庫(kù),這里簡(jiǎn)單介紹下,如何在xp下安裝mysql軟件,其實(shí)跟其它系統(tǒng)都差不多,主要是軟件對(duì)系統(tǒng)的兼容性2013-05-05
Mysql數(shù)據(jù)庫(kù)手動(dòng)及定時(shí)備份步驟
最近剛好用到了數(shù)據(jù)庫(kù)備份,想著還有個(gè)別實(shí)習(xí)或者剛工作的小伙伴一個(gè)drop不小心刪表、刪庫(kù),心內(nèi)慌得一批不知道該怎么辦,就打算跑路了,學(xué)會(huì)這個(gè)小技巧就不用跑路了2021-11-11
MySQL中的索引結(jié)構(gòu)和分類(lèi)實(shí)戰(zhàn)案例詳解
本文詳解MySQL索引結(jié)構(gòu)與分類(lèi),涵蓋B樹(shù)、B+樹(shù)、哈希及全文索引,分析其原理與優(yōu)劣勢(shì),并結(jié)合實(shí)戰(zhàn)案例探討創(chuàng)建、管理及優(yōu)化技巧,助力提升查詢(xún)性能,感興趣的朋友跟隨小編一起看看吧2025-06-06
安裝mysql時(shí)出現(xiàn)initializing database、starting service錯(cuò)誤
文章瀏覽閱讀1.2w次,點(diǎn)贊17次,收藏85次。文章記錄了重裝MySQL時(shí)遇到的initializingdatabase錯(cuò)誤和startingservice問(wèn)題的解決過(guò)程。首先,使用MySQLInstaller-Community徹底卸載并清理殘留,然后檢查并更改服務(wù)登錄賬戶(hù)為本地系統(tǒng)賬戶(hù),從而成功解決問(wèn)題。2026-04-04
SQL查詢(xún)語(yǔ)句執(zhí)行的過(guò)程
這篇文章主要介紹了SQL查詢(xún)語(yǔ)句執(zhí)行的過(guò)程,文章圍繞主題展開(kāi)SQL查詢(xún)語(yǔ)句的相關(guān)資料,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-05-05

