MySQL之wait_timeout和interactive_timeout参数
引言
在用mysql客户端对数据库进行操作时,打开终端窗口,如果一段时间没有操作,再次操作时,常常会报如下错误:
ERROR 2013 (HY000): Lost connection to MySQL server during query ERROR 2006 (HY000): MySQL server has gone away No connection. Trying to reconnect...
这个报错信息就意味着当前的连接已经断开,需要重新建立连接。那么,连接的时长是多长?如何确认和配置?
相关参数
引言中连接时长和参数interactive_timeout和wait_timeout的设置有关。
The number of seconds the server waits for activity on an interactive connection before closing it. An interactive client is defined as a client that uses the CLIENT_INTERACTIVE option to mysql_real_connect(). See also wait_timeout.
interactive_timeout针对交互式连接,wait_timeout针对非交互式连接。所谓的交互式连接,即在mysql_real_connect()函数中使用了CLIENT_INTERACTIVE选项。说得直白一点,通过mysql客户端连接数据库是交互式连接,通过jdbc连接数据库是非交互式连接。
默认值:28800,单位秒,即8个小时
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.
服务器关闭非交互连接之前等待活动的秒数。在线程启动时,根据全局wait_timeout值或全局interactive_timeout值初始化会话wait_timeout值,取决于客户端类型(由mysql_real_connect()的连接选项CLIENT_INTERACTIVE定义)。
默认值:28800,单位秒,即8个小时
控制连接最大空闲时长的参数及验证
控制连接最大空闲时长的参数是: wait_timeout
验证: 1. 只修改wait_timeout参数
mysql> select variable_name,variable_value from information_schema.session_variables where variable_name in ('interactive_timeout','wait_timeout'); +---------------------+----------------+ | variable_name | variable_value | +---------------------+----------------+ | INTERACTIVE_TIMEOUT | 28800 | | WAIT_TIMEOUT | 28800 | +---------------------+----------------+ rows in set (0.03 sec) mysql> set session WAIT_TIMEOUT=10; Query OK, 0 rows affected (0.00 sec) -------等待10s后再执行 mysql> select variable_name,variable_value from information_schema.session_variables where variable_name in ('interactive_timeout','wait_timeout'); ERROR 2013 (HY000): Lost connection to MySQL server during query
mysql> select variable_name,variable_value from information_schema.session_variables where variable_name in ('interactive_timeout','wait_timeout'); +---------------------+----------------+ | variable_name | variable_value | +---------------------+----------------+ | INTERACTIVE_TIMEOUT | 28800 | | WAIT_TIMEOUT | 28800 | +---------------------+----------------+ rows in set (0.03 sec) mysql> set session WAIT_TIMEOUT=10; Query OK, 0 rows affected (0.00 sec) -------等待10s后再执行 mysql> select variable_name,variable_value from information_schema.session_variables where variable_name in ('interactive_timeout','wait_timeout'); ERROR 2013 (HY000): Lost connection to MySQL server during query
可以看到,等待10s后再执行操作,连接已经断开。
验证: 2. 只修改interactive_timeout参数 **
mysql> select variable_name,variable_value from information_schema.session_variables where variable_name in ('interactive_timeout','wait_timeout'); +---------------------+----------------+ | variable_name | variable_value | +---------------------+----------------+ | INTERACTIVE_TIMEOUT | 28800 | | WAIT_TIMEOUT | 28800 | +---------------------+----------------+ rows in set (0.06 sec) mysql> set session INTERACTIVE_TIMEOUT=10; Query OK, 0 rows affected (0.00 sec) ----------等待10s后执行 mysql> select variable_name,variable_value from information_schema.session_variables where variable_name in ('interactive_timeout','wait_timeout'); +---------------------+----------------+ | variable_name | variable_value | +---------------------+----------------+ | INTERACTIVE_TIMEOUT | 10 | | WAIT_TIMEOUT | 28800 | +---------------------+----------------+ rows in set (0.06 sec)
mysql> select variable_name,variable_value from information_schema.session_variables where variable_name in ('interactive_timeout','wait_timeout'); +---------------------+----------------+ | variable_name | variable_value | +---------------------+----------------+ | INTERACTIVE_TIMEOUT | 28800 | | WAIT_TIMEOUT | 28800 | +---------------------+----------------+ rows in set (0.06 sec) mysql> set session INTERACTIVE_TIMEOUT=10; Query OK, 0 rows affected (0.00 sec) ----------等待10s后执行 mysql> select variable_name,variable_value from information_schema.session_variables where variable_name in ('interactive_timeout','wait_timeout'); +---------------------+----------------+ | variable_name | variable_value | +---------------------+----------------+ | INTERACTIVE_TIMEOUT | 10 | | WAIT_TIMEOUT | 28800 | +---------------------+----------------+ rows in set (0.06 sec)
可以看到,等待10s后再执行操作,连接没有断开。
会话变量wait_timeout的继承问题
验证1: 只修改全局变量interactive_timeout的值
1. 交互式连接修改INTERACTIVE_TIMEOUT值
- 打开一个Mysql客户端修改INTERACTIVE_TIMEOUT值
mysql> select variable_name,variable_value from information_schema.global_variables where variable_name in ('interactive_timeout','wait_timeout'); +---------------------+----------------+ | variable_name | variable_value | +---------------------+----------------+ | INTERACTIVE_TIMEOUT | 28800 | | WAIT_TIMEOUT | 28800 | +---------------------+----------------+ rows in set (0.13 sec) mysql> set global INTERACTIVE_TIMEOUT=10; Query OK, 0 rows affected (0.00 sec) mysql> select variable_name,variable_value from information_schema.global_variables where variable_name in ('interactive_timeout','wait_timeout'); +---------------------+----------------+ | variable_name | variable_value | +---------------------+----------------+ | INTERACTIVE_TIMEOUT | 10 | | WAIT_TIMEOUT | 28800 | +---------------------+----------------+ rows in set (0.00 sec)
- 开启另外一个mysql客户端,查看会话变量的值
mysql> select variable_name,variable_value from information_schema.session_variables where variable_name in ('interactive_timeout','wait_timeout'); +---------------------+----------------+ | variable_name | variable_value | +---------------------+----------------+ | INTERACTIVE_TIMEOUT | 10 | | WAIT_TIMEOUT | 10 | +---------------------+----------------+ rows in set (0.00 sec)
WAIT_TIMEOUT的值已经变为10,继承INTERACTIVE_TIMEOUT的值。
public class Jdbc_test { @SuppressWarnings("static-access") public static void main(String[] args) throws Exception { Connection conn = null; Statement stmt = null; ResultSet rs = null; String url = "jdbc:mysql://192.168.244.10:3306/test"; String user = "root"; String password = "123456"; Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(url, user, password); stmt = conn.createStatement(); String sql = "select variable_name,variable_value from information_schema.session_variables where variable_name in ('interactive_timeout','wait_timeout')"; rs = stmt.executeQuery(sql); while (rs.next()) { System.out .println(rs.getString(1)+": "+rs.getString(2)); } } }
输出结果
INTERACTIVE_TIMEOUT: 10 WAIT_TIMEOUT: 28800
wait_timeout的值依旧是28800,没有继承INTERACTIVE_TIMEOUT的值
验证2: 只修改全局变量wait_timeout的值
1. 交互式连接修改wait_timeout值
- 打开一个Mysql客户端修改wait_timeout值
mysql> select variable_name,variable_value from information_schema.global_variables where variable_name in ('interactive_timeout','wa it_timeout');+---------------------+----------------+ | variable_name | variable_value | +---------------------+----------------+ | INTERACTIVE_TIMEOUT | 28800 | | WAIT_TIMEOUT | 28800 | +---------------------+----------------+ rows in set (0.17 sec) mysql> set global WAIT_TIMEOUT=20; Query OK, 0 rows affected (0.07 sec) mysql> select variable_name,variable_value from information_schema.global_variables where variable_name in ('interactive_timeout','wa it_timeout');+---------------------+----------------+ | variable_name | variable_value | +---------------------+----------------+ | INTERACTIVE_TIMEOUT | 28800 | | WAIT_TIMEOUT | 20 | +---------------------+----------------+ rows in set (0.00 sec)
- 开启另外一个mysql客户端,查看会话变量的值
mysql> select variable_name,variable_value from information_schema.session_variables where variable_name in ('interactive_timeout','wait_timeout'); +---------------------+----------------+ | variable_name | variable_value | +---------------------+----------------+ | INTERACTIVE_TIMEOUT | 28800 | | WAIT_TIMEOUT | 28800 | +---------------------+----------------+ rows in set (0.03 sec)
wait_timeout的值依旧是28800,没有继承刚才设置的WAIT_TIMEOUT值
public class Jdbc_test { @SuppressWarnings("static-access") public static void main(String[] args) throws Exception { Connection conn = null; Statement stmt = null; ResultSet rs = null; String url = "jdbc:mysql://192.168.244.10:3306/test"; String user = "root"; String password = "123456"; Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(url, user, password); stmt = conn.createStatement(); String sql = "select variable_name,variable_value from information_schema.session_variables where variable_name in ('interactive_timeout','wait_timeout')"; rs = stmt.executeQuery(sql); while (rs.next()) { System.out .println(rs.getString(1)+": "+rs.getString(2)); } Thread.currentThread().sleep(21000); sql = "select 1 from dual"; rs = stmt.executeQuery(sql); while (rs.next()) { System.out .println(rs.getInt(1)); } } }
public class Jdbc_test { @SuppressWarnings("static-access") public static void main(String[] args) throws Exception { Connection conn = null; Statement stmt = null; ResultSet rs = null; String url = "jdbc:mysql://192.168.244.10:3306/test"; String user = "root"; String password = "123456"; Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(url, user, password); stmt = conn.createStatement(); String sql = "select variable_name,variable_value from information_schema.session_variables where variable_name in ('interactive_timeout','wait_timeout')"; rs = stmt.executeQuery(sql); while (rs.next()) { System.out .println(rs.getString(1)+": "+rs.getString(2)); } Thread.currentThread().sleep(21000); sql = "select 1 from dual"; rs = stmt.executeQuery(sql); while (rs.next()) { System.out .println(rs.getInt(1)); } } }
输出结果
INTERACTIVE_TIMEOUT: 28800 WAIT_TIMEOUT: 20
同时,新增了一段程序,等待20s后,再次执行查询,报如下错误:
Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure Last packet sent to the server was 12 ms ago. at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at com.mysql.jdbc.Util.handleNewInstance(Util.java:406) at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074) at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3009) at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2895) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3438) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1951) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2101) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2548) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2477) at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1422) at com.victor_01.Jdbc_test.main(Jdbc_test.java:29) Caused by: java.net.SocketException: Software caused connection abort: recv failed at java.net.SocketInputStream.socketRead0(Native Method) at java.net.SocketInputStream.socketRead(Unknown Source) at java.net.SocketInputStream.read(Unknown Source) at java.net.SocketInputStream.read(Unknown Source) at com.mysql.jdbc.util.ReadAheadInputStream.fill(ReadAheadInputStream.java:113) at com.mysql.jdbc.util.ReadAheadInputStream.readFromUnderlyingStreamIfNecessary(ReadAheadInputStream.java:160) at com.mysql.jdbc.util.ReadAheadInputStream.read(ReadAheadInputStream.java:188) at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:2452) at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2906) ... 8 more
wait_timeout的变为20,继承刚才设置的WAIT_TIMEOUT值
总结
- 控制连接最大空闲时长的wait_timeout参数。
- 对于非交互式连接,类似于jdbc连接,wait_timeout的值继承自服务器端全局变量wait_timeout。对于交互式连接,类似于mysql客户单连接,wait_timeout的值继承自服务器端全局变量interactive_timeout。
- 判断一个连接的空闲时间,可通过show processlist输出中Sleep状态的时间
mysql> show processlist; +----+------+----------------------+------+---------+------+-------+------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+----------------------+------+---------+------+-------+------------------+ | 2 | root | localhost | NULL | Query | 0 | init | show processlist | | 6 | repl | 192.168.244.20:44641 | NULL | Sleep | 1154 | | NULL | +----+------+----------------------+------+---------+------+-------+------------------+ rows in set (0.03 sec)
参考
- https://www.cnblogs.com/ivictor/p/5979731.html
- http://www.cnblogs.com/Alight/p/4118515.html
- http://www.cnblogs.com/jiunadianshi/articles/2475475.html

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
python之多线程
注:本文是廖大的教程文章,本人也在学习,因为老是记不住,自己手打一边,代码也是亲自测试。廖大传送门 多进程 多个任务可以由多进程完成,也可以由一个进程内的多线程完成。 一个线程由多个进程组成,一个进程至少有一个线程。 由于线程是操作系统直接支持的单元,因此,高级语言都内置多线程的支持,python 也不例外,并且,python 的线程是真正的 Posix Thread ,不是模拟出来的线程。 python 的标准库提供了两个模块:_thread 和 threading ,_thread 是低级模块,threading 是高级模块。绝大多数的情况下,我们只用 threading 就可以了。 启动一个线程就是把函数传入并创建 Thread 实例,然后调用 start() 函开始执行就可以了。 import time import threading #线程执行的代码 def loop(): print('thread %s is running' % threading.current_thread().name) n = 0 while n < 5: n += 1 print('t...
- 下一篇
Java编程笔记,spring boot常用属性汇总
这些属性是否生效取决于对应的组件是否声明为 Spring 应用程序上下文里的 Bean(基本是自动配置的),为一个不生效的组件设置属性是没有用的。 multipart multipart.enabled 开启上传支持(默认:true) multipart.file-size-threshold : 大于该值的文件会被写到磁盘上 multipart.location 上传文件存放位置 multipart.max-file-size 最大文件大小 multipart.max-request-size 最大请求大小 server server.address 服务器地址 server.port 服务器端口 server.context-parameters.[param name] 设置 servlet 上下文参数 server.context-path 应用上下文路径 Jsp-servelt server.jsp-servelt.class-name 针对 jsp 使用的 Servlet 类名(默认:org.apache.jasper.servlet.JspServlet) server....
相关文章
文章评论
共有0条评论来说两句吧...