java.net.ConnectException:连接被拒绝
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29071072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.net.ConnectException: Connection refused
提问by Sanjay Rapolu
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.
我正在尝试连接到远程服务器上的 mysql 数据库,每当我尝试运行代码时,它都会给我连接被拒绝的异常。
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?
此外,当我在端口 3306 上使用该 IP 进行 telnet 时,它拒绝了我的连接。如何确保我的服务器侦听端口 3306 上的连接?
回答by The Well
java.net.ConnectException: Connection refused
means that the IP address or port number you've entered is incorrect!
java.net.ConnectException: Connection refused
表示您输入的 IP 地址或端口号不正确!
回答by user207421
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.
在那个 IP:port 上没有任何东西在监听。检查 IP:port 是否正确,MySQL 是否安装在那里,在那个端口上,并且它正在运行。
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.
与此处的其他答案相反,它与用户名、密码或权限无关,而且几乎可以肯定与防火墙无关,除非您有古董。
回答by cosbor11
In general, when you are getting java.net.ConnectException: Connection refused
often it is because of the following reasons:
一般来说,当您java.net.ConnectException: Connection refused
经常收到时,是由于以下原因:
- 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.
- 服务器未启动。当您 ping 主机或尝试从浏览器访问主机时会发生什么?也许您的项目的主机名中有拼写错误。
- 您的防火墙正在阻止访问。禁用防火墙并重试
- 您使用了错误的端口。如果您有权访问服务器,请查看它配置的端口以及您的客户端连接参数是否匹配。
- 您尚未配置 SSL。端点是否使用 https?这更具体地用于连接 Web 服务而不是数据库,但是如果您没有正确处理证书,您将收到连接拒绝错误。
回答by Sanjay Rapolu
I've found the solution for the problem. My server was not listening to connections over the port 3306 and I made necessary configurations
我已经找到了问题的解决方案。我的服务器没有侦听端口 3306 上的连接,我进行了必要的配置
Thank you all
谢谢你们