问题描述:
数据库从库停止同步。
问题分析:
show slave status\G;(也可使用show full processlist)
显示 某个update语句出错,Lock wait timeout exceeded; try restarting transaction;
原因是这条语句提交的时候超时堵塞。原因在于另外的一个操作开启了事务,锁定了相应的数据,导致这条操作相同数据的sql出错。
示例,在sql中
(1)开启事务,锁定数据
终端A:
MySQL> begin; (开启事务,开启事务会锁定相关数据)
Query OK, 0 rows affected (0.00 sec)
mysql> update pet set sex="m" where name="Fluffy";(修改)
Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from pet;
+--------+--------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+--------+--------+---------+------+------------+------------+
| Bowser | Diane | dog | m | 1979-08-31 | 1995-07-29 |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Fang | Benny | dog | m | 2013-02-05 | NULL |
| Fluffy | Harold | cat | m | 2012-09-30 | NULL |
+--------+--------+---------+------+------------+------------+
5 rows in set (0.00 sec)
终端B:
mysql> select * from pet;
+--------+--------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+--------+--------+---------+------+------------+------------+
| Bowser | Diane | dog | m | 1979-08-31 | 1995-07-29 |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Fang | Benny | dog | m | 2013-02-05 | NULL |
| Fluffy | Harold | cat | NULL | 2012-09-30 | NULL |
+--------+--------+---------+------+------------+------------+
5 rows in set (0.01 sec)
因为没有提交,所以实际上没有改变。但是会锁定Fluffy这一条数据。导致如下的错误。
终端B:
mysql> update pet set sex="m" where name="Fluffy";
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
(2)提交事务,解锁数据
终端A:
mysql> commit;
Query OK, 0 rows affected (0.10 sec)
终端B:
mysql> select * from pet;
+--------+--------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+--------+--------+---------+------+------------+------------+
| Bowser | Diane | dog | m | 1979-08-31 | 1995-07-29 |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Fang | Benny | dog | m | 2013-02-05 | NULL |
| Fluffy | Harold | cat | m | 2012-09-30 | NULL |
+--------+--------+---------+------+------------+------------+
5 rows in set (0.00 sec)
mysql> update pet set sex="f" where name="Fluffy";
Query OK, 1 row affected (0.08 sec)
Rows matched: 1 Changed: 1 Warnings: 0
(3)如果where中的条件是索引字段,那么只会锁定索引对应的条目;如果不是索引字段,那么会锁定整张表。
终端A:
mysql> update pet set sex="f" where death="1995-07-29";
Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from pet;
+--------+--------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+--------+--------+---------+------+------------+------------+
| Bowser | Diane | dog | f | 1979-08-31 | 1995-07-29 |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Fang | Benny | dog | m | 2013-02-05 | NULL |
| Fluffy | Harold | cat | f | 2012-09-30 | NULL |
+--------+--------+---------+------+------------+------------+
5 rows in set (0.00 sec)
终端B:
mysql> select * from pet;
+--------+--------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+--------+--------+---------+------+------------+------------+
| Bowser | Diane | dog | m | 1979-08-31 | 1995-07-29 |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Fang | Benny | dog | m | 2013-02-05 | NULL |
| Fluffy | Harold | cat | f | 2012-09-30 | NULL |
+--------+--------+---------+------+------------+------------+
5 rows in set (0.00 sec)
mysql> update pet set death="2013-07-00" where birth="2013-02-05";
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
解决办法
(1) stop slave; start slave
(2)网上分析说
Mysql ‘Lock wait timeout exceeded; try restarting transaction‘ 解决方案
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
Temporary error: 266: Time-out in NDB, probably caused by deadlock 解决方法:在管理节点的[ndbd default]
区加:
TransactionDeadLockDetectionTimeOut=10000(设置为10秒)默认是1200(1.2秒)按照顺序重新启动各个节点就不会出现问题了。
不过,本身貌似数据库的TransactionDeadLockDetectionTimeOut已经设置很大,而且即使设置很大,如果那边一直锁表,也是解决不了问题的。