热门标签 | HotTags
当前位置:  开发笔记 > 后端 > 正文

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

这篇文章主要介绍了查找MySQL线程中死锁的ID的方法,文中介绍的方法主要是从数据字典里查找,需要的朋友可以参考下
如果遇到死锁了,怎么解决呢?找到原始的锁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 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       |

+—————-+

2 rows in set (0.00 sec)

mysql> select * from t3;

+—-+——–+——–+————+—-+—-+—-+

| id | fname | lname | birthday  | c1 | c2 | c3 |

+—-+——–+——–+————+—-+—-+—-+

| 19 | lily19 | lucy19 | 2013-04-18 | 19 | 0 | 0 |

| 20 | lily20 | lucy20 | 2013-03-13 | 20 | 0 | 0 |

+—-+——–+——–+————+—-+—-+—-+

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: 0

mysql> select connection_id();

+—————–+

| connection_id() |

+—————–+

|       16 |

+—————–+

1 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 |

+————–+

|      1 |

+————–+

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 transaction

mysql> select connection_id();

+—————–+

| connection_id() |

+—————–+

|       17 |

+—————–+

1 row in set (0.00 sec)

mysql> show processlist;

+—-+——+———–+——+———+——+——-+——————+

| Id | User | Host   | db  | Command | Time | State | Info       |

+—-+——+———–+——+———+——+——-+——————+

| 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       |

+—-+——+———–+——+———+——+——-+——————+

5 rows in set (0.00 sec)

mysql> show engine innodb status\G

————

TRANSACTIONS

————

Trx id counter 189327

Purge done for trx's n:o <189323 undo n:o <0 state: running but idle

History list length 343

LIST OF TRANSACTIONS FOR EACH SESSION:

—TRANSACTION 0, not started

MySQL thread id 11, OS thread handle 0x7f70a0c98700, query id 994 localhost root init

show engine innodb status

—TRANSACTION 189326, ACTIVE 2 sec starting index read

mysql tables in use 1, locked 1

LOCK 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 updating

update 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 waiting

Record lock, heap no 2 PHYSICAL RECORD: n_fields 9; compact format; info bits 0

0: 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 sec

2 lock struct(s), heap size 376, 3 row lock(s), undo log entries 1

MySQL thread id 16, OS thread handle 0x7f70a0b94700, query id 985 localhost root cleaning up

Trx 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: 1

trx_last_foreign_key_error: NULL

trx_adaptive_hash_latched: 0

trx_adaptive_hash_timeout: 10000

     trx_is_read_only: 0

trx_autocommit_non_locking: 0

1 row in set (0.01 sec)

mysql> 

原来是线程16忘掉COMMIT了。

推荐阅读
  • Java实战之电影在线观看系统的实现
    本文介绍了Java实战之电影在线观看系统的实现过程。首先对项目进行了简述,然后展示了系统的效果图。接着介绍了系统的核心代码,包括后台用户管理控制器、电影管理控制器和前台电影控制器。最后对项目的环境配置和使用的技术进行了说明,包括JSP、Spring、SpringMVC、MyBatis、html、css、JavaScript、JQuery、Ajax、layui和maven等。 ... [详细]
  • PHP设置MySQL字符集的方法及使用mysqli_set_charset函数
    本文介绍了PHP设置MySQL字符集的方法,详细介绍了使用mysqli_set_charset函数来规定与数据库服务器进行数据传送时要使用的字符集。通过示例代码演示了如何设置默认客户端字符集。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • 本文详细介绍了MysqlDump和mysqldump进行全库备份的相关知识,包括备份命令的使用方法、my.cnf配置文件的设置、binlog日志的位置指定、增量恢复的方式以及适用于innodb引擎和myisam引擎的备份方法。对于需要进行数据库备份的用户来说,本文提供了一些有价值的参考内容。 ... [详细]
  • 本文由编程笔记小编整理,介绍了PHP中的MySQL函数库及其常用函数,包括mysql_connect、mysql_error、mysql_select_db、mysql_query、mysql_affected_row、mysql_close等。希望对读者有一定的参考价值。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 数据库(外键及其约束理解)(https:www.cnblogs.comchenxiaoheip6909318.html)My ... [详细]
  • 本文介绍了高校天文共享平台的开发过程中的思考和规划。该平台旨在为高校学生提供天象预报、科普知识、观测活动、图片分享等功能。文章分析了项目的技术栈选择、网站前端布局、业务流程、数据库结构等方面,并总结了项目存在的问题,如前后端未分离、代码混乱等。作者表示希望通过记录和规划,能够理清思路,进一步完善该平台。 ... [详细]
  • 本文介绍了在Mac上搭建php环境后无法使用localhost连接mysql的问题,并通过将localhost替换为127.0.0.1或本机IP解决了该问题。文章解释了localhost和127.0.0.1的区别,指出了使用socket方式连接导致连接失败的原因。此外,还提供了相关链接供读者深入了解。 ... [详细]
  • yum安装_Redis —yum安装全过程
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了Redis—yum安装全过程相关的知识,希望对你有一定的参考价值。访问https://redi ... [详细]
  • 解决VS写C#项目导入MySQL数据源报错“You have a usable connection already”问题的正确方法
    本文介绍了在VS写C#项目导入MySQL数据源时出现报错“You have a usable connection already”的问题,并给出了正确的解决方法。详细描述了问题的出现情况和报错信息,并提供了解决该问题的步骤和注意事项。 ... [详细]
  • 本文介绍了关于apache、phpmyadmin、mysql、php、emacs、path等知识点,以及如何搭建php环境。文章提供了详细的安装步骤和所需软件列表,希望能帮助读者解决与LAMP相关的技术问题。 ... [详细]
  • 本文介绍了通过mysql命令查看mysql的安装路径的方法,提供了相应的sql语句,并希望对读者有参考价值。 ... [详细]
  • 本文详细介绍了MySQL表分区的创建、增加和删除方法,包括查看分区数据量和全库数据量的方法。欢迎大家阅读并给予点评。 ... [详细]
author-avatar
sadsafsasgdg
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有