正常情况下,死锁发生时,权重最小的连接将被kill并回滚。但是为了找出语句来优化,启用可启用死锁将死锁信息记录下来。
#step 1:窗口一 mysql> start transaction; mysql> update aa set name='aaa' where id = 1; #step 2:窗口二 mysql> start transaction; mysql> update bb set name='bbb' where id = 1; #step 3:窗口一 mysql> update bb set name='bbb';
#step 4:窗口三 #是否自动提交 mysql> show variables like 'autocommit'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | autocommit |> #step 3:窗口一 (若第三步中锁请求太久,则出现锁超时而终止执行) mysql> update bb set name='bbb'; ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction #"窗口一" 锁请求超时前,执行第五步,使死锁产生,则该连接 "窗口二" 执行终止,"窗口一" 顺利执行 #step 5:窗口二 mysql> update aa set name='aa'; ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction
查看最近一个死锁情况
#查看最近一个死锁情况 mysql> SHOW ENGINE INNODB STATUS\G ; ............... ------------------------ LATEST DETECTED DEADLOCK ------------------------ 2018-03-12 11:01:06 7ffb4993a700 #发生时间 *** (1) TRANSACTION: #事务1 TRANSACTION 130718515, ACTIVE 19 sec starting index read mysql tables in use 1, locked 1 #正被访问的表 LOCK WAIT 4 lock struct(s), heap size 1184, 4 row lock(s), undo log entries 1 #影响行数 MySQL thread id 5, OS thread handle 0x7ffb498f8700, query id 205 localhost root updating #线程/连接host/用户 update bb set name='bb' #请求语句 *** (1) WAITING FOR THIS LOCK TO BE GRANTED: #等待以下资源 (锁定位置及锁模式) RECORD LOCKS space id 65 page no 3 n bits 72 index `GEN_CLUST_INDEX` of table `test`.`bb` trx id 130718515 lock_mode X waiting Record lock, heap no 5 PHYSICAL RECORD: n_fields 5; compact format; info bits 0 0: len 6; hex 000000000300; asc ;; 1: len 6; hex 000007ca9b34; asc 4;; 2: len 7; hex 1f000002092075; asc u;; 3: len 4; hex 80000001; asc ;; 4: len 2; hex 6262; asc bb;; *** (2) TRANSACTION: #事务2 TRANSACTION 130718516, ACTIVE 14 sec starting index read mysql tables in use 1, locked 1 4 lock struct(s), heap size 1184, 4 row lock(s), undo log entries 1 MySQL thread id 4, OS thread handle 0x7ffb4993a700, query id 206 localhost root updating update aa set name='aa' #请求语句 *** (2) HOLDS THE LOCK(S): #持有锁资源 RECORD LOCKS space id 65 page no 3 n bits 72 index `GEN_CLUST_INDEX` of table `test`.`bb` trx id 130718516 lock_mode X Record lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0 0: len 8; hex 73757072656d756d; asc supremum;; Record lock, heap no 3 PHYSICAL RECORD: n_fields 5; compact format; info bits 0 0: len 6; hex 000000000301; asc ;; 1: len 6; hex 000007ca9b17; asc ;; 2: len 7; hex 9000000144011e; asc D ;; 3: len 4; hex 80000002; asc ;; 4: len 2; hex 6262; asc bb;; Record lock, heap no 5 PHYSICAL RECORD: n_fields 5; compact format; info bits 0 0: len 6; hex 000000000300; asc ;; 1: len 6; hex 000007ca9b34; asc 4;; 2: len 7; hex 1f000002092075; asc u;; 3: len 4; hex 80000001; asc ;; 4: len 2; hex 6262; asc bb;; *** (2) WAITING FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 64 page no 3 n bits 80 index `GEN_CLUST_INDEX` of table `test`.`aa` trx id 130718516 lock_mode X waiting Record lock, heap no 7 PHYSICAL RECORD: n_fields 5; compact format; info bits 0 0: len 6; hex 000000000200; asc ;; 1: len 6; hex 000007ca9b33; asc 3;; 2: len 7; hex 1e000001d53057; asc 0W;; 3: len 4; hex 80000001; asc ;; 4: len 2; hex 6161; asc aa;; *** WE ROLL BACK TRANSACTION (2) ...............
#死锁记录只记录最近一个死锁信息,若要将每个死锁信息都保存到错误日志,启用以下参数: mysql> show variables like 'innodb_print_all_deadlocks'; +----------------------------+-------+ | Variable_name | Value | +----------------------------+-------+ | innodb_print_all_deadlocks | OFF | +----------------------------+-------+ #上面 【step 3:窗口一】若一直请求不到资源,默认50秒则出现锁等待超时。 mysql> show variables like 'innodb_lock_wait_timeout'; +--------------------------+-------+ | Variable_name | Value | +--------------------------+-------+ | innodb_lock_wait_timeout | 50 | +--------------------------+-------+ ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction #设置全局变量 锁等待超时为60秒(新的连接生效) #mysql> set session innodb_lock_wait_timeout=50; mysql> set global innodb_lock_wait_timeout=60; #上面测试中,当事务中的某个语句超时只回滚该语句,事务的完整性属于被破坏了。为了回滚这个事务,启用以下参数: mysql> show variables like 'innodb_rollback_on_timeout'; +----------------------------+-------+ | Variable_name | Value | +----------------------------+-------+ | innodb_rollback_on_timeout | OFF | +----------------------------+-------+MySQL死锁检查处理的正常方法
扫一扫手机访问
