Richard Rhyan
Richard Rhyan

Reputation: 213

SQLException: Communications link failure (Java/mysql)

Can someone explain to me why this line works:

conn = DriverManager.getConnection("jdbc:mysql://myWebsite.com:3306/schemaName?user=userX&password=passwordX");

But this line does not:

conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/schemaName?user=userX&password=passwordX");

I get a Communications Link Failure when attempting to access through the localhost (or 127.0.0.1). However, I'm able to access the database via localhost through PHP and the MySQLQuery Browser and MySQL Aministrator.

If needed here's the entire method I'm using:

public Database() throws Exception {
Class.forName("com.mysql.jdbc.Driver").newInstance();

try {
  conn = DriverManager.getConnection("jdbc:mysql://myWebsite.com:3306/schemaName?user=userX&password=passwordX");
  // Next line does not work.
  //    conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/schemaName?user=userX&password=passwordX");
  } catch (SQLException ex) {
      displaySQLException(ex); // Seperate routine to display errors.
  }
}

Thanks for any help, Richard

Upvotes: 0

Views: 6639

Answers (4)

A.R.K.S
A.R.K.S

Reputation: 1802

In my case, I faced this error when trying to connect to mysql-server running inside a container on a Ubuntu host VM from the same host.

Example: If my VM name is abc.company.com, the following jdbc URL would not work:

jdbc:mysql://abc.company.com:3306/dbname

Above jdbc url would work fine from other machines like xyz.company.com but just not abc.company.com.

where as the following jdbc URL would work just fine on abc.company.com machine:

jdbc:mysql://localhost:3306/dbname

which led me to check the /etc/hosts file.

Adding the following line to /etc/hosts fixed this issue:

127.0.1.1 abc.company.com abc

This seems to be an OS bug that requires us to add these on some Ubuntu versions. Reference: https://www.debian.org/doc/manuals/debian-reference/ch05.en.html#_the_hostname_resolution

Before trying this, I had tried all other solutions like GRANT ALL.., changing the bind-address line in mysql.cnf.. None of them helped me in this case.

Upvotes: 0

James Allman
James Allman

Reputation: 41158

It's possible your mysqld is binding specifically to the ethernet interface instead of all interfaces (0.0.0.0) or the localhost interface (127.0.0.1).

On a *nix platform you can check which interface the daemon is listening on with the following command:

$ netstat -ln|grep 3306
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN

Upvotes: 2

Sunil Kumar B M
Sunil Kumar B M

Reputation: 2795

This might happen for many reasons, like

  1. MySQL server might be stopped on the target machine
  2. MySQL might be configured not to accept remote connections
  3. The firewall might be blocking remote connections on port 3306

Upvotes: 0

Harry Joy
Harry Joy

Reputation: 59660

In second code:

conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/schemaName?
                user=userX&password=passwordX");

It will try to connect to mysql on machine on localhost (on which the code is running). In your case it might be possible that mysql on your machine or from where you are running the code is not available or stopped or usename/password you are passing are not valid or schemaname does not exist. But on myWebsite.com it is up.

There is nothing wrong in your code. Make sure mySql is installed and running and username/password are valid, a schema with provided schemaname exists on machine on which you run this code with localhost.

Upvotes: 0

Related Questions