在用mysql客户端对数据库进行操做时,打开终端窗口,若是一段时间没有操做,再次操做时,经常会报以下错误:java
?mysql
1sql 2数据库 3服务器 |
|
这个报错信息就意味着当前的链接已经断开,须要从新创建链接。测试
那么,链接的时长是如何确认的?网站
其实,这个与interactive_timeout和wait_timeout的设置有关。
首先,看看官方文档对于这两个参数的定义
interactive_timeout
默认是28800,单位秒,即8个小时
1 |
|
wait_timeout
默认一样是28800s
The number of seconds the server waits for activity on a noninteractive connection before closing it.
On thread startup, the session wait_timeout value is initialized from the global wait_timeout value or from the global interactive_timeout value, depending on the type of client (as defined by the CLIENT_INTERACTIVE connect option to mysql_real_connect()). See also interactive_timeout.
根据上述定义,二者的区别显而易见
1> interactive_timeout针对交互式链接,wait_timeout针对非交互式链接。所谓的交互式链接,即在mysql_real_connect()函数中使用了CLIENT_INTERACTIVE选项。
说得直白一点,经过mysql客户端链接数据库是交互式链接,经过jdbc链接数据库是非交互式链接。
2> 在链接启动的时候,根据链接的类型,来确认会话变量wait_timeout的值是继承于全局变量wait_timeout,仍是interactive_timeout。
下面来测试一下,确认以下问题
1. 控制链接最大空闲时长的是哪一个参数。
2. 会话变量wait_timeout的继承问题
Q1:控制链接最大空闲时长的是哪一个参数
A1:wait_timeout
验证
只修改wait_timeout参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
能够看到,等待10s后再执行操做,链接已经断开。
只修改interactive_timeout参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
Q2:会话变量wait_timeout的继承问题
A2:若是是交互式链接,则继承全局变量interactive_timeout的值,若是是非交互式链接,则继承全局变量wait_timeout的值。
验证:
只修改全局变量interactive_timeout的值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
开启另一个mysql客户端,查看会话变量的值
1 2 3 4 5 6 7 8 |
|
发现,WAIT_TIMEOUT的值已经变为10了。
但经过jdbc测试,wait_timeout的值依旧是28800
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
结果输出以下:
INTERACTIVE_TIMEOUT: 10
WAIT_TIMEOUT: 28800
只修改全局变量wait_timeout的值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
开启另一个mysql客户端,查看会话变量的值
1 2 3 4 5 6 7 8 |
|
WAIT_TIMEOUT的值依旧是28800.
查看jdbc的结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
查看jdbc的结果
1 2 |
|
同时,新增了一段程序,等待20s后,再次执行查询,报以下错误:
Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
总结
1. 控制链接最大空闲时长的wait_timeout参数。
2. 对于非交互式链接,相似于jdbc链接,wait_timeout的值继承自服务器端全局变量wait_timeout。
对于交互式链接,相似于mysql客户单链接,wait_timeout的值继承自服务器端全局变量interactive_timeout。
3. 判断一个链接的空闲时间,可经过show processlist输出中Sleep状态的时间
1 2 3 4 5 6 7 8 |
|
以上所述是小编给你们介绍的MySQL中interactive_timeout和wait_timeout的区别,但愿对你们有所帮助,若是你们有任何疑问请给我留言,小编会及时回复你们的。在此也很是感谢你们对脚本之家网站的支持!