首页 技术 正文
技术 2022年11月19日
0 收藏 751 点赞 2,395 浏览 3888 个字

问题描述:

数据库从库停止同步。

问题分析:

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已经设置很大,而且即使设置很大,如果那边一直锁表,也是解决不了问题的。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,084
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,559
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,408
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,181
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,818
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,901