Lock wait timeout exceeded数据库死锁问题

  • 环境

    MySQL5.5

  • 现象

    A.数据更新或新增后数据经常自动回滚。

    B.表操作总报 Lock wait timeout exceeded 并长时间无反应

  • 解决方法

    A.应急方法:show processlist; kill掉出现问题的进程

    B.根治方法:select * from innodb_trx 查看有是哪些事务占据了表资源。

C.我的方法:设置MySQL锁等待超时 innodb_lock_wait_timeout=50 ,autocommit=on

  • 该类问题导致原因

    据我分析,Mysql的 InnoDB存储引擎是支持事务的,事务开启后没有被主动Commit。导致该资源被长期占用,其他事务在抢占该资源时,因上一个事务的锁而导致抢占失败!因此出现 Lock wait timeout exceeded

    • MySQL 5.5 -- innodb_lock_wait 锁 等待

    • 记得以前,当出现:ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction,
      要解决是一件麻烦的事情 ;
      特别是当一个SQL执行完了,但未COMMIT,后面的SQL想要执行就是被锁,超时结束;
      DBA光从数据库无法着手找出源头是哪个SQL锁住了;
      有时候看看show engine innodb status , 并结合 show full processlist; 能暂时解决问题;但一直不能精确定位
    • 在5.5中,information_schema 库中增加了三个关于锁的表(MEMORY引擎);
      innodb_trx ## 当前运行的所有事务
      innodb_locks ## 当前出现的锁
      innodb_lock_waits ## 锁等待的对应关系
    • 看到这个就非常激动 ; 这可是解决了一个大麻烦,先来看一下表结构
    • root@127.0.0.1   : information_schema 13:28:38> desc innodb_locks;
      +-------------+---------------------+------+-----+---------+-------+
      | Field | Type | Null | Key | Default | Extra |
      +-------------+---------------------+------+-----+---------+-------+
      | lock_id | varchar(81) | NO | | | |#锁ID
      | lock_trx_id | varchar(18) | NO | | | |#拥有锁的事务ID
      | lock_mode | varchar(32) | NO | | | |#锁模式
      | lock_type | varchar(32) | NO | | | |#锁类型
      | lock_table | varchar(1024) | NO | | | |#被锁的表
      | lock_index | varchar(1024) | YES | | NULL | |#被锁的索引
      | lock_space | bigint(21) unsigned | YES | | NULL | |#被锁的表空间号
      | lock_page | bigint(21) unsigned | YES | | NULL | |#被锁的页号
      | lock_rec | bigint(21) unsigned | YES | | NULL | |#被锁的记录号
      | lock_data | varchar(8192) | YES | | NULL | |#被锁的数据
      +-------------+---------------------+------+-----+---------+-------+
      10 rows in set (0.00 sec)

      root@127.0.0.1   : information_schema 13:28:56> desc 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)

      root@127.0.0.1   : information_schema 13:29:05> desc 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 | |#innodb_locks.lock_id
      | trx_wait_started | datetime | YES | | NULL | |#事务开始等待的时间
      | trx_weight | bigint(21) unsigned | NO | | 0 | |#
      | trx_mysql_thread_id | bigint(21) unsigned | NO | | 0 | |#事务线程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 | |#事务锁住的内存大小(B)
      | 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 | |#
      +----------------------------+---------------------+------+-----+---------------------+-------+
      22 rows in set (0.01 sec)

    • 下面我们来动手看看数据吧:
      ##建立测试数据:
      use test;
      create table tx1
      (id int primary key ,
      c1 varchar(20),
      c2 varchar(30))
      engine=innodb default charset = utf8 ;
    • insert into tx1 values
      (1,‘aaaa‘,‘aaaaa2‘),
      (2,‘bbbb‘,‘bbbbb2‘),
      (3,‘cccc‘,‘ccccc2‘);
    • commit;
    • ###产生事务;
      ### Session1
      start transaction;
      update tx1 set c1=‘heyf‘,c2=‘heyf‘ where id =3 ;
    • ## 产生事务,在innodb_trx就有数据 ;
      root@127.0.0.1   : information_schema 13:38:21> select * from innodb_trx G
      *************************** 1. row ***************************
      trx_id: 3669D82
      trx_state: RUNNING
      trx_started: 2010-12-24 13:38:06
      trx_requested_lock_id: NULL
      trx_wait_started: NULL
      trx_weight: 3
      trx_mysql_thread_id: 2344
      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: 1
      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
      1 row in set (0.00 sec)
    • ### 由于没有产生锁等待,下面两个表没有数据 ;
      root@127.0.0.1   : information_schema 13:38:31> select * from innodb_lock_waits G
      Empty set (0.00 sec)
    • root@127.0.0.1   : information_schema 13:38:57> select * from innodb_locks G
      Empty set (0.00 sec)
    • #### 产生锁等待
      #### session 2
      start transaction;
      update tx1 set c1=‘heyfffff‘,c2=‘heyffffff‘ where id =3 ;
    • root@127.0.0.1   : information_schema 13:39:01> select * from innodb_trx G
      *************************** 1. row ***************************
      trx_id: 3669D83 ##第2个事务
      trx_state: LOCK WAIT ## 处于等待状态
      trx_started: 2010-12-24 13:40:07
      trx_requested_lock_id: 3669D83:49:3:4 ##请求的锁ID
      trx_wait_started: 2010-12-24 13:40:07
      trx_weight: 2
      trx_mysql_thread_id: 2346 ##线程 ID
      trx_query: update tx1 set c1=‘heyfffff‘,c2=‘heyffffff‘ where id =3
      trx_operation_state: starting index read
      trx_tables_in_use: 1 ##需要用到1个表
      trx_tables_locked: 1 ##有1个表被锁
      trx_lock_structs: 2
      trx_lock_memory_bytes: 376
      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: 10000
      *************************** 2. row ***************************
      trx_id: 3669D82 ##第1个事务
      trx_state: RUNNING
      trx_started: 2010-12-24 13:38:06
      trx_requested_lock_id: NULL
      trx_wait_started: NULL
      trx_weight: 3
      trx_mysql_thread_id: 2344
      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: 1
      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
      2 rows in set (0.00 sec)
    • root@127.0.0.1   : information_schema 13:40:12> select * from innodb_locks G
      *************************** 1. row ***************************
      lock_id: 3669D83:49:3:4 ## 第2个事务需要的锁
      lock_trx_id: 3669D83
      lock_mode: X
      lock_type: RECORD
      lock_table: `test`.`tx1`
      lock_index: `PRIMARY`
      lock_space: 49
      lock_page: 3
      lock_rec: 4
      lock_data: 3
      *************************** 2. row ***************************
      lock_id: 3669D82:49:3:4 ## 第1个事务需要的锁
      lock_trx_id: 3669D82
      lock_mode: X
      lock_type: RECORD
      lock_table: `test`.`tx1`
      lock_index: `PRIMARY`
      lock_space: 49
      lock_page: 3
      lock_rec: 4
      lock_data: 3
      2 rows in set (0.00 sec)
    • root@127.0.0.1   : information_schema 13:40:15> select * from innodb_lock_waits G
      *************************** 1. row ***************************
      requesting_trx_id: 3669D83 ## 请求锁的事务
      requested_lock_id: 3669D83:49:3:4 ## 请求锁的锁ID
      blocking_trx_id: 3669D82 ## 拥有锁的事务
      blocking_lock_id: 3669D82:49:3:4 ## 拥有锁的锁ID
      1 row in set (0.00 sec)
