无法从JDBC连接到MySQL数据库,但是MySQLWorkbench可以

时间:2019-05-21 14:23:09

标签: java mysql macos jdbc

我正在努力创建与本地工作站上新创建的MySQL数据库的超简单JDBC连接。我有一个奇怪的问题是,MySQLWorkbench和TablePlus可以使用都在本地运行的相同用户名/密码毫无问题地连接到我的数据库。

这是我必须连接的超级简单的代码

    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);
    }

执行后还有控制台日志。

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

现在这通常很简单,所以我想知道在OS X上是否需要做一些事情才能使Java执行工作连接到MySQL。

0 个答案:

没有答案