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.

6
  • stackoverflow.com/questions/2983248/… Commented Apr 24, 2016 at 18:37
  • @FastSnail I tried all of BalusC tips, and still having the same problem. Commented Apr 24, 2016 at 20:45
  • read this one too stackoverflow.com/questions/6865538/… Commented Apr 25, 2016 at 2: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. Commented Oct 4, 2021 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 Commented Nov 23, 2021 at 18:41

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

At my end the status shows running, and still facing issue.
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 

Comments

0

You have to use database name instead of table name

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.