欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MySQL锁阻塞的深入分析

程序员文章站 2022-04-07 18:21:01
日常维护中,经常会碰到线程被阻塞,导致数据库响应非常慢,下面就看看如何获取是哪个线程导致了阻塞的。1. 环境说明rhel 6.4 x86_64 + mysql 5.6.19事务隔离级别:rr2. 测试...

日常维护中,经常会碰到线程被阻塞,导致数据库响应非常慢,下面就看看如何获取是哪个线程导致了阻塞的。

1. 环境说明

rhel 6.4 x86_64 + mysql 5.6.19

事务隔离级别:rr

2. 测试过程

MySQL锁阻塞的深入分析

3. 查看锁阻塞线程信息

这里用几中方法进行分析:

3.1  使用show processlist查看

mysql [(none)]> show processlist;
+----+------+-----------+------+---------+------+--------------+------------------------------------------+
| id | user | host  | db | command | time | state  | info          |
+----+------+-----------+------+---------+------+--------------+------------------------------------------+
| 2 | root | localhost | null | query | 0 | init   | show processlist       |
| 3 | root | localhost | test | query | 70 | sending data | select count(*) from t3 a,t3 b   |
| 4 | root | localhost | test | query | 65 | updating  | delete from emp where empno=7788   |
| 7 | root | localhost | test | query | 68 | updating  | update emp set sal=3500 where empno=7788 |
+----+------+-----------+------+---------+------+--------------+------------------------------------------+
4 rows in set (0.00 sec)

如果数据库存在较多线程的话,这种方法确实不太好确认的。

3.2  直接使用show engine innodb status查看

------------
transactions
------------
trx id counter 4131
purge done for trx's n:o < 4119 undo n:o < 0 state: running but idle
history list length 126
list of transactions for each session:
---transaction 0, not started
mysql thread id 2, os thread handle 0x7f953ffff700, query id 115 localhost root init
show engine innodb status
---transaction 4130, active 41 sec starting index read
mysql tables in use 1, locked 1
lock wait 2 lock struct(s), heap size 360, 1 row lock(s)
mysql thread id 4, os thread handle 0x7f953ff9d700, query id 112 localhost root updating
delete from emp where empno=7788
------- trx has been waiting 41 sec for this lock to be granted: ## 等待了41s
record locks space id 16 page no 3 n bits 88 index `primary` of table `test`.`emp` trx id 4130 lock_mode x locks rec but not gap waiting
record lock, heap no 9 physical record: n_fields 10; compact format; info bits 0 ## 线程4在等待往test.emp中的主键上加x锁,page num=3
 0: len 4; hex 80001e6c; asc l;;
 1: len 6; hex 000000001018; asc  ;;
 2: len 7; hex 91000001420084; asc  b ;;
 3: len 5; hex 53434f5454; asc scott;;
 4: len 7; hex 414e414c595354; asc analyst;;
 5: len 4; hex 80001d8e; asc  ;;
 6: len 4; hex 208794f0; asc  ;;
 7: len 4; hex 80000bb8; asc  ;;
 8: sql null;
 9: len 4; hex 80000014; asc  ;;
 
------------------
---transaction 4129, active 45 sec starting index read
mysql tables in use 1, locked 1
lock wait 2 lock struct(s), heap size 360, 1 row lock(s)
mysql thread id 7, os thread handle 0x7f953ff6c700, query id 111 localhost root updating
update emp set sal=3500 where empno=7788
------- trx has been waiting 45 sec for this lock to be granted: ## 等待了45s
record locks space id 16 page no 3 n bits 88 index `primary` of table `test`.`emp` trx id 4129 lock_mode x locks rec but not gap waiting
record lock, heap no 9 physical record: n_fields 10; compact format; info bits 0 ## 线程7在等待往test.emp中的主键上加x锁,page num=3
 0: len 4; hex 80001e6c; asc l;;
 1: len 6; hex 000000001018; asc  ;;
 2: len 7; hex 91000001420084; asc  b ;;
 3: len 5; hex 53434f5454; asc scott;;
 4: len 7; hex 414e414c595354; asc analyst;;
 5: len 4; hex 80001d8e; asc  ;;
 6: len 4; hex 208794f0; asc  ;;
 7: len 4; hex 80000bb8; asc  ;;
 8: sql null;
 9: len 4; hex 80000014; asc  ;;
 
