查找原始MySQL死锁ID

转载地址:http://yueliangdao0608.blog.51cto.com/397025/1180917

如果遇到死锁了,怎么解决呢?找到原始的锁ID,然后KILL掉一直持有的那个线程就可以了, 但是众多线程,可怎么找到引起死锁的线程ID呢? MySQL 发展到现在,已经非常强大了,这个问题很好解决。 直接从数据字典连查找。

 
我们来演示下。
 
线程A,我们用来锁定某些记录,假设这个线程一直没提交,或者忘掉提交了。 那么就一直存在,但是数据里面显示的只是SLEEP状态。
mysql> set @@autocommit=;
Query OK, rows affected (0.00 sec) mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| demo_test |
| t3 |
+----------------+
rows in set (0.00 sec) mysql> select * from t3;
+----+--------+--------+------------+----+----+----+
| id | fname | lname | birthday | c1 | c2 | c3 |
+----+--------+--------+------------+----+----+----+
| | lily19 | lucy19 | -- | | | |
| | lily20 | lucy20 | -- | | | |
+----+--------+--------+------------+----+----+----+
rows in set (0.00 sec) mysql> update t3 set birthday = '2022-02-23' where id = ;
Query OK, row affected (0.00 sec)
Rows matched: Changed: Warnings: mysql> select connection_id();
+-----------------+
| connection_id() |
+-----------------+
| |
+-----------------+
row in set (0.00 sec) mysql>
线程B, 我们用来进行普通的更新,但是遇到问题了,此时不知道是哪个线程把这行记录给锁定了?
 
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A Database changed
mysql> select @@autocommit;
+--------------+
| @@autocommit |
+--------------+
| |
+--------------+
row in set (0.00 sec) mysql> update t3 set birthday='2018-01-03' where id = ;
ERROR (HY000): Lock wait timeout exceeded; try restarting transaction
mysql> select connection_id();
+-----------------+
| connection_id() |
+-----------------+
| |
+-----------------+
row in set (0.00 sec) mysql> show processlist;
+----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+------+---------+------+-------+------------------+
| | root | localhost | NULL | Sleep | | | NULL |
| | root | localhost | NULL | Sleep | | | NULL |
| | root | localhost | test | Sleep | | | NULL |
| | root | localhost | test | Query | | init | show processlist |
| | root | localhost | NULL | Sleep | | | NULL |
+----+------+-----------+------+---------+------+-------+------------------+
rows in set (0.00 sec) mysql> show engine innodb status\G ------------
TRANSACTIONS
------------
Trx id counter
Purge done for trx's n:o < 189323 undo n:o < 0 state: running but idle
History list length
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION , not started
MySQL thread id , OS thread handle 0x7f70a0c98700, query id localhost root init
show engine innodb status
---TRANSACTION , ACTIVE sec starting index read
mysql tables in use , locked
LOCK WAIT lock struct(s), heap size , row lock(s)
MySQL thread id , OS thread handle 0x7f70a0bd5700, query id localhost root updating
update t3 set birthday='2018-01-03' where id =
------- TRX HAS BEEN WAITING SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id page no n bits index `PRIMARY` of table `test`.`t3` trx id lock_mode X waiting
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 00000002e38c; asc ;;
: len ; hex 7e00000d2827c9; asc ~ (' ;;
: len ; hex 6c696c793139; asc lily19;;
: len ; hex 6c7563793139; asc lucy19;;
: len ; hex 8fcc57; asc W;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; ------------------
---TRANSACTION , ACTIVE sec
lock struct(s), heap size , row lock(s), undo log entries
MySQL thread id , OS thread handle 0x7f70a0b94700, query id localhost root cleaning up
Trx read view will not see trx with id >= , sees <
上面的信息很繁多,也看不清楚到底哪里是哪里。
不过现在,我们只要从数据字典里面拿出来这部分信息就OK了。
 
mysql> SELECT * FROM information_schema.INNODB_TRX\G
*************************** . row ***************************
trx_id:
trx_state: RUNNING
trx_started: -- ::
trx_requested_lock_id: NULL
trx_wait_started: NULL
trx_weight:
trx_mysql_thread_id:
trx_query: NULL
trx_operation_state: NULL
trx_tables_in_use:
trx_tables_locked:
trx_lock_structs:
trx_lock_memory_bytes:
trx_rows_locked:
trx_rows_modified:
trx_concurrency_tickets:
trx_isolation_level: REPEATABLE READ
trx_unique_checks:
trx_foreign_key_checks:
trx_last_foreign_key_error: NULL
trx_adaptive_hash_latched:
trx_adaptive_hash_timeout:
trx_is_read_only:
trx_autocommit_non_locking:
row in set (0.01 sec) mysql>

原来是线程16忘掉COMMIT了。

上一篇:C++自定义字符串类


下一篇:PHP的学习--Traits新特性