java.net.ConnectException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13719666/
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
提问by Imri Persiado
I've seen few threads about this topic but I still can't figure out what's wrong. Following is the code:
我已经看到了一些关于这个主题的主题,但我仍然无法弄清楚出了什么问题。以下是代码:
import java.sql.*;
public class SQL
{
public static void main(String[] args)
{
Connection conn = null;
String url = "jdbc:mysql://mysql1.oyo.co.il:3306/";
String dbName = "sdarot2_winner";
String driver = "com.mysql.jdbc.Driver";
String userName = "";
String password = "";
String table = "LEADER_CAM_PRODUCTS";
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
System.out.println("Connected to the database");
conn.close();
System.out.println("Disconnected from database");
}catch (Exception e) {
System.out.println(e);
}
}
}
And here is the error I get:
这是我得到的错误:
com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying > > exception:
** BEGIN NESTED EXCEPTION **
java.net.ConnectException MESSAGE: Connection timed out: connect
STACKTRACE:
java.net.ConnectException: Connection timed out: connect at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(Unknown Source) at java.net.PlainSocketImpl.connectToAddress(Unknown Source) at java.net.PlainSocketImpl.connect(Unknown Source) at java.net.SocksSocketImpl.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at java.net.Socket.(Unknown Source) at java.net.Socket.(Unknown Source) at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256) at com.mysql.jdbc.MysqlIO.(MysqlIO.java:271) at com.mysql.jdbc.Connection.createNewIO(Connection.java:2744) at com.mysql.jdbc.Connection.(Connection.java:1553) at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285) at java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source) at SQL.main(SQL.java:17)
** END NESTED EXCEPTION **
Last packet sent to the server was 1 ms ago.
com.mysql.jdbc.CommunicationsException:由于底层 >> 异常导致通信链接失败:
** 开始嵌套异常 **
java.net.ConnectException 消息:连接超时:连接
堆栈跟踪:
java.net.ConnectException: 连接超时:在 java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(Unknown Source) at java.net.PlainSocketImpl.connectToAddress(Unknown Source) at java 连接。 net.PlainSocketImpl.connect(Unknown Source) at java.net.SocksSocketImpl.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at java.net。 Socket.(Unknown Source) at java.net.Socket.(Unknown Source) at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256) at com.mysql.jdbc.MysqlIO.(MysqlIO.java:271)在 com.mysql.jdbc.Connection.createNewIO(Connection.java:2744) 在 com.mysql.jdbc.Connection.(Connection.java:1553) 在 com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285) 在 java.sql.DriverManager.getConnection(Unknown Source) 在 java.sql.DriverManager.getConnection(Unknown Source) 在 SQL.main(SQL.java:17)
** 结束嵌套异常 **
发送到服务器的最后一个数据包是 1 毫秒前。
Here is few things I found around:
以下是我发现的一些事情:
- The I.P/domain or port is incorrect
- The I.P/domain or port (i.e service) is down
- The I.P/domain is taking longer than your default timeout to respond
- You have a firewall that is blocking requests or responses on whatever port you are using
- You have a firewall that is blocking requests to that particular host
- Your internet access is down
- IP/域或端口不正确
- IP/域或端口(即服务)已关闭
- IP/域的响应时间超过您的默认超时时间
- 您有一个防火墙阻止您使用的任何端口上的请求或响应
- 您有一个防火墙阻止对该特定主机的请求
- 您的互联网访问已关闭
a) it is correct. b) it's not down since I got a website that is working right now with that database. c) how can I check that?I don't think that it's the problem since as I said I got a running website on this database. d) now way because the same reason I mentioned above. e) samem as d
a) 这是正确的。b) 它没有关闭,因为我的网站现在正在使用该数据库。c) 我该如何检查?我不认为这是问题所在,因为正如我所说,我在这个数据库上有一个正在运行的网站。d)现在的方式,因为我上面提到的原因相同。e) 与 d 相同
So what I'm doing wrong?how come a php code can work with sql but java gives me errors? Thank you.
那么我做错了什么?为什么 php 代码可以与 sql 一起使用但 java 给我错误?谢谢你。
回答by Stephen C
As an experiment, try connecting to port 3306 on mysql1.oyo.co.il
using your system's telnet command. Or some other utility (for example nc
or netcat
) that opens a raw TCP/IP connection. Depending on your OS / distro, you may have to locate and install a suitable command to help you with this trouble-shooting.Observe what happens.
作为实验,尝试mysql1.oyo.co.il
使用系统的 telnet 命令连接到端口 3306 。或打开原始 TCP/IP 连接的某些其他实用程序(例如nc
或netcat
)。 根据您的操作系统/发行版,您可能需要找到并安装合适的命令来帮助您进行故障排除。观察会发生什么。
Re your diagnosis:
重新诊断:
a) it is correct.
a) 这是正确的。
If you say so ...
如果你这么说...
b) it's not down since I got a website that is working right now with that database.
b) 它没有关闭,因为我的网站现在正在使用该数据库。
At best you have demonstrated that the host is working. The web service on port 80 and the MySQL service on port 3306 are different services. (And in fact, it is possible that mysql1.oyo.co.il
is doing clever packet routing tricks so that traffic on different ports is being tranparently routed to different hosts or virtual hosts.)
充其量你已经证明主机正在工作。80端口的web服务和3306端口的MySQL服务是不同的服务。(事实上,它可能mysql1.oyo.co.il
正在做聪明的数据包路由技巧,以便不同端口上的流量被透明地路由到不同的主机或虚拟主机。)
c) how can I check that?
c) 我该如何检查?
Try changing / increasing the timeout.
尝试更改/增加超时。
I don't think that it's the problem since as I said I got a running website on this database.
我不认为这是问题所在,因为正如我所说,我在这个数据库上有一个正在运行的网站。
My guess it is not a timeout issue ... but as I said above, the fact you got a website says NOTHING about whether the MySQL service is running.
我猜这不是超时问题……但正如我上面所说的,你有一个网站的事实并没有说明 MySQL 服务是否正在运行。
d) now way because the same reason I mentioned above.
d)现在的方式,因为我上面提到的原因相同。
You've only demonstrated that you can get to port 80 ... see above
您只证明了您可以访问端口 80 ...见上文
e) samem as d
e) 与 d 相同
You've only demonstrated that you can get to port 80 ... see above
您只证明了您可以访问端口 80 ...见上文
Based on the above, it is plausible that:
综上所述,可以认为:
- you have a local firewall problem,
- the MySQL service on that port is not currently running,
- networking routing on the service side are broken for traffic on that port,
- the MySQL service has been permanently turned off, or
- the service's firewall has been configured to not accept network connections from random (or specific) places, including yours.
- 你有本地防火墙问题,
- 该端口上的 MySQL 服务当前未运行,
- 服务端的网络路由因该端口上的流量而中断,
- MySQL 服务已被永久关闭,或
- 该服务的防火墙已配置为不接受来自随机(或特定)位置(包括您的位置)的网络连接。
The fact that you are seeing a timeout rather than a "connection refused" suggests that this is more likely to be a networking or firewalling issue than a problem with the MySQL service endpoint itself.
您看到的是超时而不是“连接被拒绝”这一事实表明,这更可能是网络或防火墙问题,而不是 MySQL 服务端点本身的问题。
My guess is that "they" have withdrawn the service ... and that you are not supposed to use it anymore. (Providing a public MySQL database service strikes me as an odd thing to do ... as well as being problematic from a security and administration standpoint.)
我的猜测是“他们”已经撤消了该服务……而且您不应该再使用它了。(提供公共 MySQL 数据库服务对我来说是一件奇怪的事情……而且从安全和管理的角度来看也是有问题的。)
回答by user207421
'Connection timed out' indicates either:
“连接超时”表示:
- a server-side firewall that is deliberately ignoring your connection request
- a network topology problem such that IP packets aren't getting through
- the server host itself is down.
- 故意忽略您的连接请求的服务器端防火墙
- 网络拓扑问题,使得 IP 数据包无法通过
- 服务器主机本身已关闭。
Most likely it is the first.
最有可能的是第一个。
Note that, contrary to other answers here, it doesn'tindicate that the server program is down, or a local firewall problem at the client. The former would cause 'connection refused' and the latter would give you something along the lines of 'permission denied', depending on your platform.
请注意,与此处的其他答案相反,它并不表示服务器程序已关闭或客户端的本地防火墙问题。前者会导致“连接被拒绝”,后者会根据您的平台为您提供类似于“权限被拒绝”的信息。