前言:

MySQL中有两个关于连接超时的配置项。他们之间在某些条件下会互相继承,那究竟这两个参数会在什么情况下起作用呢?

本文将会通过一些测试实例来证明总结两者的相互关系。

参数介绍:

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 theCLIENT_INTERACTIVEoption tomysql_real_connect(). See alsowait_timeout.

The number of seconds the server waits for activity on a noninteractive connection before closing it. Before MySQL 5.1.41, this timeout applies only to TCP/IP connections, not to connections made through Unix socket files, named pipes, or shared memory.

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_INTERACTIVEconnect option to mysql_real_connect()). See also interactive_timeout.

CLIENT_INTERACTIVE

Permitinteractive_timeoutseconds (instead ofwait_timeoutseconds) of inactivity before closing the connection. The client's sessionwait_timeoutvariable is set to the value of the sessioninteractive_timeoutvariable.

简单的说 interactive就是交互式的终端,例如在shell里面直接执行mysql,出现 mysql> 后就是交互式的连接。而mysql -e 'select 1' 这样的直接返回结果的方式就是非交互式的连接。

第二部分  测试

2.1 继承关系

Q:通过Socket连接 timeout会从哪个global timeout继承

A:由下例可见,通过socket登录,timeout 继承于global.interactive_timeout;

mysql>set global interactive_timeout =  11111;

Query OK, 0rows affected (0.00 sec)

mysql>set global wait_timeout = 22222;

Query OK, 0rows affected (0.00 sec)

mysql> showglobal variableslike '%timeout%';

+----------------------------+----------+

| Variable_name              | Value    |

+----------------------------+----------+

| connect_timeout            | 10       |

| delayed_insert_timeout     | 300      |

| innodb_lock_wait_timeout   | 50       |

| innodb_rollback_on_timeout |OFF      |

| interactive_timeout        | 11111    |

| lock_wait_timeout          | 31536000 |

| net_read_timeout           | 30       |

| net_write_timeout          | 60       |

| slave_net_timeout          | 3600     |

| wait_timeout               | 22222    |

+----------------------------+----------+

10rows in set (0.00 sec)

mysql -uroot -ppassword -S /usr/local/mysql3310/mysql.sock

Welcometo the MySQL monitor.  Commandsend with ;or \g.

Your MySQLconnection idis 4

Server version: 5.5.16-log MySQL Community Server (GPL)

Copyright (c) 2000, 2011, Oracleand/or its affiliates.All rights reserved.

Oracleis a registered trademarkof Oracle Corporationand/or its

affiliates. Other names may be trademarksof their respective

owners.

Type'help;' or '\h' for help. Type'\c' to clear thecurrent input statement.

mysql> show session variableslike '%timeout%';

+----------------------------+----------+

| Variable_name              | Value    |

+----------------------------+----------+

| connect_timeout            | 10       |

| delayed_insert_timeout     | 300      |

| innodb_lock_wait_timeout   | 50       |

| innodb_rollback_on_timeout |OFF      |

| interactive_timeout        | 11111    |

| lock_wait_timeout          | 31536000 |

| net_read_timeout           | 30       |

| net_write_timeout          | 60       |

| slave_net_timeout          | 3600     |

| wait_timeout               | 11111    |

+----------------------------+----------+

10rows in set (0.00 sec)

Q:通过TCP/IP client 连接, timeout会从哪个global timeout继承

A:由下例可见,通过TCP/IP client 连接后的wait_timeout 仍然继承于 global.interactive_timeout

mysql -uroot -ppassword -h 127.0.0.1--port 3310

Welcometo the MySQL monitor.  Commandsend with ;or \g.

Your MySQLconnection idis 6

Server version: 5.5.16-log MySQL Community Server (GPL)

Copyright (c) 2000, 2011, Oracleand/or its affiliates.All rights reserved.

Oracleis a registered trademarkof Oracle Corporationand/or its

affiliates. Other names may be trademarksof their respective

owners.

Type'help;' or '\h' for help. Type'\c' to clear thecurrent input statement.

mysql> show session variableslike '%timeout%';

+----------------------------+----------+

| Variable_name              | Value    |

+----------------------------+----------+

| connect_timeout            | 10       |

| delayed_insert_timeout     | 300      |

| innodb_lock_wait_timeout   | 50       |

| innodb_rollback_on_timeout |OFF      |

| interactive_timeout        | 11111    |

| lock_wait_timeout          | 31536000 |

| net_read_timeout           | 30       |

| net_write_timeout          | 60       |

| slave_net_timeout          | 3600     |

