0

I'm doing a group project with Java, JavaFX and MySQL, and I have a big problem when trying to connect to my database with JDBC. I have systematically this error thrown by DriverManager.getConnection() method :

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.

EDIT: Here is the whole stack trace :

com.mysql.cj.jdbc.exceptions.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.
        at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:175)
        at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
        at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:825)
        at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:446)
        at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:239)
        at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:188)
        at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:681)
        at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:252)
        at controller.Controller.dbconnect(Controller.java:34)
        at controller.Controller.<init>(Controller.java:18)
        at App.start(App.java:15)
        at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
        at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
        at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
        at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
        at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
        at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
        at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:290)
        at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: com.mysql.cj.exceptions.CJCommunicationsException: 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.
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
        at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
        at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
        at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:62)
        at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105)
        at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:150)
        at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:166)
        at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:89)
        at com.mysql.cj.NativeSession.connect(NativeSession.java:121)
        at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:945)
        at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:815)
        ... 17 more
Caused by: java.net.ConnectException: Connection refused
        at java.base/sun.nio.ch.Net.connect0(Native Method)
        at java.base/sun.nio.ch.Net.connect(Net.java:579)
        at java.base/sun.nio.ch.Net.connect(Net.java:568)
        at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:588)
        at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327)
        at java.base/java.net.Socket.connect(Socket.java:633)
        at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:153)
        at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:63)
        ... 20 more

The code throwing the error is :

public Connection dbconnect() {
    Connection conn = null;
    try {
      conn = DriverManager
          .getConnection(
              "jdbc:mysql://127.0.0.1:3306/cookbook?user=root&password=ABC&useSSL=false");
    } catch (SQLException e) {
      System.out.println(e.getMessage());
    }
    return conn;
  }

I'm using VSCode in Windows 10 and a Debian WSL to compile. I installed MySQL Server, Workbench and Connector/J version 8.0.33.

I already spent hours trying to fix that. It works perfectly for the other members of the group and I can connect to an online database (using this), so I can be sure that the problem comes from my machine. I found a lot of different possible causes and solutions online, so many that I could not enumerate them all but here are some :

  • Disabling firewall
  • Modify the jdbc URL in various ways
  • Modify the my.ini file
  • Reinstalling MySQL
  • Downgrading MySQL
  • Asserting MySQL has good permissions

(Basically everything that is contained there, and there.)

Nothing worked, I'm running out of options and ideas, so maybe you can have a new solution I never tried.

Astruin
  • 1
  • 2
  • 1
    Can you connect from `mysql` command line client? – tostao Apr 24 '23 at 12:41
  • Try ```telnet 127.0.0.1 3306``` from the command line to make sure you can connect to port 3306 over TCP. If this works without an error, that means MySQL is connecting on localhost over that port. You may need to install ```telnet```, as it is an optional package. A Windows feature or a Linux package - based on your OS. If this works fine, that means, there is no issue with connectivity to your DB. – Ishan Apr 24 '23 at 12:48
  • @tostao Yes it works. @Ishan With WSL I got `Unable to connect to remote host: Connection refused` and `Got timeout reading communication packets` with Windows cmd, so it looks like it cannot connect through TCP. – Astruin Apr 24 '23 at 13:07
  • Not an answer, just some notes: When you use `e.getMessage()`, you are throwing away most of the stack trace. Use `e.printStackTrace(System.err);` just for now, to get the full stack trace (but it's better to use a proper logging framework). Add the full stack trace to your question (as formatted text). You can also review the notes in [Connect Java to a MySQL database](https://stackoverflow.com/q/2839321/12567365). – andrewJames Apr 24 '23 at 13:07
  • @andrewJames MySQL Workbench works fine, I can connect to the db and see the tables. It's only jdbc. I updated my question with the stack trace. Concerning the link you sent, I already tried everything from it. – Astruin Apr 24 '23 at 13:20
  • _"Caused by: java.net.ConnectException: Connection refused"_. MySQL isn't running, or the port is wrong, or it is actively blocked by a firewall. – Mark Rotteveel Apr 30 '23 at 13:47

0 Answers0