侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

查找MySQL线程中死锁的ID的方法

数据库  /  管理员 发布于 6年前   186

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

我们来演示下。

线程A,我们用来锁定某些记录,假设这个线程一直没提交,或者忘掉提交了。 那么就一直存在,但是数据里面显示的只是SLEEP状态。

mysql> set @@autocommit=0;Query OK, 0 rows affected (0.00 sec)mysql> use test;Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> show tables;+―――――-+| Tables_in_test |+―――――-+| demo_test   || t3       |+―――――-+2 rows in set (0.00 sec)mysql> select * from t3;+―-+――C+――C+――――+―-+―-+―-+| id | fname | lname | birthday  | c1 | c2 | c3 |+―-+――C+――C+――――+―-+―-+―-+| 19 | lily19 | lucy19 | 2013-04-18 | 19 | 0 | 0 || 20 | lily20 | lucy20 | 2013-03-13 | 20 | 0 | 0 |+―-+――C+――C+――――+―-+―-+―-+2 rows in set (0.00 sec)mysql> update t3 set birthday = '2022-02-23' where id = 19;Query OK, 1 row affected (0.00 sec)Rows matched: 1 Changed: 1 Warnings: 0mysql> select connection_id();+―――――C+| connection_id() |+―――――C+|       16 |+―――――C+1 row in set (0.00 sec)mysql> 

线程B, 我们用来进行普通的更新,但是遇到问题了,此时不知道是哪个线程把这行记录给锁定了?

mysql> use test;Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> select @@autocommit;+――――C+| @@autocommit |+――――C+|      1 |+――――C+1 row in set (0.00 sec)mysql> update t3 set birthday='2018-01-03' where id = 19;ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transactionmysql> select connection_id();+―――――C+| connection_id() |+―――――C+|       17 |+―――――C+1 row in set (0.00 sec)mysql> show processlist;+―-+――+―――C+――+―――+――+――-+――――――+| Id | User | Host   | db  | Command | Time | State | Info       |+―-+――+―――C+――+―――+――+――-+――――――+| 10 | root | localhost | NULL | Sleep  | 1540 |    | NULL       || 11 | root | localhost | NULL | Sleep  | 722 |    | NULL       || 16 | root | localhost | test | Sleep  | 424 |    | NULL       || 17 | root | localhost | test | Query  |  0 | init | show processlist || 18 | root | localhost | NULL | Sleep  |  5 |    | NULL       |+―-+――+―――C+――+―――+――+――-+――――――+5 rows in set (0.00 sec)mysql> show engine innodb status\G――――TRANSACTIONS――――Trx id counter 189327Purge done for trx's n:o < 189323 undo n:o < 0 state: running but idleHistory list length 343LIST OF TRANSACTIONS FOR EACH SESSION:―TRANSACTION 0, not startedMySQL thread id 11, OS thread handle 0x7f70a0c98700, query id 994 localhost root initshow engine innodb status―TRANSACTION 189326, ACTIVE 2 sec starting index readmysql tables in use 1, locked 1LOCK WAIT 2 lock struct(s), heap size 376, 1 row lock(s)MySQL thread id 17, OS thread handle 0x7f70a0bd5700, query id 993 localhost root updatingupdate t3 set birthday='2018-01-03' where id = 19――- TRX HAS BEEN WAITING 2 SEC FOR THIS LOCK TO BE GRANTED:RECORD LOCKS space id 529 page no 3 n bits 72 index `PRIMARY` of table `test`.`t3` trx id 189326 lock_mode X waitingRecord lock, heap no 2 PHYSICAL RECORD: n_fields 9; compact format; info bits 00: len 2; hex 3139; asc 19;;1: len 6; hex 00000002e38c; asc    ;;2: len 7; hex 7e00000d2827c9; asc ~  (' ;;3: len 6; hex 6c696c793139; asc lily19;;4: len 6; hex 6c7563793139; asc lucy19;;5: len 3; hex 8fcc57; asc  W;;6: len 4; hex 80000013; asc   ;;7: len 4; hex 80000000; asc   ;;8: len 4; hex 80000000; asc   ;;―――――――TRANSACTION 189324, ACTIVE 641 sec2 lock struct(s), heap size 376, 3 row lock(s), undo log entries 1MySQL thread id 16, OS thread handle 0x7f70a0b94700, query id 985 localhost root cleaning upTrx read view will not see trx with id >= 189325, sees < 189325

上面的信息很繁多,也看不清楚到底哪里是哪里。

不过现在,我们只要从数据字典里面拿出来这部分信息就OK了。

mysql> SELECT * FROM information_schema.INNODB_TRX\G*************************** 1. row ***************************          trx_id: 189324        trx_state: RUNNING       trx_started: 2013-04-18 17:48:14  trx_requested_lock_id: NULL     trx_wait_started: NULL        trx_weight: 3   trx_mysql_thread_id: 16        trx_query: NULL   trx_operation_state: NULL    trx_tables_in_use: 0    trx_tables_locked: 0     trx_lock_structs: 2  trx_lock_memory_bytes: 376     trx_rows_locked: 3    trx_rows_modified: 1 trx_concurrency_tickets: 0   trx_isolation_level: REPEATABLE READ    trx_unique_checks: 1  trx_foreign_key_checks: 1trx_last_foreign_key_error: NULLtrx_adaptive_hash_latched: 0trx_adaptive_hash_timeout: 10000     trx_is_read_only: 0trx_autocommit_non_locking: 01 row in set (0.01 sec)mysql> 

原来是线程16忘掉COMMIT了。


  • 上一条:
    使用mysqladmin检测MySQL运行状态的教程
    下一条:
    探究MySQL优化器对索引和JOIN顺序的选择
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 分库分表的目的、优缺点及具体实现方式介绍(0个评论)
    • DevDB - 在 VS 代码中直接访问数据库(0个评论)
    • 在ubuntu系统中实现mysql数据存储目录迁移流程步骤(0个评论)
    • 在mysql中使用存储过程批量新增测试数据流程步骤(0个评论)
    • php+mysql数据库批量根据条件快速更新、连表更新sql实现(0个评论)
    • 近期文章
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2017-06
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-05
    • 2018-10
    • 2018-11
    • 2020-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2021-02
    • 2021-04
    • 2021-07
    • 2021-08
    • 2021-11
    • 2021-12
    • 2022-02
    • 2022-03
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-07
    • 2023-08
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-03
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客