| wait_timeout               | 11111    |

+----------------------------+----------+

10rows in set (0.00 sec)

2.2 起效关系

Q:timeout值,对于正在运行用的语句是否起效?

A:由下例可见SQL正在执行状态的等待时间不计入timeout时间

mysql>set session wait_timeout=10;

Query OK, 0rows affected (0.00 sec)

mysql>set session interactive_timeout=10;

Query OK, 0rows affected (0.00 sec)

mysql>select 1,sleep(20)from dual;

+---+-----------+

| 1 | sleep(20) |

+---+-----------+

| 1 |         0 |

+---+-----------+

1 rowin set (20.00 sec)

mysql>

mysql> show session variableslike '%timeout%';

+----------------------------+----------+

| Variable_name              | Value    |

+----------------------------+----------+

| connect_timeout            | 10       |

| delayed_insert_timeout     | 300      |

| innodb_lock_wait_timeout   | 50       |

| innodb_rollback_on_timeout |OFF      |

| interactive_timeout        | 10       |

| lock_wait_timeout          | 31536000 |

| net_read_timeout           | 30       |

| net_write_timeout          | 60       |

| slave_net_timeout          | 3600     |

| wait_timeout               | 10       |

+----------------------------+----------+

Q:wait_timeout 和 interacitve_timeout 如何相互作用。

A:只有session.wait_timeout 会起效

mysql>set session interactive_timeout=10;

Query OK, 0rows affected (0.00 sec)

mysql>set session wait_timeout=20;

Query OK, 0rows affected (0.00 sec)

---------------------another connection-------------------------

mysql> showfull processlist;

+----+-------------+-----------------+------+---------+--------+-----------------------------------------------------------------------------+-----------------------+-----------+---------------+-----------+

| Id |User        | Host            | db   | Command |Time   | State                                                                       | Info                  | Rows_sent | Rows_examined | Rows_read |

+----+-------------+-----------------+------+---------+--------+-----------------------------------------------------------------------------+-----------------------+-----------+---------------+-----------+

|  1 | systemuser |                 |NULL |Connect | 103749 | Slave hasread all relay log; waitingfor the slave I/O threadto update it |NULL                  |         0 |             0 |         1 |

|  2 | systemuser |                 |NULL |Connect | 103750 | Connectingto master                                                        |NULL                  |         0 |             0 |         1 |

|  3 | root        | localhost       |NULL | Query   |      0 |NULL                                                                        | showfull processlist |         0 |             0 |        11 |

| 10 | root        | localhost:58946 |NULL | Sleep   |     20 |                                                                             |NULL                  |         0 |             0 |        11 |

+----+-------------+-----------------+------+---------+--------+-----------------------------------------------------------------------------+-----------------------+-----------+---------------+-----------+

4rows in set (0.00 sec)

mysql> showfull processlist;

+----+-------------+-----------+------+---------+--------+-----------------------------------------------------------------------------+-----------------------+-----------+---------------+-----------+

| Id |User        | Host      | db   | Command |Time   | State                                                                       | Info                  | Rows_sent | Rows_examined | Rows_read |

+----+-------------+-----------+------+---------+--------+-----------------------------------------------------------------------------+-----------------------+-----------+---------------+-----------+

|  1 | systemuser |           |NULL |Connect | 103749 | Slave hasread all relay log; waitingfor the slave I/O threadto update it |NULL                  |         0 |             0 |         1 |

|  2 | systemuser |           |NULL |Connect | 103750 | Connectingto master                                                        |NULL                  |         0 |             0 |         1 |

|  3 | root        | localhost |NULL | Query   |      0 |NULL                                                                        | showfull processlist |         0 |             0 |        11 |

+----+-------------+-----------+------+---------+--------+-----------------------------------------------------------------------------+-----------------------+-----------+---------------+-----------+

3rows in set (0.00 sec)

Q:global timeout和session timeout哪个起作用。

A:只有session timeout 会起作用。

测试1:

mysql>set session interactive_timeout = 10;

Query OK, 0rows affected (0.00 sec)

mysql>set session wait_timeout = 10;

Query OK, 0rows affected (0.00 sec)

mysql> show session variableslike '%timeout%';

+----------------------------+----------+

| Variable_name              | Value    |

+----------------------------+----------+

| interactive_timeout        | 10       |

| wait_timeout               | 10       |

+----------------------------+----------+

10rows in set (0.00 sec)

mysql> showglobal variableslike '%timeout%';

+----------------------------+----------+

| Variable_name              | Value    |

+----------------------------+----------+

| interactive_timeout        | 20       |

