ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction分析

1、4个用户连接数据库(A和D是本地回环登陆,B和C是本地登陆)

A用户信息:
    mysql> status;
    --------------
    Connection id:        8
    Current database:    db1
    Current user:        root@localhost
    Connection:            127.0.0.1 via TCP/IP

B用户信息:
    mysql> status;
    --------------
    Connection id:        3
    Current database:    db1
    Current user:        root@localhost
    Connection:            Localhost via UNIX socket

C用户信息:
    mysql> status;
    --------------
    Connection id:        6
    Current database:    db1
    Current user:        root@localhost
    Connection:            Localhost via UNIX socket
      
D用户信息:
    mysql> status;
    --------------
    Connection id:        9
    Current database:    db1
    Current user:        root@localhost
    Connection:            127.0.0.1 via TCP/IP

2、4个用户分别开启事务,对db1里面的t1表进行DML
    start transaction;

A用户执行:
    mysql> select * from t1;
    +------+-------+
    | id   | name  |
    +------+-------+
    |    4 | mvk   |
    |    6 | marry |
    |    5 | fang  |
    |    5 | fang  |
    +------+-------+
    4 rows in set (0.00 sec)

mysql> insert into t1 values(1,'juhua'),(2,'long');
    Query OK, 2 rows affected (0.00 sec)
    Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from t1;
    +------+-------+
    | id   | name  |
    +------+-------+
    |    4 | mvk   |
    |    6 | marry |
    |    5 | fang  |
    |    5 | fang  |
    |    1 | juhua |
    |    2 | long  |
    +------+-------+
    6 rows in set (0.01 sec)

B用户执行:
    mysql> select * from t1;
    +------+-------+
    | id   | name  |
    +------+-------+
    |    4 | mvk   |
    |    6 | marry |
    |    5 | fang  |
    |    5 | fang  |
    +------+-------+
    4 rows in set (0.00 sec)

mysql>
    mysql> update t1 set name='liu' where id=5;
    等待50秒,报错
    ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
在第5个会话层面查看:
    mysql> select * from information_schema.innodb_lock_waits;
    +-------------------+-------------------+-----------------+------------------+
    | requesting_trx_id | requested_lock_id | blocking_trx_id | blocking_lock_id |
    +-------------------+-------------------+-----------------+------------------+
    | 426545            | 426545:45:3:10    | 426540          | 426540:45:3:10   |
    +-------------------+-------------------+-----------------+------------------+
    1 row in set, 1 warning (0.00 sec)
    锁等待是426545,因为426540占着事务没有提交造成的

mysql> select * from information_schema.innodb_locks;
    +----------------+-------------+-----------+-----------+------------+-----------------+------------+-----------+----------+----------------+
    | lock_id        | lock_trx_id | lock_mode | lock_type | lock_table | lock_index      | lock_space | lock_page | lock_rec | lock_data      |
    +----------------+-------------+-----------+-----------+------------+-----------------+------------+-----------+----------+----------------+
    | 426545:45:3:10 | 426545      | X         | RECORD    | `db1`.`t1` | GEN_CLUST_INDEX |         45 |         3 |       10 | 0x000000242504 |
    | 426540:45:3:10 | 426540      | X         | RECORD    | `db1`.`t1` | GEN_CLUST_INDEX |         45 |         3 |       10 | 0x000000242504 |
    +----------------+-------------+-----------+-----------+------------+-----------------+------------+-----------+----------+----------------+
    
    mysql> select * from information_schema.innodb_trx\G
    *************************** 1. row ***************************
                        trx_id: 426546
                     trx_state: RUNNING
                   trx_started: 2019-08-07 17:24:36
         trx_requested_lock_id: NULL
              trx_wait_started: NULL
                    trx_weight: 1
           trx_mysql_thread_id: 6
                     trx_query: NULL
           trx_operation_state: NULL
             trx_tables_in_use: 0
             trx_tables_locked: 1
              trx_lock_structs: 1
         trx_lock_memory_bytes: 1136
               trx_rows_locked: 1
             trx_rows_modified: 0
       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: 0
              trx_is_read_only: 0
    trx_autocommit_non_locking: 0
    *************************** 2. row ***************************
                        trx_id: 426545
                     trx_state: LOCK WAIT
                   trx_started: 2019-08-07 17:21:36
         trx_requested_lock_id: 426545:45:3:10
              trx_wait_started: 2019-08-07 17:26:38
                    trx_weight: 5
           trx_mysql_thread_id: 3
                     trx_query: update t1 set name='liu' where id=5
           trx_operation_state: fetching rows
             trx_tables_in_use: 1
             trx_tables_locked: 1
              trx_lock_structs: 3
         trx_lock_memory_bytes: 1136
               trx_rows_locked: 6
             trx_rows_modified: 2
       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: 0
              trx_is_read_only: 0
    trx_autocommit_non_locking: 0
    *************************** 3. row ***************************
                        trx_id: 426540
                     trx_state: RUNNING
                   trx_started: 2019-08-07 17:20:10
         trx_requested_lock_id: NULL
              trx_wait_started: NULL
                    trx_weight: 4
           trx_mysql_thread_id: 8
                     trx_query: NULL
           trx_operation_state: NULL
             trx_tables_in_use: 0
             trx_tables_locked: 1
              trx_lock_structs: 2
         trx_lock_memory_bytes: 1136
               trx_rows_locked: 1
             trx_rows_modified: 2
       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: 0
              trx_is_read_only: 0
    trx_autocommit_non_locking: 0
    3 rows in set (0.00 sec)
    找到trx_mysql_thread_id对应的id号,show processlist看是否对应,然后commit事务即可。

