排查和解决:Lock wait timeout exceeded; try restarting transaction

一、问题如图:

排查和解决:Lock wait timeout exceeded; try restarting transaction_第1张图片

二、排查思路

1、什么问题引起这种报错?

某一个SQL或者大事务,将表或者行记录进行了加锁操作,然后长时间不释放锁。导致其他事务超时。

【UPDATE 、INSERT 都可能引起锁表】

2、解决方法一:修改超时时间

# 查询全局等待事务锁超时时间
SHOW GLOBAL VARIABLES LIKE 'innodb_lock_wait_timeout';

# 设置全局等待事务锁超时时间
SET  GLOBAL innodb_lock_wait_timeout=100;


# 查询当前会话等待事务锁超时时间
SHOW VARIABLES LIKE 'innodb_lock_wait_timeout';

3、解决方法二:排查事务的SQL

1、 找到超时的表,查询超时的SQL
SELECT * FROM INFORMATION_SCHEMA.INNODB_TRX WHERE trx_query LIKE '%表名%'

2、KILL 掉执行的线程

4、排查常用命令:

## 查看当前被使用的表
show OPEN TABLES where In_use > 0;
【SHOW OPEN TABLES:列举在表缓存中当前被打开的非TEMPORARY表。】
【In_use:表当前被查询使用的次数。如果该数为零,则表是打开的,但是当前没有被使用。】

##  
SELECT
    trx_id AS  `事务ID`,
    trx_state AS `事务状态`,
    trx_requested_lock_id  AS  `事务需要等待的资源`,
    trx_wait_started    AS  `事务开始等待时间`,
    trx_tables_in_use AS `事务使用表`,
    trx_tables_locked AS `事务拥有锁`,
    trx_rows_locked  AS `事务锁定行`,
    trx_rows_modified  AS `事务更改行`
FROM
    information_schema.innodb_trx ;

SELECT
    lock_id         AS  `锁ID`,
    lock_trx_id    AS  `拥有锁的事务ID`,
    lock_mode      AS  `锁模式    `,
    lock_type      AS  `锁类型`,
    lock_table         AS  `被锁的表`,
    lock_index         AS  `被锁的索引`,
    lock_space         AS  `被锁的表空间号`,
    lock_page      AS  `被锁的页号`,
    lock_rec       AS  `被锁的记录号`,
    lock_data      AS  `被锁的数据`
FROM
    information_schema.innodb_locks;


SELECT
  requesting_trx_id   AS  `请求锁的事务ID`,
  requested_lock_id   AS  `请求锁的锁ID`,
  blocking_trx_id     AS  `当前拥有锁的事务ID`,
  blocking_lock_id    AS  `当前拥有锁的锁ID`
FROM
  innodb_lock_waits;



 

 

 

 

 

 

参考文章:

  • https://www.cnblogs.com/xiluhua/p/9063660.html

  • https://www.cnblogs.com/wanderingfish/p/10838213.html

  • https://blog.csdn.net/wang_shuyu/article/details/83619613

 

你可能感兴趣的:(MySQL,mysql)