一、问题描述:

同事反馈线上一个表有其中一条数据无法删除,其他都正常,我拿到删数据的sql,尝试执行,报错如下:

mysql> delete from facebook_posts where id = 7048962;

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

 

二、问题处理

从报错信息看,应该是关于这条数据有事物未提交,锁等待超时了,下面我们就开始验证并解决问题

1、在sql执行期间,通过information_schema.innodb_trx表找到这个sql的事物ID(5316933097 )

mysql> select trx_id,trx_started,trx_requested_lock_id,trx_mysql_thread_id,trx_query from information_schema.innodb_trx where trx_query='delete from facebook_posts where id = 7048962';
+------------+---------------------+------------------------+---------------------+-----------------------------------------------+
|   trx_id |      trx_started | t rx_requested_lock_id | trx_mysql_thread_id | trx_query
+------------+---------------------+------------------------+---------------------+-----------------------------------------------+
| 5316933097 | 2017-08-15 07:31:57 | 5316933097:923:24693:6 | 1798850878      | delete from facebook_posts where id = 7048962 |
+------------+---------------------+------------------------+---------------------+-----------------------------------------------+
1 row in set (0.00 sec)

 

关于innodb_trx表字段含义的解释:

  1. mysql> desc information_schema.innodb_trx;
  2. +----------------------------+---------------------+------+-----+---------------------+-------+
  3. | Field | Type | Null | Key | Default | Extra |
  4. +----------------------------+---------------------+------+-----+---------------------+-------+
  5. | trx_id | varchar(18) | NO | | | |#事务ID
  6. | trx_state | varchar(13) | NO | | | |#事物状态
  7. | trx_started | datetime | NO | | 0000-00-00 00:00:00 | |#事物开始时间
  8. | trx_requested_lock_id | varchar(81) | YES | | NULL | |#事物请求锁ID
  9. | trx_wait_started | datetime | YES | | NULL | |#事物开始等待时间
  10. | trx_weight | bigint(21) unsigned | NO | | 0 | |#
  11. | trx_mysql_thread_id | bigint(21) unsigned | NO | | 0 | |#事物线程ID,即show processlist看到ID
  12. | trx_query | varchar(1024) | YES | | NULL | |#具体SQL
  13. | trx_operation_state | varchar(64) | YES | | NULL | |#事物当前操作状态
  14. | trx_tables_in_use | bigint(21) unsigned | NO | | 0 | |#事物中有多少个表被使用
  15. | trx_tables_locked | bigint(21) unsigned | NO | | 0 | |#使用拥有多少个锁
  16. | trx_lock_structs | bigint(21) unsigned | NO | | 0 | |#
  17. | trx_lock_memory_bytes | bigint(21) unsigned | NO | | 0 | |#事物锁住的内存大小
  18. | trx_rows_locked | bigint(21) unsigned | NO | | 0 | |#事物锁住的行数
  19. | trx_rows_modified | bigint(21) unsigned | NO | | 0 | |#使用修改的行数
  20. | trx_concurrency_tickets | bigint(21) unsigned | NO | | 0 | |#事物并发票数
  21. | trx_isolation_level | varchar(16) | NO | | | |#事物隔离级别
  22. | trx_unique_checks | int(1) | NO | | 0 | |#是否唯一性检查
  23. | trx_foreign_key_checks | int(1) | NO | | 0 | |#是否外键检查
  24. | trx_last_foreign_key_error | varchar(256) | YES | | NULL | |#最后的外键错误
  25. | trx_adaptive_hash_latched | int(1) | NO | | 0 | |#
  26. | trx_adaptive_hash_timeout | bigint(21) unsigned | NO | | 0 | |#
  27. | trx_is_read_only | int(1) | NO | | 0 | |#
  28. | trx_autocommit_non_locking | int(1) | NO | | 0 | |#
  29. +----------------------------+---------------------+------+-----+---------------------+-------+
  30. 24 rows in set (0.00 sec)
 

