OMG写的时候崩溃了一次。

触发关注这个问题的事情是 我们在使用pt-online-schedule 改表的时候总是拿不到锁,并且报出mysql innodb Lock wait timeout exceeded; try restarting transaction的问题,所以才想到要排查。

首先最先想到的肯定是

show processlist;

来查看当前正在运行的查询 或者等待休眠中的查询是哪些,包括使用了多少时间等,类似

*************************** 582. row ***************************
Id: 7485594
User: xcf
Host: cc:59580
db: xx
Command: Query
Time: 0
State: init
Info: show processlist
Rows_sent: 0
Rows_examined: 0
*************************** 583. row ***************************
Id: 7485595
User: xcf
Host: cc-19:42614
db: xx
Command: Sleep
Time: 0
State:
Info: NULL
Rows_sent: 0
Rows_examined: 0

但是可以看到,当有很多任务在执行的时候,processlist将会非常大,想要从这里面获取对我们有用的信息非常非常困难,最多也就简单的看下,哪个query耗时的确过长,会不会是因为事务没有提交导致的拿不到锁,来推测并且使用kill task_id来杀掉相应的task。

其次我们如果有权限,可以使用能看到相对来说更为复杂和详细的信息

SHOW ENGINE INNODB STATUS\G;

通过innodb status 提供的详细的系统情况来分析问题。

如果我没没有使用show engine innodb status的权限,退而求其次我们可以使用另外一种思路来找到是哪个表持续被锁,导致拿不到锁的问题。

show open tables where in_use>0;

查看现在系统正在使用的表,然后使用

show processlist;

查找正在query该表的任务,查看代码是否有一直没有提交事物,却没有commit的代码,用这个思路来找问题出现在哪儿。

另外在mysql5.5之后,information_schema数据库加了三个关于锁的表。

innodb_trx ## 当前运行的所有事务
innodb_locks ## 当前出现的锁
innodb_lock_waits ## 锁等待的对应关系

备忘一下他们的表结构

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
| 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 | |
+----------------------------+---------------------+------+-----+---------------------+-------+

这个表对于排查因为事务未提交引起的锁问题可以说是举足轻重。当我们有事务长时间未提交导致锁住数据库,其他程序拿不到锁的时候,因为对这张表进行排查。

比如我们获取一条记录的线程id, 即可拿着该线程id去information_schma.processlist中获取他的具体情况。

mysql> select * from information_schema.processlist where id=701520;
+--------+------+----------------+-----------+---------+------+-------+------+---------+-----------+---------------+
| ID | USER | HOST | DB | COMMAND | TIME | STATE | INFO | TIME_MS | ROWS_SENT | ROWS_EXAMINED |
+--------+------+----------------+-----------+---------+------+-------+------+---------+-----------+---------------+
| 701520 | ppp | hazelnut:50308 | xxxxxxxxx | Sleep | 5492 | | NULL | 5492065 | 0 | 0 |
+--------+------+----------------+-----------+---------+------+-------+------+---------+-----------+---------------+

然后找到在占用服务器50308端口的程序

netstat -nlatp |grep 50308
piperck@grape:~$ netstat -nlatp | grep 46698
(No info could be read for "-p": geteuid()=1025 but you should be root.)
tcp 0 0 192.168.2.79:46698 192.168.2.83:3306 ESTABLISHED -

可以看到协议 本机端口 去往mysql 也就是我们起初的数据库, 这里 established后面 本正常会显示占用程序的pid -p参数可以将其显示出来。

接下来看下记录锁信息的表 innodb_locks

mysql> desc information_schema.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 | |  # 被锁的数据
+-------------+---------------------+------+-----+---------+-------+

如果 我们要排查的问题正锁死我们的某张表,那么该表的数据表就会有所体现。同时和这个表使用的 还有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
+-------------------+-------------+------+-----+---------+-------+

结合以上两个表再查对应的信息  可以说已经很方便了。那天出问题,我回家的时候就已经被解决了。我并没有在线上环境尝试过,但是模拟了几次,使用这些办法提供的线索都能解决问题,等我有机会解决线上问题的时候,再补一个详细例子。

Reference:

http://stackoverflow.com/questions/5836623/getting-lock-wait-timeout-exceeded-try-restarting-transaction-even-though-im  getting-lock-wait-timeout-exceeded-try-restarting-transaction-even-though-im 
http://blog.csdn.net/hw_libo/article/details/39080809  MySQL锁阻塞分析
http://blog.sina.com.cn/s/blog_6bb63c9e0100s7cb.html  MySQL innodb_lock_wait 锁等待
http://dev.mysql.com/doc/refman/5.6/en/innodb-information-schema-understanding-innodb-locking.html  mysql5.6官方文档
https://stackoverflow.com/questions/37306568/how-to-solve-mysql-innodb-waiting-for-table-metadata-lock-on-truncate-table?rq=1    metadata_lock 锁的问题
 

