0

I am trying to connect to mysql database using jdbc in my java program. I am using a RHEL 6 machine to connect to the mysql database. I have opened the port 3306 in this machine and if I do, mysql -u root -p the mysql database connects.

If I do, mysql -h servername.edu -u root -p the mysql database connects.

From my java program,

String url = "jdbc:mysql://localhost:3306/";

Works.

Now, if I change the localhost to servername, it doesn't work.

String url = "jdbc:mysql://server.com:3306/";

I get the error as,

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure to remote databse

Before posting the question, I did a fair bit of research on this exception from here.

I have setup the my.cnf file correctly and I do not see an issue in that file. What am I missing here? The normal ping to the server works fine as well.

Community
  • 1
  • 1
Ramesh
  • 765
  • 7
  • 24
  • 52
  • the url must be like: String url = "jdbc:mysql://localhost:3306/myDatabaseName"; In your code not exist database name – Jimmysnn Jul 19 '14 at 01:19
  • also you can see http://stackoverflow.com/questions/20065653/com-mysql-jdbc-exceptions-jdbc4-communicationsexception And http://stackoverflow.com/questions/2839321/java-connectivity-with-mysql/2840358#2840358 – Jimmysnn Jul 19 '14 at 01:24

1 Answers1

0

CommunicationsException: Communications link failure

You can see this answer which also contains good tutorial about your problem:

If you get a SQLException: Connection refused or Connection timed out or a MySQL specific CommunicationsException: Communications link failure, then it means that the DB isn't reachable at all. This can have one or more of the following causes:

  1. IP address or hostname in JDBC URL is wrong.
  2. Hostname in JDBC URL is not recognized by local DNS server.
  3. Port number is missing or wrong in JDBC URL.
  4. DB server is down.
  5. DB server doesn't accept TCP/IP connections.
  6. DB server has run out of connections.
  7. Something in between Java and DB is blocking connections, e.g. a firewall or proxy.

To solve the one or the other, follow the following advices:

  1. Verify and test them with ping.
  2. Refresh DNS or use IP address in JDBC URL instead.
  3. Verify it based on my.cnf of MySQL DB.
  4. Start the DB.
  5. Verify if mysqld is started without the --skip-networking option.
  6. Restart the DB and fix your code accordingly that it closes connections in finally.
  7. Disable firewall and/or configure firewall/proxy to allow/forward the port
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Jimmysnn
  • 583
  • 4
  • 8
  • 30