Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up [duplicate]

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.

like image 792
Diet Cookiez Avatar asked Apr 24 '16 18:04

Diet Cookiez


2 Answers

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 to connection to database.

like image 124
Lucky Avatar answered Sep 20 '22 15:09

Lucky


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 
like image 22
Hemant Thorat Avatar answered Sep 19 '22 15:09

Hemant Thorat