时间: 2025-01-13 01:59:59

Lock wait timeout exceeded数据库死锁问题的相关文章

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

前面已经了解了InnoDB关于在出现锁等待的时候,会根据参数innodb_lock_wait_timeout的配置,判断是否需要进行timeout的操作,本文档介绍在出现锁等待时候的查看及分析处理: 在InnoDB Plugin之前,一般通过show full processlist(很难发现被锁的行记录问题所在)和show engine innodb status命令查看当前的数据库请求,然后再判断当前事务中锁的情况.随着mysql的发展,已经提供更加便捷的方法来监控数据库中的锁等待现象了.

数据库 1205 Error 'Lock wait timeout exceeded; try restarting transaction' on query

收到报警,mysql的从数据库在同步的过程出现问题,已停止同步. mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.199.50 Master_User: replication Master_Port: 3306 Connect

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

问题描述: 研发突然找我,遇到了mysql的 ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction,具体报错如下: org.apache.hadoop.mapred.YarnChild.main(YarnChild.java:158) Caused by: java.io .IOException: java.sql.SQLException: Lock wait timeout exceeded;

mysql Lock wait timeout exceeded; try restarting transaction解决

前面已经了解了InnoDB关于在出现锁等待的时候,会根据参数innodb_lock_wait_timeout的配置,判断是否需要进行timeout的操作,本文档介绍在出现锁等待时候的查看及分析处理: 在InnoDB Plugin之前,一般通过show full processlist(很难发现被锁的行记录问题所在)和show engine innodb status命令查看当前的数据库请求,然后再判断当前事务中锁的情况.随着mysql的发展,已经提供更加便捷的方法来监控数据库中的锁等待现象了.

mysql执行insert等语句报1205 Lock wait timeout exceeded try restarting transaction

mysql执行insert等语句是报如下错误: 1205 Lock wait timeout exceeded try restarting transaction 解决办法如下: 方法一:查看当前线程,是否有存在正在执行的你相关的语句,将其KILL  show processlist;  kill pid 方法二(方法一不行再执行):重启mysql数据库  service mysqld restart 注意:重启mysql会将在执行的线程全部kill

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

在mysql innodb中使用事务,如果插入或者更新出错,一定要主动显式地执行rollback,否则可能产生不必要的锁而锁住其他的操作 我们在使用数据库的时候,可以使用contextlib,这样异常的时候自动回滚,而且最后都会执行关闭操作 from contextlib import contextmanager engine = create_engine(EREBUS_DB_CONNECT_STRING, echo=True, pool_size=150, max_overflow=50,

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

org.springframework.dao.CannotAcquireLockException 的解决> 直接上 bug 的详细信息: 2012-03-12 15:20:31 XmlBeanDefinitionReader [INFO] Loading XML bean definitions from class path resource [org/springframework/jdbc/support/sql-error-codes.xml] 2012-03-12 15:20:31

mysql异常Lock wait timeout exceeded; try restarting transaction

mysql中使用update语句更新数据报错: Lock wait timeout exceeded; try restarting transaction. 这是因为你要更新的表的锁在其他线程手里,如果没有意外的话,很可能是在事务线程中持有该锁,办法就是杀掉这条线程. 用   show full processlist 查询当前数据库所有线程 用    SELECT * FROM information_schema.INNODB_TRX   查询数据库事务表,其中trx_mysql_thre

【MySQL】事务没有提交导致 锁等待Lock wait timeout exceeded异常

异常:Lock wait timeout exceeded; try restarting transaction 解决办法:(需要数据库最高权限) 执行select * from information_schema.innodb_trx 之后找到了一个一直没有提交的只读事务, 找到对应的线程后,执行 kill thread id,再确认一直没有提交的只读事物被干掉了就OK了.