Thelastpacketsentsuccessfullytotheserverwas0milliseconds

Thelastpacketsentsuccessfullytotheserverwas0millisecondsago.

Using Connector/J to connect to the MySQL database , the following error will be reported after the program runs for a long time:

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。

The error will also prompt you to modify wait_timeout or use the autoReconnect property of Connector/J to avoid the error.

Later, after checking some information, I found that there are quite a few people who have encountered this problem. Most of them only use the connection pool method to have this problem. It should be difficult for short connections to have this problem. Reason for this problem:

The default "wait_timeout" of the MySQL server is 28800 seconds or 8 hours, which means that if a connection is idle for more than 8 hours, MySQL will automatically disconnect the connection, but the connection pool thinks the connection is still valid (because the Check the validity of the connection), when the application applies to use the connection, it will cause the above error.

Solution:
(1) Use the autoReconnect property in the JDBC URL, add the url

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

(2) Modify the parameters of MySQL. /etc/my.cnf add

?
1
2
3
[mysqld]
wait_timeout=31536000
interactive_timeout=31536000

(3) Restart mysql

?
1
service mysql restart

Reason analysis:
(1) In the case of a large amount of data access, the mysql connection connection may fail

(2) Ask for a long time, the connection will fail

转自:https://www.2cto.com/database/201703/616486.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325169652&siteId=291194637