| wait_timeout               | 20       |

+----------------------------+----------+

10rows in set (0.00 sec)

mysql> showfull processlist;

+----+-------------+-----------------+------+---------+--------+-----------------------------------------------------------------------------+-----------------------+-----------+---------------+-----------+

| Id |User        | Host            | db   | Command |Time   | State                                                                       | Info                  | Rows_sent | Rows_examined | Rows_read |

+----+-------------+-----------------+------+---------+--------+-----------------------------------------------------------------------------+-----------------------+-----------+---------------+-----------+

|  3 | root        | localhost       |NULL | Query   |      0 |NULL                                                                        | showfull processlist |         0 |             0 |        11 |

| 17 | root        | localhost:60585 |NULL | Sleep   |     10 |                                                                             |NULL                  |        10 |            10 |        11 |

+----+-------------+-----------------+------+---------+--------+-----------------------------------------------------------------------------+-----------------------+-----------+---------------+-----------+

2rows in set (0.00 sec)

mysql> showfull processlist;

+----+-------------+-----------+------+---------+--------+-----------------------------------------------------------------------------+-----------------------+-----------+---------------+-----------+

| Id |User        | Host      | db   | Command |Time   | State                                                                       | Info                  | Rows_sent | Rows_examined | Rows_read |

+----+-------------+-----------+------+---------+--------+-----------------------------------------------------------------------------+-----------------------+-----------+---------------+-----------+

|  3 | root        | localhost |NULL | Query   |      0 |NULL                                                                        | showfull processlist |         0 |             0 |        11 |

+----+-------------+-----------+------+---------+--------+-----------------------------------------------------------------------------+-----------------------+-----------+---------------+-----------+

1rows in set (0.00 sec)

测试2:

mysql> show session variableslike '%timeout%';

+----------------------------+----------+

| Variable_name              | Value    |

+----------------------------+----------+

| interactive_timeout        | 20       |

| wait_timeout               | 20       |

+----------------------------+----------+

10rows in set (0.00 sec)

mysql> showglobal variableslike '%timeout%';

+----------------------------+----------+

| Variable_name              | Value    |

+----------------------------+----------+

| interactive_timeout        | 10       |
| wait_timeout               | 10       |

+----------------------------+----------+

10rows in set (0.00 sec)

mysql> showfull processlist;

+----+-------------+-----------------+------+---------+--------+-----------------------------------------------------------------------------+-----------------------+-----------+---------------+-----------+

| Id |User        | Host            | db   | Command |Time   | State                                                                       | Info                  | Rows_sent | Rows_examined | Rows_read |

+----+-------------+-----------------+------+---------+--------+-----------------------------------------------------------------------------+-----------------------+-----------+---------------+-----------+

|  3 | root        | localhost       |NULL | Query   |      0 |NULL                                                                        | showfull processlist |         0 |             0 |        11 |

| 19 | root        | localhost:50276 |NULL | Sleep   |     19 |                                                                             |NULL                  |        10 |            10 |        11 |

+----+-------------+-----------------+------+---------+--------+-----------------------------------------------------------------------------+-----------------------+-----------+---------------+-----------+

2rows in set (0.00 sec)

mysql> showfull processlist;

+----+-------------+-----------+------+---------+--------+-----------------------------------------------------------------------------+-----------------------+-----------+---------------+-----------+

| Id |User        | Host      | db   | Command |Time   | State                                                                       | Info                  | Rows_sent | Rows_examined | Rows_read |

+----+-------------+-----------+------+---------+--------+-----------------------------------------------------------------------------+-----------------------+-----------+---------------+-----------+

|  3 | root        | localhost |NULL | Query   |      0 |NULL                                                                        | showfull processlist |         0 |             0 |        11 |

+----+-------------+-----------+------+---------+--------+-----------------------------------------------------------------------------+-----------------------+-----------+---------------+-----------+

1rows in set (0.00 sec)

第三部分 总结

由以上的阶段测试可以获得以下结论。

1. 超时时间只对非活动状态的connection进行计算。

2. 超时时间指通过 session wait_timeout 起效。

3. 交互式连接的wait_timeout 继承于 global.interactive_timeout

非交互式连接的wait_timeout 继承于 global.wait_timeout

4. 继承关系和超时对 TCP/IP 和 Socket 连接均有效果