改变情况:
    分别在B、C、D执行DML
    B:
    mysql> update t1 set name='liu' where id=5;
    C:
    mysql> delete from t1 where id=4;
    D:
    mysql> update t1 set id=8 where id=6;
    全部发生所等待
    
    mysql> select * from information_schema.innodb_lock_waits;
    +-------------------+-------------------+-----------------+------------------+
    | requesting_trx_id | requested_lock_id | blocking_trx_id | blocking_lock_id |
    +-------------------+-------------------+-----------------+------------------+
    | 426547            | 426547:45:3:4     | 426545          | 426545:45:3:4    |
    | 426546            | 426546:45:3:4     | 426547          | 426547:45:3:4    |
    | 426546            | 426546:45:3:4     | 426545          | 426545:45:3:4    |
    | 426545            | 426545:45:3:10    | 426540          | 426540:45:3:10   |
    +-------------------+-------------------+-----------------+------------------+
    
    mysql> select * from information_schema.innodb_trx\G
    *************************** 1. row ***************************
                        trx_id: 426547
                     trx_state: LOCK WAIT
                   trx_started: 2019-08-07 17:35:19
         trx_requested_lock_id: 426547:45:3:4
              trx_wait_started: 2019-08-07 17:36:48
                    trx_weight: 4
           trx_mysql_thread_id: 9
                     trx_query: update t1 set id=8 where id=6
           trx_operation_state: starting index read
             trx_tables_in_use: 1
             trx_tables_locked: 1
              trx_lock_structs: 2
         trx_lock_memory_bytes: 1136
               trx_rows_locked: 1
             trx_rows_modified: 2
       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: 0
              trx_is_read_only: 0
    trx_autocommit_non_locking: 0
    *************************** 2. row ***************************
                        trx_id: 426546
                     trx_state: LOCK WAIT
                   trx_started: 2019-08-07 17:24:36
         trx_requested_lock_id: 426546:45:3:4
              trx_wait_started: 2019-08-07 17:36:52
                    trx_weight: 2
           trx_mysql_thread_id: 6
                     trx_query: delete from t1 where id=4
           trx_operation_state: starting index read
             trx_tables_in_use: 1
             trx_tables_locked: 1
              trx_lock_structs: 2
         trx_lock_memory_bytes: 1136
               trx_rows_locked: 2
             trx_rows_modified: 0
       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: 0
              trx_is_read_only: 0
    trx_autocommit_non_locking: 0
    *************************** 3. row ***************************
                        trx_id: 426545
                     trx_state: LOCK WAIT
                   trx_started: 2019-08-07 17:21:36
         trx_requested_lock_id: 426545:45:3:10
              trx_wait_started: 2019-08-07 17:36:56
                    trx_weight: 5
           trx_mysql_thread_id: 3
                     trx_query: update t1 set name='liu' where id=5
           trx_operation_state: fetching rows
             trx_tables_in_use: 1
             trx_tables_locked: 1
              trx_lock_structs: 3
         trx_lock_memory_bytes: 1136
               trx_rows_locked: 7
             trx_rows_modified: 2
       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: 0
              trx_is_read_only: 0
    trx_autocommit_non_locking: 0
    *************************** 4. row ***************************
                        trx_id: 426540
                     trx_state: RUNNING
                   trx_started: 2019-08-07 17:20:10
         trx_requested_lock_id: NULL
              trx_wait_started: NULL
                    trx_weight: 4
           trx_mysql_thread_id: 8
                     trx_query: NULL
           trx_operation_state: NULL
             trx_tables_in_use: 0
             trx_tables_locked: 1
              trx_lock_structs: 2
         trx_lock_memory_bytes: 1136
               trx_rows_locked: 1
             trx_rows_modified: 2
       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: 0
              trx_is_read_only: 0
    trx_autocommit_non_locking: 0
    4 rows in set (0.00 sec)
    直接在426540对应的trx_mysql_thread_id: 8会话提交之后,发现B和D锁等待消失,只有C还有锁等待。
    查找innodb_lock_waits表看到426545还引起了锁等待。同理commit即可。
   
    mysql> select * from information_schema.innodb_lock_waits;
    +-------------------+-------------------+-----------------+------------------+
    | requesting_trx_id | requested_lock_id | blocking_trx_id | blocking_lock_id |
    +-------------------+-------------------+-----------------+------------------+
    | 426546            | 426546:45:3:4     | 426545          | 426545:45:3:4    |
    +-------------------+-------------------+-----------------+------------------+
    1 row in set, 1 warning (0.00 sec)

