溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

mysql怎么防止死鎖

發(fā)布時(shí)間:2021-08-17 08:23:12 來源:億速云 閱讀:165 作者:chen 欄目:MySQL數(shù)據(jù)庫

這篇文章主要介紹“mysql怎么防止死鎖”,在日常操作中,相信很多人在mysql怎么防止死鎖問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”mysql怎么防止死鎖”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

    死鎖是事務(wù)繞不開的話題,mysql當(dāng)然也不例外,本文主要模擬一下mysql的死鎖,以及應(yīng)對措施。
    首先看一個(gè)參數(shù),默認(rèn)innodb_print_all_deadlocks參數(shù)是關(guān)閉。開啟后可以將死鎖記錄到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,改參數(shù)是全局參數(shù),可以動(dòng)態(tài)調(diào)整。
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)
會(huì)話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)

會(huì)話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

會(huì)話A
mysql> insert into test01 select 2,2;
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0
會(huì)話A在(2,2)上加了X的行鎖,會(huì)話B要?jiǎng)h除相同的數(shù)據(jù)行,那么也要在該行上加X的行鎖(lock_mode X locks rec but not gap),所以出現(xiàn)了等待(lock_mode X waiting)。后面會(huì)話A要插入一行(2,2),因?yàn)樽侄蝐2上有唯一索引,插入的時(shí)候要檢查duplicate key的檢查,這個(gè)過程需要申請S的鎖,而在得到這個(gè)鎖之前,它需要等會(huì)話B先得到會(huì)話A最開始執(zhí)行的X鎖。也就是說,會(huì)話B要等待會(huì)話A第一條語句釋放X鎖,會(huì)話A第二條語句又要等待會(huì)話B釋放X鎖,兩個(gè)會(huì)話之間形成了等待的閉合回路,形成了死鎖。出現(xiàn)死鎖后,mysql會(huì)選擇一個(gè)小事務(wù)進(jìn)行回滾,以解決死鎖。
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,因?yàn)閟how engine innodb status只顯示最近一次的死鎖信息。
2.保持事務(wù)小而短,并盡快提價(jià)
3.避免在一個(gè)事務(wù)里面修改幾張表,或者是同一張表修改不同的結(jié)果集
4.可以嘗試使用較低的隔離級(jí)別,比如RC。也可以使用鎖定讀
5.建立合適的索引
6.如果應(yīng)用允許的話,串行化事務(wù)

到此,關(guān)于“mysql怎么防止死鎖”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI