mysql中的interactive_timeout和wait_timeout

interactive_timeout和wait_timeout(mysql8小时)

mysql默认的interactive_timeoutwait_timeout时间是28800秒,也就是我们所说的8小时问题引起的原因

mysql> show variables like '%timeout%';
+-----------------------------+----------+
| Variable_name               | Value    |
+-----------------------------+----------+
| connect_timeout             | 10       |
| delayed_insert_timeout      | 300      |
| innodb_flush_log_at_timeout | 1        |
| innodb_lock_wait_timeout    | 50       |
| innodb_rollback_on_timeout  | OFF      |
| interactive_timeout         | 28800    |
| lock_wait_timeout           | 31536000 |
| net_read_timeout            | 30       |
| net_write_timeout           | 60       |
| rpl_stop_slave_timeout      | 31536000 |
| slave_net_timeout           | 3600     |
| wait_timeout                | 28800    |
+-----------------------------+----------+
12 rows in set

mysql> show global variables like '%timeout%';
+-----------------------------+----------+
| Variable_name               | Value    |
+-----------------------------+----------+
| connect_timeout             | 10       |
| delayed_insert_timeout      | 300      |
| innodb_flush_log_at_timeout | 1        |
| innodb_lock_wait_timeout    | 50       |
| innodb_rollback_on_timeout  | OFF      |
| interactive_timeout         | 28800    |  <--
| lock_wait_timeout           | 31536000 |
| net_read_timeout            | 30       |
| net_write_timeout           | 60       |
| rpl_stop_slave_timeout      | 31536000 |
| slave_net_timeout           | 3600     |
| wait_timeout                | 28800    |  <--
+-----------------------------+----------+

interactive_timeout

interactive_timeout指mysql服务器关闭交互连接方式超时的时间,如cmd模式下与mysql交互式查询

wait_timeout

wait_timeout指mysql服务器关闭非交互连接方式超时的时间,如jdbc的连接这种方式

使用控制变量法来测试,然后通过交互式连接方式和非交互式连接分别来查看interactive_timeoutwait_timeout相互受影响的情况

1.先设置global interactive_timeout=30

mysql> set global interactive_timeout=30;
Query OK, 0 rows affected

mysql> show global variables like '%timeout%';
+-----------------------------+----------+
| Variable_name               | Value    |
+-----------------------------+----------+
| connect_timeout             | 10       |
| delayed_insert_timeout      | 300      |
| innodb_flush_log_at_timeout | 1        |
| innodb_lock_wait_timeout    | 50       |
| innodb_rollback_on_timeout  | OFF      |
| interactive_timeout         | 30       |  <--
| lock_wait_timeout           | 31536000 |
| net_read_timeout            | 30       |
| net_write_timeout           | 60       |
| rpl_stop_slave_timeout      | 31536000 |
| slave_net_timeout           | 3600     |
| wait_timeout                | 28800    |  <--
+-----------------------------+----------+
12 rows in set

通过命令行来交互式访问

mysql> show variables like '%timeout%';
+-----------------------------+----------+
| Variable_name               | Value    |
+-----------------------------+----------+
| connect_timeout             | 10       |
| delayed_insert_timeout      | 300      |
| innodb_flush_log_at_timeout | 1        |
| innodb_lock_wait_timeout    | 50       |
| innodb_rollback_on_timeout  | OFF      |
| interactive_timeout         | 30       |  <--
| lock_wait_timeout           | 31536000 |
| net_read_timeout            | 30       |
| net_write_timeout           | 60       |
| rpl_stop_slave_timeout      | 31536000 |
| slave_net_timeout           | 3600     |
| wait_timeout                | 30       |  <--
+-----------------------------+----------+
12 rows in set (0.00 sec)

//after 30 seconds
mysql> select 1;
ERROR 2013 (HY000): Lost connection to MySQL server during query

mysql> select 1;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    4
Current database: *** NONE ***

+---+
| 1 |
+---+
| 1 |
+---+
1 row in set (0.01 sec)

可以看到interactive_timeoutwait_timeout都变成了30,并且30秒之后,客户端与mysql服务器失去连接,mysql服务器关闭了连接

非交互式访问