------------------
---transaction 4128, active 51 sec
2 lock struct(s), heap size 360, 1 row lock(s)
mysql thread id 3, os thread handle 0x7f953ffce700, query id 110 localhost root cleaning up

我们知道,主要根因还是thread=3引起的,但从innodb status中却无法分析得到这个结果。

从上面来看,线程4和线程7都在等待往test.emp中的主键上加x锁,page num=3,但是线程7等待的时间为45s,而线程4等待的时间为41s,是较线程7之后申请的锁,所以可以判断是线程7阻塞了线程4。至于线程7为什么出现等待,这里分析不到根因。

3.3  使用mysqladmin debug查看

# mysqladmin -s /tmp/mysql3306.sock debug

然后在error日志中,会看到:

thread database.table_name   locked/waiting  lock_type
 
 
3  test.t3      locked - read   low priority read lock
7  test.emp     locked - write  high priority write lock

这种方法中,能找到线程id=3和7是阻塞者,但还是不太准确,判断不出来线程7也是被线程id=3阻塞的。

3.4  使用innodb_lock_monitor来获取阻塞锁线程

mysql [test]> create table innodb_lock_monitor (a int) engine=innodb; ## 随便在一个数据库中创建这个表,就会打开lock monitor
query ok, 0 rows affected, 1 warning (0.07 sec)
 
mysql [test]> show warnings\g
*************************** 1. row ***************************
 level: warning
 code: 131
message: using the table name innodb_lock_monitor to enable diagnostic output is deprecated and may be removed in future releases. use information_schema or performance_schema tables or set global innodb_status_output=on.
1 row in set (0.00 sec)

说明:这个在5.6中有一个warning,但不影响使用。

然后再使用show engine innodb status查看:

------------
transactions
------------
trx id counter 4667
purge done for trx's n:o < 4659 undo n:o < 0 state: running but idle
history list length 138
list of transactions for each session:
---transaction 0, not started
mysql thread id 9, os thread handle 0x7f813c5f7700, query id 152 localhost root init
show engine innodb status
---transaction 4663, active 78 sec starting index read
mysql tables in use 1, locked 1
lock wait 2 lock struct(s), heap size 360, 1 row lock(s)
mysql thread id 4, os thread handle 0x7f813c628700, query id 149 localhost root updating
delete from emp where empno=7788
------- trx has been waiting 78 sec for this lock to be granted:  ## 等待了78s
record locks space id 16 page no 3 n bits 88 index `primary` of table `test`.`emp` trx id 4663 lock_mode x locks rec but not gap waiting
record lock, heap no 9 physical record: n_fields 10; compact format; info bits 0 ## 线程4在等待往test.emp中的主键上加x锁,page num=3
 0: len 4; hex 80001e6c; asc  l;;
 1: len 6; hex 000000001018; asc    ;;
 2: len 7; hex 91000001420084; asc   b ;;
 3: len 5; hex 53434f5454; asc scott;;
 4: len 7; hex 414e414c595354; asc analyst;;
 5: len 4; hex 80001d8e; asc   ;;
 6: len 4; hex 208794f0; asc   ;;
 7: len 4; hex 80000bb8; asc   ;;
 8: sql null;
 9: len 4; hex 80000014; asc   ;;
 
------------------
table lock table `test`.`emp` trx id 4663 lock mode ix  ## 在给主键行上加x锁之前,先要在表上加意向锁ix
record locks space id 16 page no 3 n bits 88 index `primary` of table `test`.`emp` trx id 4663 lock_mode x locks rec but not gap waiting
record lock, heap no 9 physical record: n_fields 10; compact format; info bits 0
 0: len 4; hex 80001e6c; asc  l;;
 1: len 6; hex 000000001018; asc    ;;
 2: len 7; hex 91000001420084; asc   b ;;
 3: len 5; hex 53434f5454; asc scott;;
 4: len 7; hex 414e414c595354; asc analyst;;
 5: len 4; hex 80001d8e; asc   ;;
 6: len 4; hex 208794f0; asc   ;;
 7: len 4; hex 80000bb8; asc   ;;
 8: sql null;
 9: len 4; hex 80000014; asc   ;;
 
