16

I'm having trouble connecting to my database. I think there is a setting somewhere that I don't know about and needs to be changed. I have some really basic code here:

public static void main(String[] args)
{
    try
    {
        Connection con = DriverManager.getConnection("jdbc:mysql://IP:3306/TABLENAME?autoReconnect=true","USERNAME", "PASSWORD");
        con.close();
    }

    catch(Exception e)
    {
        e.printStackTrace();
    }
}

Obviously with the login info. And it causes this error:

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.

Caused by: 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.

Caused by: java.net.ConnectException: Connection refused

I've looked through about 10 different posts about the same problem and tried their solutions and still couldn't get it to work.

Kainix
  • 1,186
  • 3
  • 21
  • 33
Diet Cookiez
  • 161
  • 1
  • 1
  • 5
  • http://stackoverflow.com/questions/2983248/com-mysql-jdbc-exceptions-jdbc4-communicationsexception-communications-link-fai – Madhawa Priyashantha Apr 24 '16 at 18:37
  • @FastSnail I tried all of BalusC tips, and still having the same problem. – Diet Cookiez Apr 24 '16 at 20:45
  • read this one too http://stackoverflow.com/questions/6865538/solving-a-communications-link-failure-with-jdbc-and-mysql – Madhawa Priyashantha Apr 25 '16 at 02:18
  • Linked answers do not cover the problem space. The error in the header is not mentioned in the provided links. In my case, it turns out that my old `MySql5.7` wanted to use `TLSv1.1`, which has been blocked by the `OpenJDK v11.0.11` release. To allow it again, you can remove `TLSv1.1` from the list at `jdk.tls.disabledAlgorithms` in `openjdk64-11.0.11/conf/security/java.security`. This is one possible solution to one possible cause of this problem. Please reopen the question. – Casper Lehmann Oct 04 '21 at 14:23
  • Please check your mysql server version. it should be compatible to mysql connector which version you mentioned in dependency of mysql connector in pom.xml – Muhammad Hamed K Nov 23 '21 at 18:41
  • I am facing the same issue. On running "FLUSH HOSTS;" command it again starts connecting the server. But I dont want FLUSH HOSTS; rather find the root cause. Any leads? – Suresh Huse Jun 27 '23 at 07:01

3 Answers3

4

This problem occurs when the MySQL service is not running and you're trying to connect to the mysql database.

To resolve this in Windows,

  • Start >> type 'services' and open it.

  • Find the MySQL service. Probably with name MySQLxx. xx being the version.
    eg. MySQL56, MySQL57 etc

  • Start the service and now try the connection to database.

Lucky
  • 16,787
  • 19
  • 117
  • 151
2

Increase Mysql max connection,

SET GLOBAL max_connections = 1500;  

If still getting an error then queries might be taking too long to respond.

In that case increase attempt while creating jdbc connection,

&maxReconnects=XX 
Hemant Thorat
  • 2,386
  • 20
  • 14
0

You have to use database name instead of table name