侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

MySQL中interactive_timeout和wait_timeout的区别

数据库  /  管理员 发布于 6年前   314

在用mysql客户端对数据库进行操作时,打开终端窗口,如果一段时间没有操作,再次操作时,常常会报如下错误:

ERROR 2013 (HY000): Lost connection to MySQL server during queryERROR 2006 (HY000): MySQL server has gone awayNo connection. Trying to reconnect...

这个报错信息就意味着当前的连接已经断开,需要重新建立连接。

那么,连接的时长是如何确认的?

其实,这个与interactive_timeout和wait_timeout的设置有关。

首先,看看官方文档对于这两个参数的定义

interactive_timeout

默认是28800,单位秒,即8个小时

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.

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参数

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 |+---------------------+----------------+2 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后再执行操作,连接已经断开。

只修改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 |+---------------------+----------------+2 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 |+---------------------+----------------+2 rows in set (0.06 sec)

Q2:会话变量wait_timeout的继承问题

A2:如果是交互式连接,则继承全局变量interactive_timeout的值,如果是非交互式连接,则继承全局变量wait_timeout的值。

验证:

只修改全局变量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 |+---------------------+----------------+2 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 |+---------------------+----------------+2 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 |+---------------------+----------------+2 rows in set (0.00 sec)

发现,WAIT_TIMEOUT的值已经变为10了。

但通过jdbc测试,wait_timeout的值依旧是28800

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的值

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 |+---------------------+----------------+2 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','wait_timeout');+---------------------+----------------+| variable_name | variable_value |+---------------------+----------------+| INTERACTIVE_TIMEOUT | 28800 || WAIT_TIMEOUT | 20 |+---------------------+----------------+2 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 |+---------------------+----------------+2 rows in set (0.03 sec)

WAIT_TIMEOUT的值依旧是28800.

查看jdbc的结果

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));}}}

查看jdbc的结果

INTERACTIVE_TIMEOUT: 28800WAIT_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 failedat 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

总结

1. 控制连接最大空闲时长的wait_timeout参数。

2. 对于非交互式连接,类似于jdbc连接,wait_timeout的值继承自服务器端全局变量wait_timeout。

对于交互式连接,类似于mysql客户单连接,wait_timeout的值继承自服务器端全局变量interactive_timeout。

3. 判断一个连接的空闲时间,可通过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 |+----+------+----------------------+------+---------+------+-------+------------------+2 rows in set (0.03 sec)

以上所述是小编给大家介绍的MySQL中interactive_timeout和wait_timeout的区别,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对站的支持!


  • 上一条:
    mysql 实现互换表中两列数据方法简单实例
    下一条:
    mysql 5.7.16 zip包安装配置方法图文教程
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 分库分表的目的、优缺点及具体实现方式介绍(0个评论)
    • DevDB - 在 VS 代码中直接访问数据库(0个评论)
    • 在ubuntu系统中实现mysql数据存储目录迁移流程步骤(0个评论)
    • 在mysql中使用存储过程批量新增测试数据流程步骤(0个评论)
    • php+mysql数据库批量根据条件快速更新、连表更新sql实现(0个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2017-06
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-05
    • 2018-10
    • 2018-11
    • 2020-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2021-02
    • 2021-04
    • 2021-07
    • 2021-08
    • 2021-11
    • 2021-12
    • 2022-02
    • 2022-03
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-07
    • 2023-08
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-03
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客