您好,登錄后才能下訂單哦!
這篇文章主要介紹“mysql怎么防止死鎖”,在日常操作中,相信很多人在mysql怎么防止死鎖問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”mysql怎么防止死鎖”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
死鎖是事務繞不開的話題,mysql當然也不例外,本文主要模擬一下mysql的死鎖,以及應對措施。
首先看一個參數,默認innodb_print_all_deadlocks參數是關閉。開啟后可以將死鎖記錄到error.log中。否則只能通過show engine innodb status查看。
mysql> SHOW VARIABLES LIKE 'INNODB_PRINT_ALL_DEADLOCKS';
+----------------------------+-------+
| Variable_name | Value |
+----------------------------+-------+
| innodb_print_all_deadlocks | OFF |
+----------------------------+-------+
1 row in set (0.00 sec)
開啟innodb_print_all_deadlocks,改參數是全局參數,可以動態調整。
mysql> SET GLOBAL innodb_print_all_deadlocks=1;
Query OK, 0 rows affected (0.00 sec)
表test01上c1是主鍵,c2是唯一約束。
mysql> show create table test01\G
*************************** 1. row ***************************
Table: test01
Create Table: CREATE TABLE `test01` (
`c1` bigint(20) NOT NULL AUTO_INCREMENT,
`c2` int(11) DEFAULT NULL,
PRIMARY KEY (`c1`),
UNIQUE KEY `uidx_test01_c2` (`c2`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
mysql> select * from test01;
+----+------+
| c1 | c2 |
+----+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
+----+------+
3 rows in set (0.00 sec)
會話A
mysql> begin ;
Query OK, 0 rows affected (0.00 sec)
mysql> delete from test01 where c2=2;
Query OK, 1 row affected (0.00 sec)
會話B
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> delete from test01 where c2=2;
ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction
會話A
mysql> insert into test01 select 2,2;
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
會話A在(2,2)上加了X的行鎖,會話B要刪除相同的數據行,那么也要在該行上加X的行鎖(lock_mode X locks rec but not gap),所以出現了等待(lock_mode X waiting)。后面會話A要插入一行(2,2),因為字段c2上有唯一索引,插入的時候要檢查duplicate key的檢查,這個過程需要申請S的鎖,而在得到這個鎖之前,它需要等會話B先得到會話A最開始執行的X鎖。也就是說,會話B要等待會話A第一條語句釋放X鎖,會話A第二條語句又要等待會話B釋放X鎖,兩個會話之間形成了等待的閉合回路,形成了死鎖。出現死鎖后,mysql會選擇一個小事務進行回滾,以解決死鎖。
show engine innodb status查看死鎖信息,error.log中記錄的死鎖也類似下面:
------------------------
LATEST DETECTED DEADLOCK
------------------------
2019-07-18 11:11:32 0x7fdc50298700
*** (1) TRANSACTION:
TRANSACTION 713521, ACTIVE 122 sec starting index read
mysql tables in use 1, locked 1
LOCK WAIT 2 lock struct(s), heap size 1136, 2 row lock(s)
MySQL thread id 4, OS thread handle 140584214165248, query id 144 localhost root updating
delete from test01 where c2=2
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 25 page no 4 n bits 72 index uidx_test01_c2 of table `ming`.`test01` trx id 713521 lock_mode X waiting
Record lock, heap no 3 PHYSICAL RECORD: n_fields 2; compact format; info bits 32
0: len 4; hex 80000002; asc ;;
1: len 8; hex 8000000000000002; asc ;;
*** (2) TRANSACTION:
TRANSACTION 713523, ACTIVE 16 sec inserting
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 3, OS thread handle 140584214431488, query id 146 localhost root executing
insert into test01 select 2,2
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 25 page no 4 n bits 72 index uidx_test01_c2 of table `ming`.`test01` trx id 713523 lock_mode X locks rec but not gap
Record lock, heap no 3 PHYSICAL RECORD: n_fields 2; compact format; info bits 32
0: len 4; hex 80000002; asc ;;
1: len 8; hex 8000000000000002; asc ;;
*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 25 page no 4 n bits 72 index uidx_test01_c2 of table `ming`.`test01` trx id 713523 lock mode S waiting
Record lock, heap no 3 PHYSICAL RECORD: n_fields 2; compact format; info bits 32
0: len 4; hex 80000002; asc ;;
1: len 8; hex 8000000000000002; asc ;;
針對死鎖的舉措:
1.如果死鎖較多的話,那么建議開啟innodb_print_all_deadlocks,因為show engine innodb status只顯示最近一次的死鎖信息。
2.保持事務小而短,并盡快提價
3.避免在一個事務里面修改幾張表,或者是同一張表修改不同的結果集
4.可以嘗試使用較低的隔離級別,比如RC。也可以使用鎖定讀
5.建立合適的索引
6.如果應用允許的話,串行化事務
到此,關于“mysql怎么防止死鎖”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。