C:\Users\dxjm>mysql -uroot -p -e "select VARIABLE_NAME,VARIABLE_VALUE   
from information_schema.SESSION_VARIABLES where VARIABLE_NAME like '%timeout%'"
Enter password: ******
+-----------------------------+----------------+
| VARIABLE_NAME               | VARIABLE_VALUE |
+-----------------------------+----------------+
| INNODB_FLUSH_LOG_AT_TIMEOUT | 1              |
| RPL_STOP_SLAVE_TIMEOUT      | 31536000       |
| DELAYED_INSERT_TIMEOUT      | 300            |
| INNODB_ROLLBACK_ON_TIMEOUT  | OFF            |
| NET_WRITE_TIMEOUT           | 60             |
| CONNECT_TIMEOUT             | 10             |
| SLAVE_NET_TIMEOUT           | 3600           |
| LOCK_WAIT_TIMEOUT           | 31536000       |
| INNODB_LOCK_WAIT_TIMEOUT    | 50             |
| INTERACTIVE_TIMEOUT         | 30             |  <--
| WAIT_TIMEOUT                | 28800          |  <--
| NET_READ_TIMEOUT            | 30             |
+-----------------------------+----------------+

可以看到interactive_timeout=30wait_timeout=28800,即wait_timeout没有变化

2.设置global wait_timeout=50

mysql> set global wait_timeout=50;
Query OK, 0 rows affected

mysql> show global variables like '%timeout%';
+-----------------------------+----------+
| Variable_name               | Value    |
+-----------------------------+----------+
| connect_timeout             | 10       |
| delayed_insert_timeout      | 300      |
| innodb_flush_log_at_timeout | 1        |
| innodb_lock_wait_timeout    | 50       |
| innodb_rollback_on_timeout  | OFF      |
| interactive_timeout         | 30       |  <--
| lock_wait_timeout           | 31536000 |
| net_read_timeout            | 30       |
| net_write_timeout           | 60       |
| rpl_stop_slave_timeout      | 31536000 |
| slave_net_timeout           | 3600     |
| wait_timeout                | 50       |  <--
+-----------------------------+----------+
12 rows in set

通过命令行来交互式访问

C:\Users\dxjm>mysql -uroot -p
Enter password: ******
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 267
Server version: 5.6.24-log MySQL Community Server (GPL)

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> show variables like '%timeout%';
+-----------------------------+----------+
| Variable_name               | Value    |
+-----------------------------+----------+
| connect_timeout             | 10       |
| delayed_insert_timeout      | 300      |
| innodb_flush_log_at_timeout | 1        |
| innodb_lock_wait_timeout    | 50       |
| innodb_rollback_on_timeout  | OFF      |
| interactive_timeout         | 30       |  <--
| lock_wait_timeout           | 31536000 |
| net_read_timeout            | 30       |
| net_write_timeout           | 60       |
| rpl_stop_slave_timeout      | 31536000 |
| slave_net_timeout           | 3600     |
| wait_timeout                | 30       |  <--
+-----------------------------+----------+
12 rows in set (0.00 sec)

//after 30 seconds
mysql> select 1;
ERROR 2013 (HY000): Lost connection to MySQL server during query

mysql> select 1;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    4
Current database: *** NONE ***

+---+
| 1 |
+---+
| 1 |
+---+
1 row in set (0.01 sec)

可以看到interactive_timeout=30,而wait_timeout=30而不是变成了50

非交互式访问

C:\Users\dxjm>mysql -uroot -p -e "select VARIABLE_NAME,VARIABLE_VALUE   
from information_schema.SESSION_VARIABLES where VARIABLE_NAME like '%timeout%'"
Enter password: ******
+-----------------------------+----------------+
| VARIABLE_NAME               | VARIABLE_VALUE |
+-----------------------------+----------------+
| INNODB_FLUSH_LOG_AT_TIMEOUT | 1              |
| RPL_STOP_SLAVE_TIMEOUT      | 31536000       |
| DELAYED_INSERT_TIMEOUT      | 300            |
| INNODB_ROLLBACK_ON_TIMEOUT  | OFF            |
| NET_WRITE_TIMEOUT           | 60             |
| CONNECT_TIMEOUT             | 10             |
| SLAVE_NET_TIMEOUT           | 3600           |
| LOCK_WAIT_TIMEOUT           | 31536000       |
| INNODB_LOCK_WAIT_TIMEOUT    | 50             |
| INTERACTIVE_TIMEOUT         | 30             |  <--
| WAIT_TIMEOUT                | 50             |  <--
| NET_READ_TIMEOUT            | 30             |
+-----------------------------+----------------+

可以看到interactive_timeout=30不变,wait_timeout=50,即刚刚设置的值

你可能感兴趣的:(mysql学习)