2、通过上面步骤1找到的事物ID,找到占有锁的事物ID(5316888834  )

  1. mysql> select * from information_schema.innodb_lock_waits where requesting_trx_id=5316933097;
  2. +-------------------+------------------------+-----------------+------------------------+
  3. | requesting_trx_id | requested_lock_id | blocking_trx_id | blocking_lock_id |
  4. +-------------------+------------------------+-----------------+------------------------+
  5. | 5316933097 | 5316933097:923:24693:6 | 5316888834 | 5316888834:923:24693:6 |
  6. +-------------------+------------------------+-----------------+------------------------+
  7. 1 row in set (0.00 sec)
 

关于innodb_lock_waits 表的字段含义的解释:

  1. mysql> desc information_schema.innodb_lock_waits;
  2. +-------------------+-------------+------+-----+---------+-------+
  3. | Field | Type | Null | Key | Default | Extra |
  4. +-------------------+-------------+------+-----+---------+-------+
  5. | requesting_trx_id | varchar(18) | NO | | | |#请求锁的事物ID
  6. | requested_lock_id | varchar(81) | NO | | | |#请求锁的锁ID
  7. | blocking_trx_id | varchar(18) | NO | | | |#当前拥有锁的事物ID
  8. | blocking_lock_id | varchar(81) | NO | | | |#当前拥有锁的锁ID
  9. +-------------------+-------------+------+-----+---------+-------+
  10. 4 rows in set (0.00 sec)
 

3、通过步骤2找到的占有锁的事物ID,找到占有锁的事物线程ID(1790259884 )

  1. mysql> select * from information_schema.innodb_trx where trx_id=5316888834 \G
  2. *************************** 1. row ***************************
  3. trx_id: 5316888834
  4. trx_state: RUNNING
  5. trx_started: 2017-08-15 06:00:21
  6. trx_requested_lock_id: NULL
  7. trx_wait_started: NULL
  8. trx_weight: 6
  9. trx_mysql_thread_id: 1790259884
  10. trx_query: NULL
  11. trx_operation_state: NULL
  12. trx_tables_in_use: 0
  13. trx_tables_locked: 0
  14. trx_lock_structs: 6
  15. trx_lock_memory_bytes: 1184
  16. trx_rows_locked: 10
  17. trx_rows_modified: 0
  18. trx_concurrency_tickets: 0
  19. trx_isolation_level: REPEATABLE READ
  20. trx_unique_checks: 1
  21. trx_foreign_key_checks: 1
  22. trx_last_foreign_key_error: NULL
  23. trx_adaptive_hash_latched: 0
  24. trx_adaptive_hash_timeout: 10000
  25. trx_is_read_only: 0
  26. trx_autocommit_non_locking: 0
  27. 1 row in set (0.00 sec)
 

