The last packet sent successfully to the server was 0 milliseconds ago.

Posted jerry_sun16

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了The last packet sent successfully to the server was 0 milliseconds ago.相关的知识,希望对你有一定的参考价值。

使用Connector/J连接MySQL数据库,程序运行较长时间后就会报以下错误:

Communications link failure,The last packet successfully received from the server was *** millisecond ago.The last packet successfully sent to the server was ***  millisecond ago。

其中错误还会提示你修改wait_timeout或是使用Connector/J的autoReconnect属性避免该错误。

后来查了一些资料,才发现遇到这个问题的人还真不少,大部分都是使用连接池方式时才会出现这个问题,短连接应该很难出现这个问题。这个问题的原因:

mysql服务器默认的“wait_timeout”是28800秒即8小时,意味着如果一个连接的空闲时间超过8个小时,MySQL将自动断开该连接,而连接池却认为该连接还是有效的(因为并未校验连接的有效性),当应用申请使用该连接时,就会导致上面的报错。


解决方法:
(1)使用JDBC URL中使用autoReconnect属性,url添加

&autoReconnect=true&failOverReadOnly=false&maxReconnects=10

(2) 修改MySQL的参数. /etc/my.cnf 添加

[mysqld]
wait_timeout=31536000
interactive_timeout=31536000

(3)重启mysql

service mysql restart

原因分析:
(1)大量数据访问情况下,mysql connection连接有可能失效
(2)长时间不妨问,connection会失效

以上是关于The last packet sent successfully to the server was 0 milliseconds ago.的主要内容,如果未能解决你的问题,请参考以下文章

The last packet sent successfully to the server was 0 milliseconds ago.

The last packet sent successfully to the server was 0 milliseconds ago.

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received

Springboot项目启动:报错The last packet sent successfully to the server was 0 milliseconds ago

MysqlCommunications link failure,The last packet sent successfully to the server was 0 millisecond

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received