实验基础:
MySQL 版本:5.7.28
交互连接方式进行测试
开始实验前,interactive_timeout和wait_timeout的global和session级都是28800s
开启两个session,以下简称session1、session2
mysql> select @@global.interactive_timeout,@@session.interactive_timeout;
+------------------------------+-------------------------------+
| @@global.interactive_timeout | @@session.interactive_timeout |
+------------------------------+-------------------------------+
| 28800 | 28800 |
+------------------------------+-------------------------------+
1 row in set (0.00 sec)
mysql> select @@global.wait_timeout,@@session.wait_timeout;
+-----------------------+------------------------+
| @@global.wait_timeout | @@session.wait_timeout |
+-----------------------+------------------------+
| 28800 | 28800 |
+-----------------------+------------------------+
1 row in set (0.00 sec)
mysql> set global interactive_timeout = 600;
Query OK, 0 rows affected (0.00 sec)
mysql> select @@global.interactive_timeout,@@session.interactive_timeout;
+------------------------------+-------------------------------+
| @@global.interactive_timeout | @@session.interactive_timeout |
+------------------------------+-------------------------------+
| 600 | 28800 |
+------------------------------+-------------------------------+
1 row in set (0.00 sec)
mysql> select @@global.wait_timeout,@@session.wait_timeout;
+-----------------------+------------------------+
| @@global.wait_timeout | @@session.wait_timeout |
+-----------------------+------------------------+
| 28800 | 28800 |
+-----------------------+------------------------+
1 row in set (0.00 sec)
设置global级的interactive_timeout会影响当前session的@@global.interactive_timeout
mysql> select @@global.interactive_timeout,@@session.interactive_timeout;
+------------------------------+-------------------------------+
| @@global.interactive_timeout | @@session.interactive_timeout |
+------------------------------+-------------------------------+
| 600 | 28800 |
+------------------------------+-------------------------------+
1 row in set (0.00 sec)
mysql> select @@global.wait_timeout,@@session.wait_timeout;
+-----------------------+------------------------+
| @@global.wait_timeout | @@session.wait_timeout |
+-----------------------+------------------------+
| 28800 | 28800 |
+-----------------------+------------------------+
1 row in set (0.00 sec)
设置global级的interactive_timeout会影响其他未退出登录session的@@global.interactive_timeout
mysql> select @@global.interactive_timeout,@@session.interactive_timeout;
+------------------------------+-------------------------------+
| @@global.interactive_timeout | @@session.interactive_timeout |
+------------------------------+-------------------------------+
| 600 | 600 |
+------------------------------+-------------------------------+
1 row in set (0.00 sec)
mysql> select @@global.wait_timeout,@@session.wait_timeout;
+-----------------------+------------------------+
| @@global.wait_timeout | @@session.wait_timeout |
+-----------------------+------------------------+
| 28800 | 600 |
+-----------------------+------------------------+
1 row in set (0.00 sec)
设置global级的interactive_timeout会影响其他新登录session的@@global.interactive_timeout、@@session.interactive_timeout、@@session.wait_timeout
mysql> set session interactive_timeout = 300;
Query OK, 0 rows affected (0.00 sec)
mysql> select @@global.interactive_timeout,@@session.interactive_timeout;
+------------------------------+-------------------------------+
| @@global.interactive_timeout | @@session.interactive_timeout |
+------------------------------+-------------------------------+
| 600 | 300 |
+------------------------------+-------------------------------+
1 row in set (0.00 sec)
mysql> select @@global.wait_timeout,@@session.wait_timeout;
+-----------------------+------------------------+
| @@global.wait_timeout | @@session.wait_timeout |
+-----------------------+------------------------+
| 28800 | 600 |
+-----------------------+------------------------+
1 row in set (0.00 sec)
设置session级的interactive_timeout会影响当前session的@@session.interactive_timeout
mysql> select @@global.interactive_timeout,@@session.interactive_timeout;
+------------------------------+-------------------------------+
| @@global.interactive_timeout | @@session.interactive_timeout |
+------------------------------+-------------------------------+
| 600 | 600 |
+------------------------------+-------------------------------+
1 row in set (0.00 sec)
mysql> select @@global.wait_timeout,@@session.wait_timeout;
+-----------------------+------------------------+
| @@global.wait_timeout | @@session.wait_timeout |
+-----------------------+------------------------+
| 28800 | 600 |
+-----------------------+------------------------+
1 row in set (0.00 sec)
设置session级的interactive_timeout不会影响其他未退出登录session的interactive_timeout
mysql> select @@global.interactive_timeout,@@session.interactive_timeout;
+------------------------------+-------------------------------+
| @@global.interactive_timeout | @@session.interactive_timeout |
+------------------------------+-------------------------------+
| 600 | 600 |
+------------------------------+-------------------------------+
1 row in set (0.00 sec)
mysql> select @@global.wait_timeout,@@session.wait_timeout;
+-----------------------+------------------------+
| @@global.wait_timeout | @@session.wait_timeout |
+-----------------------+------------------------+
| 28800 | 600 |
+-----------------------+------------------------+
1 row in set (0.00 sec)
设置session级的interactive_timeout不会影响其他新登录session的interactive_timeout
mysql> set global wait_timeout= 200;
Query OK, 0 rows affected (0.00 sec)
mysql> select @@global.interactive_timeout,@@session.interactive_timeout;
+------------------------------+-------------------------------+
| @@global.interactive_timeout | @@session.interactive_timeout |
+------------------------------+-------------------------------+
| 600 | 600 |
+------------------------------+-------------------------------+
1 row in set (0.00 sec)
mysql> select @@global.wait_timeout,@@session.wait_timeout;
+-----------------------+------------------------+
| @@global.wait_timeout | @@session.wait_timeout |
+-----------------------+------------------------+
| 200 | 600 |
+-----------------------+------------------------+
1 row in set (0.00 sec)
设置global级的wait_timeout会影响当前session的@@global.wait_timeout
mysql> select @@global.interactive_timeout,@@session.interactive_timeout;
+------------------------------+-------------------------------+
| @@global.interactive_timeout | @@session.interactive_timeout |
+------------------------------+-------------------------------+
| 600 | 600 |
+------------------------------+-------------------------------+
1 row in set (0.00 sec)
mysql> select @@global.wait_timeout,@@session.wait_timeout;
+-----------------------+------------------------+
| @@global.wait_timeout | @@session.wait_timeout |
+-----------------------+------------------------+
| 200 | 600 |
+-----------------------+------------------------+
1 row in set (0.00 sec)
设置global级的wait_timeout会影响其他未退出登录session的@@global.wait_timeout
mysql> select @@global.interactive_timeout,@@session.interactive_timeout;
+------------------------------+-------------------------------+
| @@global.interactive_timeout | @@session.interactive_timeout |
+------------------------------+-------------------------------+
| 600 | 600 |
+------------------------------+-------------------------------+
1 row in set (0.00 sec)
mysql> select @@global.wait_timeout,@@session.wait_timeout;
+-----------------------+------------------------+
| @@global.wait_timeout | @@session.wait_timeout |
+-----------------------+------------------------+
| 200 | 600 |
+-----------------------+------------------------+
1 row in set (0.00 sec)
设置global级的wait_timeout会影响其他新登录session的@@global.wait_timeout,但@@session.wait_timeout没有改变
mysql> set session wait_timeout= 100;
Query OK, 0 rows affected (0.00 sec)
mysql> select @@global.interactive_timeout,@@session.interactive_timeout;
+------------------------------+-------------------------------+
| @@global.interactive_timeout | @@session.interactive_timeout |
+------------------------------+-------------------------------+
| 600 | 600 |
+------------------------------+-------------------------------+
1 row in set (0.00 sec)
mysql> select @@global.wait_timeout,@@session.wait_timeout;
+-----------------------+------------------------+
| @@global.wait_timeout | @@session.wait_timeout |
+-----------------------+------------------------+
| 200 | 100 |
+-----------------------+------------------------+
1 row in set (0.00 sec)
设置session级的wait_timeout会影响当前session的@@session.wait_timeout
mysql> select @@global.interactive_timeout,@@session.interactive_timeout;
+------------------------------+-------------------------------+
| @@global.interactive_timeout | @@session.interactive_timeout |
+------------------------------+-------------------------------+
| 600 | 600 |
+------------------------------+-------------------------------+
1 row in set (0.00 sec)
mysql> select @@global.wait_timeout,@@session.wait_timeout;
+-----------------------+------------------------+
| @@global.wait_timeout | @@session.wait_timeout |
+-----------------------+------------------------+
| 200 | 600 |
+-----------------------+------------------------+
1 row in set (0.00 sec)
设置session级的wait_timeout不会影响其他未退出登录session的wait_timeout
mysql> select @@global.interactive_timeout,@@session.interactive_timeout;
+------------------------------+-------------------------------+
| @@global.interactive_timeout | @@session.interactive_timeout |
+------------------------------+-------------------------------+
| 600 | 600 |
+------------------------------+-------------------------------+
1 row in set (0.00 sec)
mysql> select @@global.wait_timeout,@@session.wait_timeout;
+-----------------------+------------------------+
| @@global.wait_timeout | @@session.wait_timeout |
+-----------------------+------------------------+
| 200 | 600 |
+-----------------------+------------------------+
1 row in set (0.00 sec)
设置session级的wait_timeout不会影响其他新登录session的wait_timeout
1、
设置session级的interactive_timeout仅会影响当前session的@@session.interactive_timeout,退出即失效
设置session级的wait_timeout仅会影响当前session的@@session.wait_timeout,退出即失效
这跟我们的固有理论相符。
2、
设置global 级别的wait_timeout会影响当前session的@@global.wait_timeout、其他未退出登录session的@@global.wait_timeout,以及新登录session的@@global.wait_timeout
设置global级的interactive_timeout会影响当前session的@@global.interactive_timeout、其他未退出登录session的@@global.interactive_timeout,以及新登录session的@@global.interactive_timeout
这跟我们的固有理论相符。
3、
@@session.interactive_timeout继承了@@global.interactive_timeout的值。(这跟我们的固有理论相符)
设置global级的wait_timeout不会影响新登录session的@@session.wait_timeout
设置global级的interactive_timeout会影响其他新登录session的@@session.wait_timeout
最后两点就与众不同了。
简言之,交互连接下的session级的wait_timeout,没有继承@@global.wait_timeout,反而继承了@@global.interactive_timeout的值。
这就意味着,如果需要修改全局wait_timeout的值,使之对新的交互连接生效,常规思路的set global wait_timeout将失效。应该通过set global interactive_timeout的方式去修改wait_timeout。
注:非交互模式下,session级的wait_timeout继承的是global级的wait_timeout
################
#非交互的实验待补充#
################
摘录一段官方文档对此的释疑:
wait_timeout: The number of seconds the server waits for activity on a noninteractive connection before closing it.
等待非交互连接转为activity状态的超时时间(即转入Sleep状态后开始计算超时时间)
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.
新连接启动后,session级的wait_timeout值是根据:global级的wait_timeout值、或者global级的interactive_timeout值初始化而来,不同的客户端连接类型会选择不同的初始化参考值。
interactive_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.
等待交互连接转为activity状态的超时时间
CLIENT_INTERACTIVE: Permit interactive_timeout seconds of inactivity (rather than wait_timeout seconds) before closing the connection. The client's session wait_timeout variable is set to the value of the session interactive_timeout variable.
mysql_real_connect() 的client_flag默认为0,可自定义flag,例如:CLIENT_INTERACTIVE
连接池中默认是怎么设置的呢?待研究。。。
参考文档:
【MySQL】浅析interactive_timeout和wait_timeout
MySQL中interactive_timeout和wait_timeout的区别
MySQL中 timeout相关参数解析
[MySQL分享]--interactive_timeout和wait_timeout小结(interactive_timeout会覆盖wait_timeout)