1

I have the following situation:

I can

  • connect to my mysql database via the command line tool mysql, both from the local machine and a remote host
  • connect from a small java test program from the remote host

I can not

  • connect from that same small java test program from the local machine

The test code is basically the same as in this question and I followed all the suggestions given there to solve my problem with no avail. I have tried the following connection strings:

  1. jdbc:mysql://localhost/database
  2. jdbc:mysql://localhost:3306/database
  3. jdbc:mysql://127.0.0.1/database
  4. jdbc:mysql://hostname/database

all resulting in the same error message. On the remote machine I could use the fourth alternative with no problem.

I am running a mysql-5.5 server. The remote uses java version "1.7.0_101" and the local machine has java version "1.6.0_38".

Here is the error message when I run my java program on the local machine:

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!
Community
  • 1
  • 1
mjaga
  • 23
  • 2
  • the mysql server is running on the local machine, right? – sobrino Jun 17 '16 at 13:12
  • 1
    is it possible that the MySQL server is configured **only** to the external IP. see in /etc/my.cnf the bind parameter. Then you can reach the DB only from extern and over the unix socket. there a two was to resolve this problem: 1) change the bind to 0.0.0.0 the the server listen on each network, or use the external IP in the connection string. – Bernd Buffen Jun 17 '16 at 13:21
  • I'm really tempted to close as duplicate of http://stackoverflow.com/questions/6865538/solving-a-communications-link-failure-with-jdbc-and-mysql but I will leave this open for now. You really need to provide some more information (eg the `bind-address` config, and the user privileges (eg maybe the user isn't allowed to connect from localhost)). – Mark Rotteveel Jun 17 '16 at 17:22
  • the fact that I **am able** to connect locally via the `mysql` tool should mean that the server is not configured only for external IPs and that there are no problems with user prvileges, right? It's really only the local java connection that is failing, using code that works from the external machine. – mjaga Jun 20 '16 at 07:16

1 Answers1

0

I just realized that mysql -u user database would work, but not mysql -u user -h 127.0.0.1 database, so it turned out I had an error in my /etc/hosts.allow file. Problem solved!

mjaga
  • 23
  • 2