远程连接工作时无法从本地连接到mysql的mysql

时间:2016-06-17 13:08:12

标签: java mysql jdbc

我有以下情况:

我可以

  • 通过命令行工具mysql连接到我的mysql数据库,来自本地计算机远程主机
  • 远程主机
  • 的小型java测试程序进行连接

我不能

  • 本地计算机
  • 连接相同的小型java测试程序

测试代码与this question中的测试代码基本相同,我遵循了那里给出的所有建议,无法解决我的问题。我尝试了以下连接字符串:

  1. JDBC:MySQL的://本地主机/数据库
  2. JDBC:MySQL的://本地主机:3306 /数据库
  3. JDBC:MySQL的://127.0.0.1/database
  4. JDBC:MySQL的://主机名/数据库
  5. 所有导致相同的错误消息。在远程机器上,我可以毫无问题地使用第四种替代方案。

    我正在运行mysql-5.5服务器。远程使用java版本“1.7.0_101”,本地机器使用java版本“1.6.0_38”。

    以下是在本地计算机上运行java程序时的错误消息:

    Connecting to database...
    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.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:534)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1116)
    at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:688)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1094)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2337)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2370)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2154)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:792)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:534)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:381)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
    at java.sql.DriverManager.getConnection(DriverManager.java:615)
    at java.sql.DriverManager.getConnection(DriverManager.java:195)
    at FirstExample.main(FirstExample.java:22)
    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.jdbc.MysqlIO.readFully(MysqlIO.java:2540)
    at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:612)
    ... 16 more
    Goodbye!
    

1 个答案:

答案 0 :(得分:0)

我刚刚意识到mysql -u user database可行,但不是mysql -u user -h 127.0.0.1 database,所以事实证明我的/etc/hosts.allow文件中有错误。问题解决了!