Lock wait timeout分析的更多相关文章

  1. SQL性能优化常见措施(Lock wait timeout exceeded)

    SQL性能优化常见措施 目 录 1.mysql中explain命令使用 2.mysql中mysqldumpslow的使用 3.mysql中修改my.ini配置文件记录日志 4.mysql中如何加索引 ...

  2. mysql死锁,等待资源,事务锁,Lock wait timeout exceeded; try restarting transaction解决

    前面已经了解了InnoDB关于在出现锁等待的时候,会根据参数innodb_lock_wait_timeout的配置,判断是否需要进行timeout的操作,本文档介绍在出现锁等待时候的查看及分析处理: ...

  3. java.sql.SQLException: Lock wait timeout exceeded --转

    org.springframework.dao.CannotAcquireLockException 的解决> 直接上 bug 的详细信息: 2012-03-12 15:20:31 XmlBea ...

  4. 排查mysql innodb Lock wait timeout exceeded; try restarting transaction的问题

    OMG写的时候崩溃了一次. 触发关注这个问题的事情是 我们在使用pt-online-schedule 改表的时候总是拿不到锁,并且报出mysql innodb Lock wait timeout ex ...

  5. com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Lock wait timeout exceeded; try restarting transaction

    本文为博主原创: 以下为在程序运行过程中报的错误, org.springframework.dao.CannotAcquireLockException: ### Error updating dat ...

  6. mysql Lock wait timeout exceeded; try restarting transaction解决

    前面已经了解了InnoDB关于在出现锁等待的时候,会根据参数innodb_lock_wait_timeout的配置,判断是否需要进行timeout的操作,本文档介绍在出现锁等待时候的查看及分析处理: ...

  7. Spring_错误 java.sql.SQLException: Lock wait timeout exceeded | CannotAcquireLockException 的解决

    java.sql.SQLException: Lock wait timeout exceeded |  org.springframework.dao.CannotAcquireLockExcept ...

  8. ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction 表被锁的解决办法

    转自:https://blog.csdn.net/mchdba/article/details/38313881 前言:朋友咨询我说执行简单的update语句失效,症状如下:mysql> upd ...

  9. Lock wait timeout exceeded数据库死锁问题

    环境 MySQL5.5 现象 A.数据更新或新增后数据经常自动回滚. B.表操作总报 Lock wait timeout exceeded 并长时间无反应 解决方法 A.应急方法:show proce ...

随机推荐

  1. BUG处理流程图

    流程描述: 1. 测试人员发现bug提交给开发. 2. 开发人员判断是否是bug. 3. 如果是bug,进行修改,修改完成后更改bug状态为已解决. 4. 如果不是bug,退回给测试人员并描述退回原因 ...

  2. Bladex使用代码生成器操作步骤

    一.从私服上下载BladeX和Saber 二.运行BladeX所有服务 三.运行Saber 四.数据库创建自己需要使用的表(建议表名和字段名为:bldex_xxxx,xxx_xxxx,不要使用驼峰命名 ...

  3. 简易商城 [ html + css ] 练习

    1. 前言 通过使用 HTML + CSS 编写一个简易商城首页. 如图: 2. 布局思路 通过页面分析,大致可以决定页面的布局分为 5 大板块. 接下来,可以先定义页面的布局: <!DOCTY ...

  4. 论文阅读:FaceBoxes: A CPU Real-time Face Detector with High Accuracy

    文章: <FaceBoxes: A CPU Real-time Face Detector with High Accuracy> Introduction 2个挑战: 1)在杂乱背景下人 ...

  5. dubbo调用外网接口 注册外网ip到zookeeper 暴露外网ip

    dubbo注册时会通过主机名寻找ip,会将内网ip注册到zookeeper 如果我们调用外网服务器server的话是找不到的 会报如下错误 [DUBBO] client reconnect to 12 ...

  6. docker+k8s基础篇五

    Docker+K8s基础篇(五) service资源介绍 A:service资源的工作特性 service的使用 A:service字段介绍 B:ClusterIP的简单使用 C:NodePort的简 ...

  7. url与uri区别

    url属性: 是要求按照url的写法来写地址 URL:Uniform Resource Locator 统一资源定位符.它是可以唯一标识一个资源的位置 写法: http://localhost:808 ...

  8. hive学习(1)

    什么是Hive Hive是基于Hadoop的一个数据仓库工具(E抽取T转换L加载),可以将结构化的数据文件映射为一张表,并提供类SQL查询功能. 本质是:将HQL转化成MapReduce程序 Hive ...

  9. Relative Sort Array

    Relative Sort Array Given two arrays arr1 and arr2, the elements of arr2 are distinct, and all eleme ...

  10. JMeter中文乱码的解决

    对于JMeter中文乱码分为两部分: 1.请求(request):由于引用了csv文件当csv文件编码与JMeter不统一可能造成服务器收到数据的中文乱码 2.返回(response):由于服务器返回 ...