连接的超时时间
set global interactive_timeout=120;
set global wait_timeout=120;
该连接指类似应用访问数据库的连接,可以查询、DML、DDL等
测试一:超时后,DML事务断开,相当于事务提交失败,原数据无变化
mysql> select sysdate();begin;update test set tvalue=‘123‘ where test_id=5; +---------------------+ | sysdate() | +---------------------+ | 2018-09-22 11:08:45 | +---------------------+ 1 row in set (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select sysdate();commit; ERROR 2006 (HY000): MySQL server has gone away No connection. Trying to reconnect... Connection id: 344 Current database: vodb +---------------------+ | sysdate() | +---------------------+ | 2018-09-22 11:10:47 | +---------------------+ 1 row in set (0.00 sec) Query OK, 0 rows affected (0.00 sec) mysql> select test_id,tvalue from test; +---------+-----------------------------------------------------------------------------+ | test_id | tvalue | +---------+-----------------------------------------------------------------------------+ | 1 | 有张有驰有分寸0 | | 2 | 适当休息有利于提高工作效率 | | 3 | 人类对事物的看法达成一致时,可以形成精神层面的共鸣 | | 4 | 有张有驰有分寸3 | | 5 | 456 | +---------+-----------------------------------------------------------------------------+ 5 rows in set (0.00 sec)
注意:超时后会出现,下面的信息,后面的连接是客户端自己又重新连接的,事务已经失效了
ERROR 2006 (HY000): MySQL server has gone away
下面是在连接120时间内提交,事务就会成功
mysql> select test_id,tvalue from test; +---------+-----------------------------------------------------------------------------+ | test_id | tvalue | +---------+-----------------------------------------------------------------------------+ | 1 | 有张有驰有分寸0 | | 2 | 适当休息有利于提高工作效率 | | 3 | 人类对事物的看法达成一致时,可以形成精神层面的共鸣 | | 4 | 有张有驰有分寸3 | | 5 | 456 | +---------+-----------------------------------------------------------------------------+ 5 rows in set (0.00 sec) mysql> mysql> mysql> select sysdate();begin;update test set tvalue=‘123‘ where test_id=5; +---------------------+ | sysdate() | +---------------------+ | 2018-09-22 11:32:55 | +---------------------+ 1 row in set (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select sysdate();commit; +---------------------+ | sysdate() | +---------------------+ | 2018-09-22 11:34:14 | +---------------------+ 1 row in set (0.00 sec) Query OK, 0 rows affected (0.09 sec) mysql> select test_id,tvalue from test; +---------+-----------------------------------------------------------------------------+ | test_id | tvalue | +---------+-----------------------------------------------------------------------------+ | 1 | 有张有驰有分寸0 | | 2 | 适当休息有利于提高工作效率 | | 3 | 人类对事物的看法达成一致时,可以形成精神层面的共鸣 | | 4 | 有张有驰有分寸3 | | 5 | 123 | +---------+-----------------------------------------------------------------------------+ 5 rows in set (0.00 sec)
参数的设置需要需要下面两个参数一起设置,注意查看是否生效的时候,要使用show global variables like ‘%timeout‘;
set global interactive_timeout=120;
set global wait_timeout=120;
测试二:wait_timeout是空闲连接的超时时间,如果一个连接正在query,而不是sleep状态,则超时也不会断开
set global interactive_timeout=60;
set global wait_timeout=60;
set interactive_timeout=60;
set wait_timeout=60;
test表有5万数据,正在不断插入数据,自连接进行笛卡尔集查询,时间远远超过了连接的超时设置60,但连接并没有断开,所以这里的连接指的是空连接;
mysql> select count(aa.tvalue) from test aa,test bb where aa.tid=bb.tid ; +------------------+ | count(aa.tvalue) | +------------------+ | 50015 | +------------------+ 1 row in set (2 min 58.55 sec)
DDL锁超时时间
mysql> set global lock_wait_timeout=5; Query OK, 0 rows affected (0.11 sec) mysql> mysql> mysql> show global variables like ‘%lock_wait%‘; +--------------------------+-------+ | Variable_name | Value | +--------------------------+-------+ | innodb_lock_wait_timeout | 50 | | lock_wait_timeout | 5 | +--------------------------+-------+ 2 rows in set (0.51 sec)
修改DDL超时时间为5秒,结果很悲剧,5秒时该DDL未执行完,mysql实例直接关闭了,这个参数的默认时间31536000秒(365天),最好还是不要改动了
mysql> alter table test drop column beizhu;
ERROR 2013 (HY000): Lost connection to MySQL server during query
DML事务锁超时时间
set global innodb_lock_wait_timeout=30;
set innodb_lock_wait_timeout=30;
一个innodb事务等待一个row lock的秒数,超时这个秒数,则该innodb事务就不再等了,直接回退了。此处设置为30秒,实验如下
会话一,事务持续超过了30秒,在这中间执行了会话二
mysql> select sysdate();begin;update test set tvalue=‘一些小的痛苦可以引人思考,让人成长;但长时间深陷痛苦之中,则会伤害身心‘ where test_id=4; +---------------------+ | sysdate() | +---------------------+ | 2018-09-22 22:13:25 | +---------------------+ 1 row in set (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select sysdate();commit; +---------------------+ | sysdate() | +---------------------+ | 2018-09-22 22:14:03 | +---------------------+ 1 row in set (0.00 sec) Query OK, 0 rows affected (0.01 sec)
会话二,在会话一执行的中间执行,在第31秒时,出现了 lock wait timeout,然后原数据并未变化,也就是说事务回退了
mysql> select sysdate();update test set tvalue=‘小痛助长,大痛伤身‘ where test_id=4; +---------------------+ | sysdate() | +---------------------+ | 2018-09-22 22:13:30 | +---------------------+ 1 row in set (0.00 sec) ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction mysql> select tid,tvalue from test where test_id<6; +------+-----------------------------------------------------------------------------------------------------------+ | tid | tvalue | +------+-----------------------------------------------------------------------------------------------------------+ | 0 | 有张有驰有分寸0 | | 1 | 适当休息有利于提高工作效率 | | 2 | 人类对事物的看法达成一致时,可以形成精神层面的共鸣 | | 3 | 一些小的痛苦可以引人思考,让人成长;但长时间深陷痛苦之中,则会伤害身心 | | 4 | 123 | +------+-----------------------------------------------------------------------------------------------------------+ 5 rows in set (0.00 sec)
注意,这是说一个事务等待一个row lock的最大时间,但是事务本身需要很久的时间是没有问题的,是可以执行成功的;同时也说明,一个长事务有让其他的事务失效回退的可能。本例是Mysql7.2.22,RR事务隔离级别。
test表已有120万数据量(还在不断插入数据中),让它自己跟自己笛卡尔一下就能产生个长事务,test_id是索引字段,tid上无索引,这是在全表所有row的tid列上加锁
mysql> update test aa,test bb set aa.tid=bb.test_id where bb.tid = aa.tid;
下面的这个事务在等待30秒后,就自动回退了,这就是长事务阻断了其他事务的情况
mysql> update test aa set aa.tid = 7 where aa.tid=8;
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
原文地址:https://www.cnblogs.com/perfei/p/9689741.html