转载地址: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了。

查找原始MySQL死锁ID的更多相关文章

  1. MySQL 死锁问题分析

    转载: MySQL 死锁问题分析 线上某服务时不时报出如下异常(大约一天二十多次):"Deadlock found when trying to get lock;". Oh, M ...

  2. 为什么MySQL死锁检测会严重降低TPS

    在大量的客户端,更新数据表的同一行时,会造成数据库的吞吐量大幅降低. 很多数据库的前辈和同行分别通过实验和源码的方法,定位到了罪魁祸首----MySQL死锁检测 实验方式:http://blog.cs ...

  3. mysql 死锁检查

    今天看了一篇关于死锁检查的blog. Advanced InnoDB Deadlock Troubleshooting – What SHOW INNODB STATUS Doesn’t Tell Y ...

  4. MySQL 死锁日志分析

    ------------------------ LATEST DETECTED DEADLOCK ------------------------ 140824  1:01:24 *** (1) T ...

  5. mysql死锁——mysql之四

    1.MySQL常用存储引擎的锁机制 MyISAM和MEMORY采用表级锁(table-level locking) BDB采用页面锁(page-level locking)或表级锁,默认为页面锁 In ...

  6. Mysql死锁问题解决方式 & 聚簇索引、隔离级别等知识

    参考了这篇文章:http://www.cnblogs.com/LBSer/p/5183300.html  <mysql死锁问题分析> 写的不错. 如果Mysql死锁,会报出: 1.1 死锁 ...

  7. 一次MySQL死锁问题解决

    一次MySQL死锁问题解决 一.环境 CentOS, MySQL 5.6.21-70, JPA 问题场景:系统有定时批量更新数据状态操作,每次更新上千条记录,表中总记录数约为500W左右. 二.错误日 ...

  8. MySQL死锁案例分析与解决方案

    MySQL死锁案例分析与解决方案 现象: 数据库查询: SQL语句分析:  mysql. 并发delete同一行记录,偶发死锁.   delete from x_table where id=?   ...

  9. MySQL死锁分析一例

    Tomcat日志报死锁错误,show innodb status获取死锁信息: ------------------------ LATEST DETECTED DEADLOCK ---------- ...

随机推荐

  1. C#连接Sql Serve数据库及增,删,改操作

    一:连接. string sqlconn = "server=主机名;database=数据名;integrated security=true" //integrated sec ...

  2. oracle 几个不常用的关键字

    ntile (n)是分析函数,n是吧窗体的数据分成几组. sys_connect_by_path 用来列转行 connect_by_isleaf 判断层级查询是否为叶子节点. siblings 用来层 ...

  3. 分享一个js中的bind方法使用

    来源:http://www.cnblogs.com/yuzhongwusan/archive/2012/02/13/2348782.html Js代码 复制代码 代码如下: var first_obj ...

  4. 修改订单的收货时间(MySQL)脚本

    需求 下单时间为21:00-23:59,并且收货时间为第二天的订单,将这些订单的收货时间改为第三天: #!/bin/bash ###time:2016-04-26 ###This script is ...

  5. lua 学习笔记一

    参考: http://www.kancloud.cn/digest/luanote/119924 1.基础概念 1.lua八种基本数据类型:nil.boolean.string.function.ta ...

  6. @ViewDebug.ExportedProperty的使用

    原文链接:http://daemon369.github.io/android/2014/06/12/android-viewdebug-exportedproperty/ http://www.eo ...

  7. 驱动开发学习笔记. 0.06 嵌入式linux视频开发之预备知识

    驱动开发读书笔记. 0.06  嵌入式linux视频开发之预备知识 由于毕业设计选择了嵌入式linux视频开发相关的项目,于是找了相关的资料,下面是一下预备知识 UVC : UVC,全称为:USB v ...

  8. java的Iterator源码浅析

    在java的集合中,List接口继承Collection接口,AbstractList类实现了List接口,在AbstractList中的内部类Itr实现了Iterator接口 ArrayList实现 ...

  9. 编辑器Ultraedit快捷键

    编辑器Ultraedit快捷键说到编辑器的快捷键,VIM是无与伦比的.要反对,也得是带脚踏板的EmaCS.UE还是有差距的,很大差距.注意:VIM是开源.免费的,而UE则需要注册.UE是Windows ...

  10. 选择c3p0作为连接池

    <hibernate-configuration>     <session-factory>         <property name="dialect& ...