这个问题在这里已经有答案了:
用jdbc和mysql[duplicate]解决“通信链路故障”(25个答案)
java.net.connectexception:连接被拒绝(4个答案)
两年前关门了。
首先,我想说对不起,因为这个问题被问了十亿次,但我仍然不确定我的错误是什么。
所以我的问题是:
我试图用主机、端口等连接到mysql服务器,但是我得到了“连接被拒绝”的异常。这通常意味着问题是关于连接/登录的,并且意味着它不能连接到mysql服务器。当我通过phpmyadmin登录到服务器时,我使用的是完全相同的数据。下面是错误和我的源代码,我希望有人能告诉我我的错误是什么。
提前谢谢。
例外情况:
The last packet sent successfully to the server was 0 milliseconds ago. The
driver has not received any packets from the server.
at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:172)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:862)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:444)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:230)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:226)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at teabot.database.DatabaseConnection.<init>(DatabaseConnection.java:26)
at teabot.database.DatabaseHelper.setupDB(DatabaseHelper.java:16)
at teabot.main.Main.init(Main.java:39)
at teabot.main.Main.main(Main.java:69)
Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:59)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:103)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:149)
at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:165)
at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:92)
at com.mysql.cj.NativeSession.connect(NativeSession.java:152)
at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:982)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:852)
... 9 more
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:173)
at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:66)
源代码:
protected DatabaseConnection(String host, int port, String databaseName, String username, String password) {
boolean error = false;
try {
String url = "jdbc:mysql://" + host + ":" + port + "/" + databaseName;
this.connection = DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
e.printStackTrace();
Helper.log(2, e.getMessage());
error = true;
}
if (!error) {
Helper.log(0, "Database is ready.");
}
}
编辑:我的问题是我的mysql服务器没有监听这个端口,因为当我用phpmyadmin试用它时,我忘记了它是在同一台机器上运行的。
2条答案
按热度按时间62lalag41#
与其说是答案,不如说是诊断问题的方法:
我通常尝试使用telnet或putty之类的工具通过相关端口连接到mysql数据库。如果你不能连接,你知道问题不是你的代码,而是你的网络连接。这就是堆栈轨迹在我看来的样子—网络连接问题,而不是其他任何问题。可能是防火墙阻塞了端口?
然后我将尝试使用带有用户名和密码的mysql连接工具。
连接字符串中可能缺少其他一些参数。
gfttwv5a2#
strack轨迹显示
Caused by: java.net.ConnectException: Connection refused: connect
这意味着mysql服务器不是实时的url
您通过了,或者如果服务器处于活动状态,则它不会监听port
您作为参数传递