DDR爱好者之家 Design By 杰米

1、查询进程

show processlist

mysql查看死锁与去除死锁示例详解

2、 查询到相对应的进程,然后 kill id

mysql查看死锁与去除死锁示例详解

验证(kill后再看是否还有锁)

mysql查看死锁与去除死锁示例详解

2、查询是否锁表

show OPEN TABLES where In_use > 0;

示例:

新建一个会话执行如下的显示锁示例

LOCK TABLES account_data.account READ;
SELECT SLEEP(160);
UNLOCK TABLES account_data.account;

另开启一个会话检查锁表情况:

mysql> show OPEN TABLES where In_use > 0;
+--------------+---------+--------+-------------+
| Database  | Table | In_use | Name_locked |
+--------------+---------+--------+-------------+
| account_data | account |  1 |   0 |
+--------------+---------+--------+-------------+
1 row in set (0.00 sec)

mysql> select * from information_schema.innodb_locks\G;
Empty set, 1 warning (0.00 sec)

ERROR: 
No query specified

mysql> show processlist\G;
*************************** 1. row ***************************
  Id: 5
 User: root
 Host: 192.168.0.206:64294
  db: NULL
Command: Sleep
 Time: 4051
 State: 
 Info: NULL
*************************** 2. row ***************************
  Id: 8
 User: root
 Host: 192.168.0.206:64297
  db: NULL
Command: Sleep
 Time: 4042
 State: 
 Info: NULL
*************************** 3. row ***************************
  Id: 10
 User: root
 Host: localhost
  db: NULL
Command: Query
 Time: 0
 State: starting
 Info: show processlist
*************************** 4. row ***************************
  Id: 19
 User: root
 Host: 192.168.0.206:54603
  db: account_data
Command: Sleep
 Time: 245
 State: 
 Info: NULL
*************************** 5. row ***************************
  Id: 20
 User: root
 Host: 192.168.0.206:54604
  db: information_schema
Command: Query
 Time: 20
 State: User sleep
 Info: select sleep(160)
5 rows in set (0.00 sec)

ERROR: 
No query specified

mysql>

3、在5.5中,information_schema 库中增加了三个关于锁的表(innoDB引擎):

innodb_trx ## 当前运行的所有事务

innodb_locks ## 当前出现的锁

innodb_lock_waits ## 锁等待的对应关系

先来看一下这三张表结构:

root@127.0.0.1 : information_schema 13:28:38> desc innodb_locks;
+————-+———————+——+—–+———+——-+
| Field  | Type    | Null | Key | Default | Extra |
+————-+———————+——+—–+———+——-+
| lock_id  | varchar(81)   | NO |  |   |  |#锁ID
| lock_trx_id | varchar(18)   | NO |  |   |  |#拥有锁的事务ID
| lock_mode | varchar(32)   | NO |  |   |  |#锁模式
| lock_type | varchar(32)   | NO |  |   |  |#锁类型
| lock_table | varchar(1024)  | NO |  |   |  |#被锁的表
| lock_index | varchar(1024)  | YES |  | NULL |  |#被锁的索引
| lock_space | bigint(21) unsigned | YES |  | NULL |  |#被锁的表空间号
| lock_page | bigint(21) unsigned | YES |  | NULL |  |#被锁的页号
| lock_rec | bigint(21) unsigned | YES |  | NULL |  |#被锁的记录号
| lock_data | varchar(8192)  | YES |  | NULL |  |#被锁的数据
+————-+———————+——+—–+———+——-+
10 rows in set (0.00 sec)
 
root@127.0.0.1 : information_schema 13:28:56> desc innodb_lock_waits;
+——————-+————-+——+—–+———+——-+
| Field    | Type  | Null | Key | Default | Extra |
+——————-+————-+——+—–+———+——-+
| requesting_trx_id | varchar(18) | NO |  |   |  |#请求锁的事务ID
| requested_lock_id | varchar(81) | NO |  |   |  |#请求锁的锁ID
| blocking_trx_id | varchar(18) | NO |  |   |  |#当前拥有锁的事务ID
| blocking_lock_id | varchar(81) | NO |  |   |  |#当前拥有锁的锁ID
+——————-+————-+——+—–+———+——-+
4 rows in set (0.00 sec)
 