mysql几个timeout参数_MySQL中 timeout相关参数解析相关推荐

  1. mysql慢查询日志轮转_MySQL slow log相关参数解释

    slow_query_log=1       #是否启用慢查询日志,1为启用,0为禁用 slow_query_log_file=slow.log       #指定慢查询日志文件的路径和名字,可使用绝 ...

  2. python魔法参数_python中的魔法参数:*args和**kwargs

    def foo(*args, **kwargs): print 'args = ', args print 'kwargs = ', kwargs print '------------------- ...

  3. mysql limit 动态参数_MySQL中limit使用动态参数的解决方法(拼接SQL字符串语句来执行SQL)...

    官方好像说过limit已经在5.6版本上支持了动态参数,但是测试时依然还是不行. 那么要解决limit动态参数唯一能做的就是使用字符串SQL拼接的形式,然后再进行执行. 一般有以下方式解决: 1.存储 ...

  4. MySQL怎么存base64编码_MySQL中如何将字符串转为base64编码?

    在MySQL中,TO_BASE64()函数将字符串转换为以base-64编码的字符串并返回结果.(相关推荐:<MySQL教程>) 语法TO_BASE64(str) 其中str是需要编码的字 ...

  5. mysql数据库回滚日志_MySQL中是如何实现事务提交和回滚的?

    什么是事务 事务是由数据库中一系列的访问和更新组成的逻辑执行单元 事务的逻辑单元中可以是一条SQL语句,也可以是一段SQL逻辑,这段逻辑要么全部执行成功,要么全部执行失败 举个最常见的例子,你早上出去 ...

  6. mysql创建临时表 主键_MySQL中临时表的基本创建与使用教程

    当工作在非常大的表上时,你可能偶尔需要运行很多查询获得一个大量数据的小的子集,不是对整个表运行这些查询,而是让MySQL每次找出所需的少数记录,将记录选择到一个临时表可能更快些,然后在这些表运行查询. ...

  7. mysql获取当前的月_MYSQL中获取当前的年和月

    展开全部 select year(curdate()),month(curdate()),day(curdate()); select weekofyear(curdate()); 有点细微的差别,w ...

  8. mysql 导入数据库sql语句_mysql中导入数据与导出数据库sql语句

    本文章来详细介绍关于mysql中导入数据与导出数据库sql语句,在mysql中常用的导入与导出数据的命令有source与mysqldump大家可参考. 1.例1:连接到本机上的MYSQL 首先在打开D ...

  9. mysql日期隐式转换_mysql中的隐式转换

    什么隐式类型转换? 在MySQL中: 当操作符与不同类型的操作数一起使用时,会发生类型转换以使操作数兼容.则会发生转换隐式 也就是说,MySQL会根据需要自动将数字转换为字符串,将字符串转换数字.看到 ...

最新文章

  1. http方法_手撸HTTP是理解HTTP的最好方法(0)
  2. 自定义复制和新建的文件名
  3. windows nt service 框架
  4. C语言数组相似度比对,某课程设计---文件相似度判断
  5. android JNI(转)
  6. mysql中名词解析
  7. 机器人动力学与控制_机器人领域值得一看的好书推荐
  8. CAD插件学习系列教程(一) 贱人工具箱的使用
  9. 全开源!智能灯串开发资料全开源!为这个冬天装点烂漫“星空”
  10. 太赞了,英伟达又一突破,输入关键词就可以生成直逼摄影师的大片
  11. 管理部门使用计算机属于固定资产核算吗,固定资产核算管理内容
  12. 我的世界服务器修改器1.7.10,我的世界修改器_我的世界TMI内置修改器1.7.10 - 99单机游戏...
  13. Windows命令实现匿名邮件发送
  14. 门禁系统服务器 控制器 读卡器,门禁系统建设方案.docx
  15. AlertPay网银
  16. 一周搞定scrapy之第一天--爬取起点中文小说网
  17. uni-app 微信小程序提示音一闪而逝
  18. Android RxJava操作符的学习---功能性操作符--网络请求出错重连(结合Retrofit)
  19. 实验2-4-5 简单实现x的n次方(10分)
  20. java订单编号工具类_[idmiss-common] Java 工具类之- 顺序订单号的生成

热门文章

  1. android自定义布局实现优惠券效果
  2. 云计算:容器技术变革云计算,SaaS带动CaaS市场
  3. 门道多:一次MaxCompute PS任务的问题排查之旅
  4. Linux学习 Unit 9
  5. Unity3D研究院之Android同步方法读取streamingAssets
  6. iOS APP网络分析之rvictl(可以捕捉除了Wifi以外的网络类型)
  7. kali下生成web端后门
  8. IOS静态库生成及测试
  9. asp.net ajax1.0基础回顾(七):综合应用
  10. 上传附件删除、session清空问题