Why MySQL Connections Fail on CentOS with Spring Boot and How to Fix Them
When deploying a Spring Boot application on CentOS, MySQL connections may throw CommunicationsException or SSLHandshakeException due to JDK mismatches, socket path errors, SSL settings, timeout defaults, or configuration issues, and this guide explains each cause and provides concrete fixes.
Problem Overview
A Spring Boot project runs fine locally and can connect to the MySQL server from the command line, but when the same application is deployed on a CentOS server it throws a CommunicationsException indicating a communications link failure.
Exception Details
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.
... (stack trace) ...
Caused by: java.net.ConnectException: Connection refused: connectRoot Causes & Solutions
1. Incompatible JDK Version
The server originally used the OpenJDK package installed via yum, while the local machine used the Oracle JDK 1.8.0_151. Reinstalling the Oracle JDK 1.8.0_241 on the server resolved the connection problem.
2. Incorrect mysql.sock Path
After moving MySQL data to a new disk and updating datadir, the socket file location changed (default locations are /var/lib/mysql/mysql.sock or /tmp/mysql.sock). Updating all MySQL client sections ([client], [mysql], [mysqld]) to use the same socket path fixed the Communications link failure error.
3. SSL Connection Issues
When the JDK was upgraded to 1.8, an SSLHandshakeException may appear:
javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
at sun.security.ssl.Handshaker.activate(Handshaker.java:529)
...Removing SSLv3 from the jdk.tls.disabledAlgorithms entry in $JAVA_HOME/jre/lib/security/java.security resolves the issue. If MySQL is configured to use SSL, follow the standard MySQL SSL setup instructions.
4. MySQL Wait Timeout
The default wait_timeout is 8 hours (28800 s). When a connection stays idle longer, MySQL closes it, but the connection pool may still consider it valid, leading to the same exception during runtime.
Fix: increase the timeout in my.ini or add &autoReconnect=true to the JDBC URL. Example configuration:
wait_timeout=31536000
interactive_timeout=31536000Restart MySQL after editing.
5. Connection Pool Idle Time
If the pool’s maxIdleTime is set to 0, idle connections are never discarded, causing stale connections to be reused. Setting maxIdleTime to a reasonable value (e.g., 20 seconds) prevents the error.
6. Other Common Causes
Database account permissions – ensure the IP and user are authorized.
Network firewall – open the MySQL port (default 3306).
Incorrect port number or blocked port.
Wrong password or missing IP‑specific privileges.
Driver version mismatch with the MySQL server.
Unstable network causing intermittent drops.
Connection pool size too large for the MySQL max connections.
IPv4 vs IPv6 address conflicts.
By checking each of these items, developers can quickly pinpoint and resolve MySQL connection failures on CentOS deployments.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Senior Brother's Insights
A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
