一.msyql死锁
- 死锁是相互争用资源过程中,都等待彼此释放资源,产生的现象。
死锁场景模拟
- 当session1持有共享锁S,并请求排他锁X,同时session2持有排他锁,并请求共享锁S的时候会产生死锁。
1)模拟session1持有共享锁,session2持有排他锁
## 在session1中执行
start transaction;
Query OK, 0 rows affected (0.00 sec)
select * from aa where userid = 18 lock in share mode;
+----+--------+
| id | userid |
+----+--------+
| 7 | 18 |
+----+--------+
1 row in set (0.00 sec)
## 在session1未提交之前,在session2执行删除,请求排他锁
start transaction;
Query OK, 0 rows affected (0.00 sec)
## 执行删除的时候,delete会一直阻塞,直到超过innodb_lock_wait_timeout,返回错误信息ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
delete from aa where id=7;
## 为了实验观察,调整innodb_lock_wait_timeout
show variables like 'innodb_lock_wait_timeout';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| innodb_lock_wait_timeout | 3600 |
+--------------------------+-------+
1 row in set (0.00 sec)
2)观察执行线程执行情况
## session2一直在执行
select * from information_schema.processlist order by state desc;
+----+------+-----------+------+---------+------+-----------+------------------------------------------------------------------+
| ID | USER | HOST | DB | COMMAND | TIME | STATE | INFO |
+----+------+-----------+------+---------+------+-----------+------------------------------------------------------------------+
| 4 | root | localhost | test | Query | 49 | updating | delete from aa where id=7 |
| 5 | root | localhost | NULL | Query | 0 | executing | select * from information_schema.processlist order by state desc |
| 3 | root | localhost | test | Sleep | 331 | | NULL |
| 7 | root | localhost | NULL | Sleep | 991 | | NULL |
+----+------+-----------+------+---------+------+-----------+------------------------------------------------------------------+
4 rows in set (0.00 sec)
3)模拟session1再持有排他锁
## 在session1中再执行
delete from aa where id=7;
Query OK, 1 row affected (0.00 sec)
## session1执行成功的同时session2的delete操作返回死锁错误信息
ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction
4)检查死锁情况
show engine innodb status\G;
MySQL thread id 2, OS thread handle 140247796619008, query id 141 localhost root updating
delete from aa where id=7
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 27 page no 3 n bits 80 index PRIMARY of table `test`.`aa` trx id 3336 lock mode S
Record lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0
0: len 8; hex 73757072656d756d; asc supremum;;
*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 27 page no 3 n bits 80 index PRIMARY of table `test`.`aa` trx id 3336 lock_mode X locks rec but not gap waiting
Record lock, heap no 8 PHYSICAL RECORD: n_fields 4; compact format; info bits 0
0: len 4; hex 80000007; asc ;;
1: len 6; hex 000000000b04; asc ;;
2: len 7; hex a4000001180128; asc (;;
3: len 4; hex 80000012; asc ;;
5)查看历史执行情况
select * from performance_schema.events_statements_history;
二.mysql等待锁
- mysql8.0已经废弃掉innodb_lock_waits表
- state状态是Waiting for table metadata lock,事务A等待事务B释放资源
## session1中执行
set autocommit=0;
Query OK, 0 rows affected (0.00 sec)
select * from aa where id >6;
+----+--------+
| id | userid |
+----+--------+
| 7 | 18 |
+----+--------+
1 row in set (0.00 sec)
## session2中执行修改表,会一直等待session1中事务提交
alter table aa add column addr varchar(100) not null default '';
## session3中查看线程信息,可以看到状态是Waiting
select * from information_schema.processlist order by state desc;
+----+------+-----------+------+---------+------+---------------------------------+------------------------------------------------------------------+
| ID | USER | HOST | DB | COMMAND | TIME | STATE | INFO |
+----+------+-----------+------+---------+------+---------------------------------+------------------------------------------------------------------+
| 3 | root | localhost | test | Query | 236 | Waiting for table metadata lock | alter table aa add column addr varchar(100) not null default '' |
| 4 | root | localhost | NULL | Query | 0 | executing | select * from information_schema.processlist order by state desc |
| 2 | root | localhost | test | Sleep | 130 | | NULL |
| 5 | root | localhost | NULL | Sleep | 4597 | | NULL |
+----+------+-----------+------+---------+------+---------------------------------+------------------------------------------------------------------+