0
votes

I am trying to connect to a mysql database on remote server and whenever I try to run the code it is giving me connection refused exception.

 Connection con = null;
          String driver = "com.mysql.jdbc.Driver";
          String url1="jdbc:mysql://IPADDRESS:3306/";
          String db = "hola";
          String dbUser = "root";
          String dbPasswd = "root";
          Class.forName(driver).newInstance();
          con = DriverManager.getConnection(url1+db, dbUser, dbPasswd);
          System.out.println("Database Connection Established");

Also, when I telnet with that IP on a port 3306 it is giving me connection refused. How can I make sure that my server listens to connection over port 3306?

4
May be possible that you don't have access to that db. - Charvee Shah
Your connection should be proper to that IP - Prashant
If your port number and ip are correct, Try after disabling firewall of the destination system - Anptk
I have disabled the firewall on that remote system, I have added the bind address to 0.0.0.0 in my.cnf file of mysql in the remote system. when I telnet the system Ip over that port 3306, its giving me connection refused. Can anyone tell me how to make the remote server to accept connections over the port 3306 - Sanjay Rapolu

4 Answers

3
votes

java.net.ConnectException: Connection refused means that the IP address or port number you've entered is incorrect!

2
votes

There is nothing listening at that IP:port. Check that the IP:port is correct, that MySQL is installed there, on that port, and that it is running.

Contrary to other answers here, it has nothing to do with the user name, password, or permissions, and almost certainly nothing to do with the firewall either unless you have an antique.

0
votes

In general, when you are getting java.net.ConnectException: Connection refused often it is because of the following reasons:

  1. The server is not started. What happens when you ping the host or try to access the host from you browser? Maybe there is a spelling error in the hostname of your project.
  2. Your firewall is blocking access. Disable you firewall and try again
  3. You are using the wrong port. If you have access to the server see what port it is configured with and that your client connection params match.
  4. You have not configured SSL. Is the endpoint using https? This is more specific to connecting web services rather than databases, but if you are not handling certs correctly you will get a connection refused error.
-2
votes

I've found the solution for the problem. My server was not listening to connections over the port 3306 and I made necessary configurations

Thank you all