问题
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?
回答1:
java.net.ConnectException: Connection refused
means that the IP address or port number you've entered is incorrect!
回答2:
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.
回答3:
In general, when you are getting java.net.ConnectException: Connection refused
often it is because of the following reasons:
- 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.
- Your firewall is blocking access. Disable you firewall and try again
- 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.
- 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.
回答4:
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
来源:https://stackoverflow.com/questions/29071072/java-net-connectexception-connection-refused