0

I'm struggling to create a super simple JDBC connection to a newly created MySQL database on my local station. The weird issue I have is that MySQLWorkbench and TablePlus can connect without a problem to my database using both the same username/password all running locally.

Here's the super simple code I have to connect

    public static void main(String[] args) throws Exception {
        System.out.println("Connecting to localhost MySQL");
        Connection conn = getConnection("jdbc:mysql://127.0.0.1:3306/mod_explorer_restore", "username", "password");
        conn.isValid(1000);

    }

    public static Connection getConnection(String url, String user, String pwd)
            throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
        String strDriver = "com.mysql.cj.jdbc.Driver";
        String strUrl = url;
        String strUser = user;
        String strPass = pwd;
        System.out.println("Loading MySQL driver '" + strDriver + "'");
        Class.forName(strDriver).newInstance();
        return DriverManager.getConnection(strUrl, strUser, strPass);
    }

And the console log after the execution.

Connecting to localhost MySQL
Loading MySQL driver 'com.mysql.cj.jdbc.Driver'
Exception in thread "main" 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:174)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835)
    at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:455)
    at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
    at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at com.modulusdata.tools.retries.ManualDBConnection.getConnection(ManualDBConnection.java:24)
    at com.modulusdata.tools.retries.ManualDBConnection.main(ManualDBConnection.java:11)
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 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151)
    at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167)
    at com.mysql.cj.protocol.a.NativeProtocol.readMessage(NativeProtocol.java:562)
    at com.mysql.cj.protocol.a.NativeProtocol.readServerCapabilities(NativeProtocol.java:514)
    at com.mysql.cj.protocol.a.NativeProtocol.beforeHandshake(NativeProtocol.java:404)
    at com.mysql.cj.protocol.a.NativeProtocol.connect(NativeProtocol.java:1447)
    at com.mysql.cj.NativeSession.connect(NativeSession.java:165)
    at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:955)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:825)
    ... 7 more
Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
    at com.mysql.cj.protocol.FullReadInputStream.readFully(FullReadInputStream.java:67)
    at com.mysql.cj.protocol.a.SimplePacketReader.readHeader(SimplePacketReader.java:63)
    at com.mysql.cj.protocol.a.SimplePacketReader.readHeader(SimplePacketReader.java:45)
    at com.mysql.cj.protocol.a.NativeProtocol.readMessage(NativeProtocol.java:556)
    ... 13 more

Now this is usually straightforward so I'm wondering if there is something that needs to be done on OS X to have the Java execution working to connect to MySQL.

Sergio
  • 590
  • 1
  • 9
  • 16
  • Check this link. https://stackoverflow.com/questions/2983248/com-mysql-jdbc-exceptions-jdbc4-communicationsexception-communications-link-fai – Sambit May 21 '19 at 14:25
  • Maybe you need to add localhost 127.0.0.1 to your Mac /etc/hosts file – duffymo May 21 '19 at 14:28

0 Answers0