当前位置:网站首页>Mysql interactive_ Timeout and wait_ Timeout differences
Mysql interactive_ Timeout and wait_ Timeout differences
2022-06-25 04:52:00 【Oglie's magic slippers~】
Mysql interactive_timeout and wait_timeout The difference between
In use mysql When the client operates the database , Open terminal window , If there is no operation for a period of time , When operating again , The following mistakes are often reported :
ERROR 2013 (HY000): Lost connection to MySQL server during query ERROR
2006 (HY000): MySQL server has gone away No connection. Trying to
reconnect…
This error message means that the current connection has been disconnected , The connection needs to be reestablished .
that , How to confirm the connection duration ?
Actually , This with interactive_timeout and wait_timeout The settings of .
First , Take a look at the official document definition of these two parameters
interactive_timeout
The default is 28800, Unit second , namely 8 Hours
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
The default is the same 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.
According to the above definition , The difference between the two is obvious
1> interactive_timeout For interactive connections ,wait_timeout For non interactive connections . So called interactive connection , That is to say mysql_real_connect() Used in function CLIENT_INTERACTIVE Options .
To be frank , adopt mysql The client connection to the database is an interactive connection , adopt jdbc Connecting to a database is a non interactive connection .
2> When the connection starts , Depending on the type of connection , To confirm the session variables wait_timeout The value of is inherited from the global variable wait_timeout, still interactive_timeout.
Let's test it , Confirm the following questions
Which parameter controls the maximum idle time of the connection .
Session variables wait_timeout The question of inheritance
Q1: Which parameter controls the maximum idle time of the connection
A1:wait_timeout
verification
Modify only wait_timeout Parameters
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)
------- wait for 10s Post execution
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
You can see , wait for 10s Then perform the operation , The connection has been disconnected .
Modify only interactive_timeout Parameters
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)
---------- wait for 10s After execution
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: Session variables wait_timeout The question of inheritance
A2: If it's an interactive connection , Then inherit the global variable interactive_timeout Value , If it's a non interactive connection , Then inherit the global variable wait_timeout Value .
verification :
Modify only global variables interactive_timeout Value
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)
Open another mysql client , Look at the value of the session variable
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)
Find out ,WAIT_TIMEOUT The value of has changed to 10 了 .
But through jdbc test ,wait_timeout The value of is still 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));
}
}
}
The output is as follows :
INTERACTIVE_TIMEOUT: 10
WAIT_TIMEOUT: 28800
Modify only global variables wait_timeout Value
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 |
+---------------------+----------------+
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','wa it_timeout');+---------------------+----------------+
| variable_name | variable_value |
+---------------------+----------------+
| INTERACTIVE_TIMEOUT | 28800 |
| WAIT_TIMEOUT | 20 |
+---------------------+----------------+
2 rows in set (0.00 sec)
Open another mysql client , Look at the value of the session variable
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 The value of is still 28800.
see jdbc Result
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));
}
}
}
see jdbc Result
INTERACTIVE_TIMEOUT: 28800
WAIT_TIMEOUT: 20
meanwhile , A new program has been added , wait for 20s after , Execute the query again , Report the following error :
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
summary
Control the maximum idle time of the connection wait_timeout Parameters .
For non interactive connections , Be similar to jdbc Connect ,wait_timeout The value of is inherited from the server-side global variable wait_timeout.
For interactive connections , Be similar to mysql Customer single connection ,wait_timeout The value of is inherited from the server-side global variable interactive_timeout.
Determine the idle time of a connection , It can be done by show processlist Output in progress Sleep Time of state
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)
边栏推荐
猜你喜欢

固态硬盘开盘数据恢复的方法

领导:谁再用 Redis 过期监听实现关闭订单,立马滚蛋!

dotnet-exec 0.4.0 released

JS arguments

Kotlin compose perfect todo project surface rendering background and shadow

Student achievement management system based on SSH

halcon之区域:多种区域(Region)生成(3)

Kotlin Compose 完善toDo项目 Surface 渲染背景 与阴影

My IC journey - the growth of senior chip design verification engineers - "Hu" said that IC engineers are perfect and advanced

Web3 DApp用户体验最佳实践
随机推荐
epplus复制模板后打印区域变小的问题
Sleep more, you can lose weight. According to the latest research from the University of Chicago, sleeping more than 1 hour a day is equivalent to eating less than one fried chicken leg
How to apply for software
PostgreSQL database Wal - RM_ HEAP_ ID logging action
halcon之区域:多种区域(Region)生成(3)
Upgrade PHP to php7 The impact of X (2), the obsolescence of mcrypt decryption
初识 Flutter 的绘图组件 — CustomPaint
Chapter IX app project test (2) test tools
Triangle class (construction and deconstruction)
30岁了开始自学编程,家里比较困难还来得及吗?
以太网是什么要怎么连接电脑
SOC验证环境的启动方式
魔法猪系统重装大师怎么使用
OOP vector addition and subtraction (friend + copy construction)
渗透测试-目录遍历漏洞
第九章 APP项目测试(2) 测试工具
leetcode1221. 分割平衡字符串
CTF_ Web: Changan cup-2021 old but a little new & asuka
Wechat applet new version prompt update
After the newly assigned variable of the applet is modified, the original variable will also be modified