[Solved] MySQL connection error: communications link failure

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:Communications link failure

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.

1. Causes

Communication link failure occurs when Java connects to the database. Why can’t you log in

2 solution (my situation is invalid)

Method 1

[1] Login mysql,cmd command
mysql -h host address -u username -p user password (for example mysql -hlocalhost -uroot -p123456)

[2] Check the wait_timeout,cmd command.
show global variables like 'wait_timeout';


[3] If the wait_timeout is too small, modify it. cmd command:
set global wait_timeout=604800;
set global interactive_timeout=604800;

Method 2

Add the following parameter to the connection URL: &autoReconnect=true

3. Troubleshooting of other causes

If the above two methods are not solved, check whether there are problems in the server environment and database port.

After investigation, if the database port passes through nginx reverse proxy, or if the database port performs proxy operations through other servers, it is caused by nginx reverse proxy timeout that the database cannot be connected

You need to modify the configuration file of nginx and use steam agent Mysl

stream {
     upstream mysql {
         zone myapp1 64k;
         server localhost:3306 weight=1 max_fails=3 fail_timeout=30s;
     }
     server {
         listen 10086;
         proxy_connect_timeout 1s;
         proxy_timeout 3s;
         proxy_pass mysql;
    }
}

In other cases, you can also check whether the MySQL port has been proxy

Read More: