0

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

1
  • does "userX" has the permission to access the database from the localhost? May be PHPandMySQl query browser and MySQL administrator uses a different user who can access to the database from the localhost. Commented Feb 14, 2012 at 4:33

4 Answers 4

2

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 
Sign up to request clarification or add additional context in comments.

2 Comments

I'm not all that familiar with java and/or mysql... so I don't understand why this worked, when I had no problems with other apps. Thanks to all who responded.
@RichardRhyan The other applications may have been using a socket interface instead of tcp.
0

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.

Comments

0

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

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.