In this article I will explain how to resolve com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure exception
Before you proceed with the below solutions make sure your MySQL server is running in good condition with out issues.
Solution for com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
There are several reasons you would get this error the first reason you need to check is if there is a below warning above this error message.
Mon Dec 12 10:47:57 PST 2022 WARN: 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 connection must be established by default if explicit option isn't set.
For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'.
You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Exception in thread "main" com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
If so, you can solve this issue by adding the verifyServerCertificate property to your JDBC URL as shown below.
jdbc:mysql://localhost:3306/database_name?enabledTLSProtocols=TLSv1.2
In my case, after adding TLS protocol to this verifyServerCertificate
property my issue is solved.
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/database_name?enabledTLSProtocols=TLSv1.2", "root", "root");
Confirm the Server & Database Details
You would also get this error when you are unable to connect to the MySQL database servers as it might be down or some of your connection properties using are not right. So make sure the following.
- Make sure the MySQL database server is running
- Check IP address or hostname in JDBC URL is wrong.
- The hostname in JDBC URL is not recognized by the local DNS server.
- The Port number is missing or wrong in JDBC URL.
- DB server doesn’t accept TCP/IP connections.
- DB server has run out of connections.
- Make sure the firewall is open between your system and the database server.
Use the IP Address Instead localhost
Sometimes you can’t connect to the MySQL database with the localhost, so find the IP address where the MySQL server is running and use it instead of the localhost to solve com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure.
Connection con = DriverManager.getConnection(
"jdbc:mysql://192.168.1.1:3306/database_name?enabledTLSProtocols=TLSv1.2", "root", "root");
Upgrade the MySQL Connector Jar version
Sometimes you will also get this error by a version mismatch between the Java MySQL connector jar you are using and the MySQL server version.
Running MySQL server version mysql-8.0.31
and using the old MySQL connector mysql-connector-java-6.0.6.jar
also gets this error. so change the version of MySQL connector.
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>8.0.13</version>
</dependency>
I hope the above solutions helped resolve your problem com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure. If not, please use the below link for more solutions.
Related Articles
- java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver
- PySpark Read and Write MySQL Database Table
- Spark Read and Write MySQL Database Table
- SOLVED: java.sql.SQLException: Unable to load authentication plugin ‘caching_sha2_password’.
- PySpark Read and Write SQL Server Table
I battled this for 2 days, and I finally solved it. For me it was the text encoding in Eclipse:
1. Right click project name in project explorer and choose Properties
2. General > Workspace
3. In there is Text file encoding. Mine was set to UTF-16.
I changed it to UTF-8 and it fixed the problem!