2

None of the solutions I have found online works. I keep hitting

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

I am trying to connect to a MySQL db from another server. I can assure you that the url, user and password information is correct as it works when I try to connect the db using PgAdmin software. However, it fails when I try executing using Java code (via NetBean 8.1). Ok, so the following is my code:

import oracle.jdbc.pool.OracleDataSource;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Client9{
      public static void main(String[] args) {
           System.out.println("MySQL Connect Example.");
           Connection conn = null;
           String url = "jdbc:mysql://MySQLUrl:3306/";
           String dbName = "MySQL_dbName";
           String driver = "com.mysql.jdbc.Driver";
           String userName = "user"; 
           String password = "password";
           try {
                Class.forName(driver).newInstance();
                conn = DriverManager.getConnection(url+dbName,userName,password);
                System.out.println("Connected to the database");
                conn.close();
                System.out.println("Disconnected from database");
           } catch (Exception e) {
                e.printStackTrace();
           }
      }

}

Error message is shown below:

run:
MySQL Connect Example.
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:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1117)
at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:673)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1084)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2465)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2498)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2283)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:822)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
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:422)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:404)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at netiqcustomprov.Client9.main(Client9.java:35)
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:3052)
at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:597)
... 16 more
BUILD SUCCESSFUL (total time: 1 minute 0 seconds)

Solutions I have tried so far: I have imported ojdb14.jar and mysql-connector-java-5.1.23-bin.jar into the library. I have tried all 3 different methods getConnection()

  • getConnection(String url)
    (where my string url = "jdbc:mysql://MySQLUrl:3306/MySQL_dbName?user=user&password=password")
  • getConnection(String url, Properties info)
  • getConnection(String url, String user, String password)

I have also tried to register the driver using the following command:

Driver myDriver = new oracle.jdbc.driver.OracleDriver();
DriverManager.registerDriver( myDriver );

One of the solution I found is to comment the "skip-networking" rule in my.cnf file. However, I could not find such file in the server. (The server operates based on Linux OS)

Greatly appreciate the assistance in solving this issue. Thanks in advance.

UPDATE: See my answer. Once again I apologise for my silly mistake.

Shubham Avasthi
  • 192
  • 4
  • 16
Kristin.Y
  • 39
  • 1
  • 10
  • The oracle thing has nothing to do here. Can we see a `sudo lsof -i:3306` ran on this linux server, also did you try a `mysql -u user -p` on the command line (again on this server) –  May 11 '16 at 04:40
  • A `ping MySQLUrl` from the machine running the java code might also help the diagnostic –  May 11 '16 at 04:44
  • Is MySQL server running? Are you able to connect from any mysql client ? – Gangaraju May 11 '16 at 04:53
  • you run `PgAdmin` from same server as Java program? – Sabir Khan May 11 '16 at 04:53
  • why don't you try **localhost** keyword instead of **mysqlurl**, I am not sure how you change name.. – bananas May 11 '16 at 05:36
  • @RC. I have run `sudo lsof -i:3306` but no response return. I have also run `mysql -u user -p` but return "If 'mysql' is not a typo you can run...." – Kristin.Y May 11 '16 at 05:57
  • @RC. The ping was successful. There is a reply from the mysql server – Kristin.Y May 11 '16 at 05:59
  • @Gangaraju As I have said earlier, the server is running. I am able to connect to the server via PgAdmin – Kristin.Y May 11 '16 at 06:00
  • @Sabir Khan Yes I am running `PgAdmin` from the same server as my Java program (which is my laptop by the way) – Kristin.Y May 11 '16 at 06:01
  • 1
    [Have you read this question and answer?](http://stackoverflow.com/questions/2983248/com-mysql-jdbc-exceptions-jdbc4-communicationsexception-communications-link-fai) – ujulu May 11 '16 at 06:03
  • @holidayCoder Actually mysqlurl represent IP address for 192.168.X.XXX. The mysql server is not the same as my localhost. The database is in different server. I am trying to connect the db using java code from my laptop – Kristin.Y May 11 '16 at 06:03
  • the "lsof" was to list processes listening to 3306, so you don't have any mysql server listening on 3306 if you have no output.. Also note that AFAIK pgadmin is for postgresql not mysql –  May 11 '16 at 06:43

1 Answers1

0

Finally I know what was the error. It was really a stupid mistake. It was a postgres db instead of mysql. So, I just changes the url and driver.

String url = "jdbc:postgresql:://MySQLUrl:3306/";
String driver = "org.postgresql.Driver";

Sorry my mistakes and thanks to the people that have tried to help me.

Kristin.Y
  • 39
  • 1
  • 10