mysql5 及以上版本 8小时会断开连接, autoReconnect=true 设置了不生效解决方法

mysql在空闲超过8小时会自动断开连接,在mysql5以下版本可以设置如下既可以解决:

autoReconnect=true&failOverReadOnly=false

但mysql5及以上设置了也不会生效,空闲超过8小时再去操作mysql数据库会提示一下错误:

Caused by: com.mysql.cj.jdbc.exceptions.CommunicationsException: The last packet successfully received from the server was 47,771,561 milliseconds ago.  The last packet sent successfully to the server was 47,771,562 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.
    at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:172)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:960)
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeQuery(ClientPreparedStatement.java:1019)
    at sun.reflect.GeneratedMethodAccessor100.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.tomcat.jdbc.pool.StatementFacade$StatementProxy.invoke(StatementFacade.java:114)
    at com.sun.proxy.$Proxy124.executeQuery(Unknown Source)
    at org.springframework.jdbc.core.JdbcTemplate$1.doInPreparedStatement(JdbcTemplate.java:692)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:633)
    ... 80 common frames omitted

解决方法:

方法1:

[root@iZuf61q00u1u1gzrtxkmibZ ~]# /usr/local/mysql/bin/mysql -uroot -h127.0.0.1 -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 909
Server version: 8.0.12 MySQL Community Server - GPL

Copyright (c) 2000, 2018, 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      |
| have_statement_timeout            | YES      |
| innodb_flush_log_at_timeout       | 1        |
| innodb_lock_wait_timeout          | 50       |
| innodb_rollback_on_timeout        | OFF      |
| interactive_timeout               | 28800    |
| lock_wait_timeout                 | 31536000 |
| mysqlx_connect_timeout            | 30       |
| mysqlx_idle_worker_thread_timeout | 60       |
| mysqlx_interactive_timeout        | 28800    |
| mysqlx_port_open_timeout          | 0        |
| mysqlx_read_timeout               | 30       |
| mysqlx_wait_timeout               | 28800    |
| mysqlx_write_timeout              | 60       |
| net_read_timeout                  | 30       |
| net_write_timeout                 | 60       |
| rpl_stop_slave_timeout            | 31536000 |
| slave_net_timeout                 | 60       |
| wait_timeout                      | 28800    |
+-----------------------------------+----------+
20 rows in set (0.00 sec)

mysql> set interactive_timeout 31536000;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '31536000' at line 1
mysql> set interactive_timeout=31536000;
Query OK, 0 rows affected (0.00 sec)

mysql> set wait_timeout=31536000;
Query OK, 0 rows affected (0.00 sec)

mysql> 

将interactive_timeout和wait_timeout设置成长一些,设置31536000即1年。

 

方法2:

修改MySQL的参数,interactive_timeout和wait_timeout设置为31536000即1年,在my.cnf修改: 

[mysqld] 
wait_timeout=31536000 
interactive_timeout=31536000 

mysql需要重启才生效

你可能感兴趣的:(mysql5 及以上版本 8小时会断开连接, autoReconnect=true 设置了不生效解决方法)