排查mysql innodb Lock wait timeout exceeded; try restarting transaction的问题的更多相关文章

  1. mysql异常Lock wait timeout exceeded; try restarting transaction

    mysql中使用update语句更新数据报错: Lock wait timeout exceeded; try restarting transaction. 这是由于你要更新的表的锁在其它线程手里. ...

  2. mysql 异常 Lock wait timeout exceeded; try restarting transaction;expc=java.sql.SQLExcept

    这种一般是等锁超时了,可以设置延长等锁时间. mysql> set innodb_lock_wait_timeout=100 Query OK, 0 rows affected (0.02 se ...

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

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

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

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

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

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

  6. MySQL应用报错:java.sql.SQLException: Lock wait timeout exceeded; try restarting transaction

    开发反馈,某业务系统插入一条记录的时候,日志报错,插入失败: ### Error updating database. Cause: java.sql.SQLException: Lock wait ...

  7. com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Lock wait timeout exceeded; try restarting transaction 问题解决

    有两种设置方法 第一种在mysql的配置文件中加入,然后重启mysql innodb_lock_wait_timeout = 500 第二种直接执行如下命令 set global innodb_loc ...

  8. MySQL事务锁等待超时 Lock wait timeout exceeded; try restarting transaction

    工作中处理定时任务分发消息时出现的问题,在查找并解决问题的时候,将相关的问题博客收集整理,在此记录下,以便之后再遇到相同的问题,方便查阅. 问题场景 问题出现的场景: 在消息队列处理消息时,同一事务内 ...

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

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

随机推荐

  1. ethereum/EIPs-155 Simple replay attack protection 35,36

    EIP 155:重放攻击保护——防止了在一个以太坊链上的交易被重复广播到另外一条链. 在看椭圆曲线时有提到,与r.s.v中的v相关 不同的共有链定义不同的chainId, 防止同一笔交易在不同的共有链 ...

  2. linux连接iscsi存储方法

    当前存储openfiler   IP为 192.168.221.99  端口为3260 安装.启动iscsi rpm包  并改为开机自动运行 探测存储服务器 iscsiadm -m discovery ...

  3. pycharm 取消 rebase 操作

    291/5000 取消rebase操作从主菜单中选择VCS | Git | 中止重新定位(abrot rebasing)如果rebase在两个或多个本地存储库中启动,则会显示“中止重新排序”对话框. ...

  4. 【小程序】当遇到bindTap绑定无法跳转到tabbar页面时

    如下图: 更换成navigator包裹跳转也不起作用. cart目录在app.json中定义在底部tabBar中 在小程序 导航 文档 最下方表示 所以,以上应改为

  5. Nowcoder186C 失衡天平 背包

    题目传送门 题意:给你$N$个数,你可以将其划分为若干对集合(这里所说的集合允许数字重复)(即集合是两个集合两个集合出现),每对集合中两个集合所有元素的和的差的绝对值不超过$M$,可以有数字不在集合内 ...

  6. 对int array进行排序

    今天再学习一些C#的基础知识,如对 Int Array进行排序: 你可以在控制台应用程序中,创建一个类别,它属性和2个构造函数: class Af { private int[] myVar; pub ...

  7. redis 配置 架构 基础

    redis 官网  redis.io   io为某国家域名后缀 有redis各种版本. java 版本 又分各种工具 clients 下 RedisClient为图形化管理界面 Jedis 才是jav ...

  8. Ionic2 App Import BrowserAnimationsModule or NoopAnimationsModule问题

    在开发app的过程中遇见了动画相关方面的问题,具体如下: 解决方法是:在app.module.ts模块中引入BrowserAnimationsModule import { BrowserAnimat ...

  9. vue2.0中使用pug(jade)

    第一部分:pug(jade)模板引擎 pug,原名jade,是流行的HTML模板引擎,它是HAML在JavaScript上的实现,最大的特色是使用缩进排列替代成对标签. 它简化了HTML的成对标签的写 ...

  10. Docker容器学习梳理 - 基础环境安装

    以下是centos系统安装docker的操作记录 1)第一种方法:采用系统自带的docker安装,但是这一般都不是最新版的docker安装epel源[root@docker-server ~]# wg ...