4、通过步骤3找的事物ID,可以查看下这个事物发起的账号和主机信息,提供给开发人员查找异常的真正原因,并kill这个事物ID,这条数据就可以正常删除了

  1. #查看下这个事物发起的账号和主机信息
  2. mysql> select * from information_schema.processlist where ID=1790259884;
  3. +------------+----------+---------------------+--------+---------+------+-------+------+
  4. | ID | USER | HOST | DB | COMMAND | TIME | STATE | INFO |
  5. +------------+----------+---------------------+--------+---------+------+-------+------+
  6. | 1790259884 | spider_w | 172.31.11.143:46120 | db_mta | Sleep | 1319 | | NULL |
  7. +------------+----------+---------------------+--------+---------+------+-------+------+
  8. 1 row in set (0.01 sec)
  9. #kill 这个未提交的事物线程ID
  10. mysql> CALL mysql.rds_kill(1790259884);
  11. Query OK, 0 rows affected (0.00 sec)
  12. #删除数据
  13. mysql> delete from facebook_posts where id = 7041232;
  14. Query OK, 1 row affected (0.02 sec)

ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction解决办法的更多相关文章

  1. ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction

    测试库一条update语句报错:ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction mysql> ...

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

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

  3. Mysql错误:ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction

    昨晚添加完索引之后, 查询整表的时候抛出Lock wait timeout exceeded; try restarting transaction, 吓死小白的我, 为什么条件查询可以, 整表查不了 ...

  4. 执行 update操作的时候有报错 ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction

    mysql> show full processlist; #查看问题的线程!!!! 找到异常进程的ID 然后kill 掉: mysql> kill xxxxxxx; #xxxxxx是ID ...

  5. Mysql错误: ERROR 1205: Lock wait timeout exceeded try restarting transaction解决办法

    select * from information_schema.INNODB_TRX;show full processlist;//找出目前连接的列表kill ID//根据ID kill掉

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

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

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

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

  8. SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded; try restarting transaction 解决方法

    SHOW FULL PROCESSLIST; KILL 263071

  9. InternalError: (pymysql.err.InternalError) (1205, u'Lock wait timeout exceeded; try restarting transaction')

    在mysql innodb中使用事务,如果插入或者更新出错,一定要主动显式地执行rollback,否则可能产生不必要的锁而锁住其他的操作 我们在使用数据库的时候,可以使用contextlib,这样异常 ...

随机推荐

  1. Javascript 2.9

    对象:属性和方法属性:隶属于某个特定对象的变量方法:只有某个特定对象才能调用的函数 对象和方法都用"."来访问: Object.property Object.method() 由 ...

  2. 安装Oracle数据库心得

    学到Oracle数据库了,想在自己电脑上安装个Oracle数据库.在网上下载了一个Oracle18c版 下边是我安装Oracle18c版的数据库失败,后来在卸载过程中遇到的问题: 1.用Univers ...

  3. jmeter的简单http接口用法

    1.  jmeter的启动:windows下的环境 进入jmeter的并目录双击启动 Mac电脑 进入bin目录找到jmeter.sh 文件 在终端执行./jmeter.sh 或者./jmeter. ...

  4. security cookie 机制(2)--- 初始化___security_cookie

    在 cookie 检查中,必定先要取出初始的 cookie 值: 0011392E A1 14 70 11 00       mov         eax,dword ptr [___securit ...

  5. MongoDB Sharding分片 shell 脚本

    #!/bin/sh CONFIG_NAME=$ CONFIG_PORT=$ SERIAL_NUM=$ STORAGE_HOME=$ if [ ! -n "$CONFIG_NAME" ...

  6. SQL优化经验

    SQL 优化经验总结34条   我们要做到不但会写SQL,还要做到写出性能优良的SQL,以下为笔者学习.摘录.并汇总部分资料与大家分享!   (1) 选择最有效率的表名顺序(只在基于规则的优化器中有效 ...

  7. Python中的@符号

    1.基本含义 @符号用做函数的修饰符,可以在模块或者类的定义层内对函数进行修饰,出现在函数定义的前一行,不允许和函数定义在同一行. 一个修饰符就是一个函数,它将被修饰的函数作为参数,并返回修饰后的同名 ...

  8. LAB1 partII

    PartII   实现单词统计 实现 main/wc.go 两个函数 mapF() . reduceF() 单词是任意字母连续序列, 由unicode.IsLetter 决定字母 测试数据 pg-*. ...

  9. Nodejs 中将html转换成pdf文件

    Nodejs 中将html转换成pdf文件,Nodejs Convert html into pdf 1. 下载phantomjs.exe,将该文件放在根目录 2. 编写pdf.js文件(在githu ...

  10. python3学习笔记11(函数)

    函数 python提供了许多内建函数,例如print(). 自己创建的函数,叫做用户自定义函数. 定义一个由自己想要功能的函数,以下是简单的规则: 函数代码块以 def 关键词开头,后接函数标识符名称 ...