root@127.0.0.1 : information_schema 13:29:05> desc innodb_trx ;
+—————————-+———————+——+—–+———————+——-+
| Field      | Type    | Null | Key | Default    | Extra |
+—————————-+———————+——+—–+———————+——-+
| trx_id      | varchar(18)   | NO |  |      |  |#事务ID
| trx_state     | varchar(13)   | NO |  |      |  |#事务状态:
| trx_started    | datetime   | NO |  | 0000-00-00 00:00:00 |  |#事务开始时间;
| trx_requested_lock_id  | varchar(81)   | YES |  | NULL    |  |#innodb_locks.lock_id
| trx_wait_started   | datetime   | YES |  | NULL    |  |#事务开始等待的时间
| trx_weight     | bigint(21) unsigned | NO |  | 0     |  |#
| trx_mysql_thread_id  | bigint(21) unsigned | NO |  | 0     |  |#事务线程ID
| trx_query     | varchar(1024)  | YES |  | NULL    |  |#具体SQL语句
| trx_operation_state  | varchar(64)   | YES |  | NULL    |  |#事务当前操作状态
| trx_tables_in_use   | bigint(21) unsigned | NO |  | 0     |  |#事务中有多少个表被使用
| trx_tables_locked   | bigint(21) unsigned | NO |  | 0     |  |#事务拥有多少个锁
| trx_lock_structs   | bigint(21) unsigned | NO |  | 0     |  |#
| trx_lock_memory_bytes  | bigint(21) unsigned | NO |  | 0     |  |#事务锁住的内存大小(B)
| trx_rows_locked   | bigint(21) unsigned | NO |  | 0     |  |#事务锁住的行数
| trx_rows_modified   | bigint(21) unsigned | NO |  | 0     |  |#事务更改的行数
| trx_concurrency_tickets | bigint(21) unsigned | NO |  | 0     |  |#事务并发票数
| trx_isolation_level  | varchar(16)   | NO |  |      |  |#事务隔离级别
| trx_unique_checks   | int(1)    | NO |  | 0     |  |#是否唯一性检查
| trx_foreign_key_checks  | int(1)    | NO |  | 0     |  |#是否外键检查
| trx_last_foreign_key_error | varchar(256)  | YES |  | NULL    |  |#最后的外键错误
| trx_adaptive_hash_latched | int(1)    | NO |  | 0     |  |#
| trx_adaptive_hash_timeout | bigint(21) unsigned | NO |  | 0     |  |#
+—————————-+———————+——+—–+———————+——-+
22 rows in set (0.01 sec)

查看正在锁的事务

SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCKS;

mysql查看死锁与去除死锁示例详解

查看等待锁的事务

SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCK_WAITS;

mysql查看死锁与去除死锁示例详解

查看锁阻塞线程信息

3.1 使用show processlist查看

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掉即可。

3.5、 查看表锁的情况:

mysql> show status like 'table%';
+----------------------------+---------+
| Variable_name | Value |
+----------------------------+---------+
| Table_locks_immediate | 100 |
| Table_locks_waited | 11 |
+----------------------------+---------+

3.6、查看InnoDB_row_lock状态变量来分析系统上的行锁的争夺情况:

mysql> show status like 'InnoDB_row_lock%';
+-------------------------------+--------+
| Variable_name     | Value |
+-------------------------------+--------+
| Innodb_row_lock_current_waits | 0  |
| Innodb_row_lock_time   | 159372 |
| Innodb_row_lock_time_avg  | 39843 |
| Innodb_row_lock_time_max  | 51154 |
| Innodb_row_lock_waits   | 4  |
+-------------------------------+--------+
5 rows in set (0.01 sec)

mysql>

4. 结论

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

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

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

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

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

参考:https://www.jb51.net/article/201222.htm

DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。