【Mysql】Communications link failure,The last packet sent successfully to the server was 0 millisecond

The background of the project is that the database and the project are not under the same server. When starting, I suddenly encountered the following error:

  Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

try

After running the project, the above error occurred, and I still reported the error after many attempts, but I found that using the client Navicat to connect to the database, it can be connected normally...

therefore

Through search and exploration, the summary is as follows:
1. Learn the difference between interactive links and non-interactive links

1. Interactive connection
refers to the client connection, which connects to the MySQL server, and the client connection has a reconnection mechanism (the operation SQL will automatically connect after disconnection) (1) Instruct the connection
terminal
command line to execute the connection instruction, that is Can connect to mysql server.
$ mysql -hlocalhost -p3306 -uroot -p
or
$ mysql -uroot -p
Enter password:

(2) Tool connection
For example, navicat is a database management tool, but when connecting to MySQL, it also encapsulates the client connection instructions inside.
mysql -h$ip -P$port -u$user -p

2. Non-interactive connection
refers to non-client connection, such as application creation connection pool, JDBC connection~

At the beginning of this article, I mentioned that the error caused by my application accessing MySQL refers to the disconnection of the non-interactive connection.

Two, MySQL connection parameters

The autoReconnect
database connection is abnormal, whether to automatically connect, autoReconnect=true means automatic connection

failOverReadOnly
database connection, whether it is set to read-only mode, failOverReadOnly=false means read-only mode

wait_timeout
wait_timeout connection timeout, indicating non-interactive connection (non-interactive connection, wait_timeout takes effect), how long the connection is idle, MySQL will be cut off. The default is 28800 (in seconds, that is, 8 hours), which is obviously relatively short. Therefore, there are often small partners who report that the next day, when they run the project and request to access the database, they will throw the information of abnormal database connection.

interactive_timeout
interactive_timeout indicates that after an interactive connection (non-interactive connection, interactive_timeout takes effect), how long the connection is idle will be cut off by MySQL. The default is 28800 (in seconds, that is, 8 hours).

problem solved

Due to the different causes of the problem, here are several ways for you to try to solve it.

1. One is to delete the SSL=true option, but it will report

Establishing SSL connection without server’s identity verification is
not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+
requirements SSL 。。。。等等警告

Then assign useSSL to false so that there will be no warning.

2. Another way, which is what I solved

Since wait_timeout is 8 hours by default, if an idle connection exceeds 8 hours, MySQL will automatically disconnect, but the connection pool thinks that the connection is available, and then it is equivalent to using an invalid connection, and then an error occurs...

configuration operation

2.1 Configuration Valid Scope

    2.1.1 Command configuration (session takes effect immediately)
    MySQL> set wait_timeout 604800;

    MySQL> set interactive_timeout 604800;
    This method is only valid for the current session connection. After the connection is disconnected and reconnected, the wait_timeout configured by the set will be invalid and replaced by the global interactive_timeout parameter value.

    2.1.2 Command configuration (global effective immediately)
    MySQL> set global 604800;

    MySQL> set global interactive_timeout 604800;
    It will take effect immediately after configuration. For all global connections, even if the connection is disconnected and reconnected, the wait_timeout configured by set will still take effect.

    2.1.3 File configuration (effective after restarting mysql globally)
    vi /etc/my.cnf
    modify the MySQL configuration file. This method is for all connections and has the highest priority. After modifying the parameter value, restart the MySQL service to take effect, and it will overwrite all the wait_timeout and interactive_timeout configured by the set command parameter value.

2.2 Parameter configuration (problem solution)
    2.2.1 Configure connection parameters
    By modifying the database connection parameters of the application configuration file jdbc.properties, add the content in red:
    jdbc.url=jdbc:mysql://192.168.0.109:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&autoReconnect=true&failOverReadOnly=false

    Indicates that the automatic reconnection mechanism is adopted. This method may not take effect. Continue to configure the connection timeout period~

    2.2.2 Configure the connection timeout
     command configuration (method 1)
    This method is more suitable for scenarios where you do not need to restart the MySQL service and modify the global wait_timeout timeout time to take effect immediately~

    The first step: connect to mysql
    $ mysql -uroot -p

    Step 2: View wait_timeout
    mysql> show global variables like '%timeout%';

    Step 3: Modify the time

    mysql> set global wait_timeout=604800;
    mysql> set global interactive_timeout=31536000;

    Parameter value description: 604800=7 days, 31536000=365 days

    Check wait_timeout again

    mysql> show global variables like '%timeout%';

    File configuration (method 2)
    By modifying the MySQL configuration file and restarting the MySQL service to take effect, caution is required. Friends who cannot restart the MySQL server can achieve it through method 1~

    Step 1: Modify the configuration file

    vi /etc/my.cnf

    The configuration file my.cnf of the Linux operating system MySQL generally exists in /etc/my.cnf, and the actual installation shall prevail~

    Add the following after [mysqld] (604800 seconds = 7 days, 31536000 = 365 days)

    wait_timeout=604800
    interactive_timeout=31536000
    Step 2: Restart MySQL

    After modifying the my.cnf configuration file, restart mysql to take effect! ! ! !

    $ service mysql restart

Restart needs to be cautious, and must be considered according to the actual situation! ! !

Finally, resend the request to access the application to see if the error of connecting to the database is still reported!

Guess you like

Origin blog.csdn.net/weixin_43431218/article/details/128164730