---transaction 4662, active 81 sec starting index read
mysql tables in use 1, locked 1
lock wait 2 lock struct(s), heap size 360, 1 row lock(s)
mysql thread id 7, os thread handle 0x7f813c5c6700, query id 148 localhost root updating
update emp set sal=3500 where empno=7788
------- trx has been waiting 81 sec for this lock to be granted: ## 等待了81s
record locks space id 16 page no 3 n bits 88 index `primary` of table `test`.`emp` trx id 4662 lock_mode x locks rec but not gap waiting
record lock, heap no 9 physical record: n_fields 10; compact format; info bits 0 ## 线程7在等待往test.emp中的主键上加x锁,page num=3
 0: len 4; hex 80001e6c; asc  l;;
 1: len 6; hex 000000001018; asc    ;;
 2: len 7; hex 91000001420084; asc   b ;;
 3: len 5; hex 53434f5454; asc scott;;
 4: len 7; hex 414e414c595354; asc analyst;;
 5: len 4; hex 80001d8e; asc   ;;
 6: len 4; hex 208794f0; asc   ;;
 7: len 4; hex 80000bb8; asc   ;;
 8: sql null;
 9: len 4; hex 80000014; asc   ;;
 
------------------
table lock table `test`.`emp` trx id 4662 lock mode ix  ## 在给主键行上加x锁之前,先要在表上加意向锁ix
record locks space id 16 page no 3 n bits 88 index `primary` of table `test`.`emp` trx id 4662 lock_mode x locks rec but not gap waiting
record lock, heap no 9 physical record: n_fields 10; compact format; info bits 0
 0: len 4; hex 80001e6c; asc  l;;
 1: len 6; hex 000000001018; asc    ;;
 2: len 7; hex 91000001420084; asc   b ;;
 3: len 5; hex 53434f5454; asc scott;;
 4: len 7; hex 414e414c595354; asc analyst;;
 5: len 4; hex 80001d8e; asc   ;;
 6: len 4; hex 208794f0; asc   ;;
 7: len 4; hex 80000bb8; asc   ;;
 8: sql null;
 9: len 4; hex 80000014; asc   ;;
 
---transaction 4615, active 1579 sec, thread declared inside innodb 1222
mysql tables in use 2, locked 0
2 lock struct(s), heap size 360, 1 row lock(s)
mysql thread id 3, os thread handle 0x7f813c659700, query id 147 localhost root sending data
select count(*) from t3 a,t3 b  ## 这是线程3当前正在执行的sql
trx read view will not see trx with id >= 4662, sees < 4659
table lock table `test`.`emp` trx id 4615 lock mode ix ## 线程3中正在拥有表上的意向ix锁,并且有test.emp表上主键的行级x锁,page num=3
record locks space id 16 page no 3 n bits 88 index `primary` of table `test`.`emp` trx id 4615 lock_mode x locks rec but not gap
record lock, heap no 9 physical record: n_fields 10; compact format; info bits 0
 0: len 4; hex 80001e6c; asc  l;;
 1: len 6; hex 000000001018; asc    ;;
 2: len 7; hex 91000001420084; asc   b ;;
 3: len 5; hex 53434f5454; asc scott;;
 4: len 7; hex 414e414c595354; asc analyst;;
 5: len 4; hex 80001d8e; asc   ;;
 6: len 4; hex 208794f0; asc   ;;
 7: len 4; hex 80000bb8; asc   ;;
 8: sql null;
 9: len 4; hex 80000014; asc   ;;

为什么线程3当前执行的是一个select t3表操作,但却锁住了test.emp表上page num=3?

有可能是线程3之前对test.emp表的操作事务没有及时提交导致。

所以得出:线程3阻塞了线程7,而线程7又阻塞了线程4,所以根因就是线程3,让线程3尽快提交或是kill掉即可。

4. 结论

在分析innodb中锁阻塞时,几种方法的对比情况:

(1)使用show processlist查看不靠谱;

(2)直接使用show engine innodb status查看,无法判断到问题的根因;

(3)使用mysqladmin debug查看,能看到所有产生锁的线程,但无法判断哪个才是根因;

(4)开启innodb_lock_monitor后,再使用show engine innodb status查看,能够找到锁阻塞的根因。

原文链接:

到此这篇关于mysql锁阻塞深入分析的文章就介绍到这了,更多相关mysql锁阻塞内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关标签: mysql 锁阻塞