一、问题描述:

同事反馈线上一个表有其中一条数据无法删除,其他都正常,我拿到删数据的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表字段含义的解释:

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

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

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

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

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

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

mysql> select * from  information_schema.innodb_trx where trx_id=5316888834  \G
*************************** 1. row ***************************
trx_id: 5316888834
trx_state: RUNNING
trx_started: 2017-08-15 06:00:21
trx_requested_lock_id: NULL
trx_wait_started: NULL
trx_weight: 6
trx_mysql_thread_id: 1790259884
trx_query: NULL
trx_operation_state: NULL
trx_tables_in_use: 0
trx_tables_locked: 0
trx_lock_structs: 6
trx_lock_memory_bytes: 1184
trx_rows_locked: 10
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: 10000
trx_is_read_only: 0
trx_autocommit_non_locking: 0
1 row in set (0.00 sec)
 

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

#查看下这个事物发起的账号和主机信息
mysql> select * from information_schema.processlist where ID=1790259884;
+------------+----------+---------------------+--------+---------+------+-------+------+
| ID | USER | HOST | DB | COMMAND | TIME | STATE | INFO |
+------------+----------+---------------------+--------+---------+------+-------+------+
| 1790259884 | spider_w | 172.31.11.143:46120 | db_mta | Sleep | 1319 | | NULL |
+------------+----------+---------------------+--------+---------+------+-------+------+
1 row in set (0.01 sec)
#kill 这个未提交的事物线程ID
mysql> CALL mysql.rds_kill(1790259884);
Query OK, 0 rows affected (0.00 sec)
#删除数据
mysql> delete from facebook_posts where id = 7041232;
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. Thread.yield( )通俗理解

    一. Thread.yield( )方法: 使当前线程从执行状态(运行状态)变为可执行态(就绪状态).cpu会从众多的可执行态里选择,也就是说,当前也就是刚刚的那个线程还是有可能会被再次执行到的,并不 ...

  2. 【Jest】笔记三:全局变量

    一.前提 我们在使用unittest,testng框架的时候都知道,每个case都是项目独立的,上一个case返回的值是不能使用到下一个case的,但是实际中接口之间的关系是紧密相连的,这个时候我们怎 ...

  3. Int和String互转的方法

    Java 中int.String的类型转换   int -> String int i=12345;String s="";第一种方法:s=i+"";第二 ...

  4. vue cli 3.x的history 和 hash模式切换的问题

    使用vue cli 3.x 创建的项目,有一个选项:Use history mode for router? (Requires proper server setup for index fallb ...

  5. (C#)生成指定长度的随机字符串的通用方法

    .NET(C#)生成指定长度的随机字符串的通用方法,此方法可以指定字符串的长度,是否包含数字,是否包含符号,是否包含小写字母,是否包含大写字母等, 源码: #region 生成指定长度的随机字符串 / ...

  6. IIS的启动与停止命令

    IIS的启动与停止命令 IIS启动 net start w3svc IIS停止 net stop iisreset DOS下IIS服务命令: 可以通过iisreset /?命令来查看 iisreset ...

  7. 内存问题-JVM调优思路

    通常,内存的问题就是 GC 的问题,因为 Java 的内存由 GC 管理.有2种情况,一种是内存溢出了,一种是内存没有溢出,但 GC 不健康. 内存溢出的情况可以通过加上 -XX:+HeapDumpO ...

  8. C# 6.0:在catch和finally中使用await

    Asyn方法是一个现在很常用的方法,当使用async和await时,你或许曾有这样的经历,就是你想要在catch块或finally块中使用它们,比如当出现一个exception而你希望将日志记在文件或 ...

  9. keepalived+nginx实现niginx高可用,宕机自动重启

    nginx作为http服务器,在集群中 用于接受客户单发送过来的请求,并且根据配置的策略将请求 转发给具体的哪台服务器 如果在nginx服务器使用轮询策略处理客户端的请求,出现了tomcat 宕机的情 ...

  10. nvidia驱动自动更新版本后问题解决 -- failed to initialize nvml: driver/library version mismatch

    因为必须关闭桌面窗口, 建议另外一台电脑ssh连接操作 1. 卸载旧版本并关闭图形界面 sudo apt-get remove --purge nvidia-\* sudo service light ...