首先,我想对不起,因为这个问题被问了十亿次,但我仍然不确定我的错误是什么。
所以我遇到的问题如下:
我尝试使用主机,端口等连接到我的MySQL Server,但出现“拒绝连接”异常。通常,这表示问题与连接/登录有关,并且表示它无法连接至MySQL Server。
通过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 Server没有在该端口上侦听,因为当我使用phpMyAdmin对其进行尝试时,我忘记了它在同一台计算机上运行。
答案 0 :(得分:0)
与其说是答案,不如说是一种诊断问题的方法:
我通常会尝试使用telnet或putty之类的工具在相关端口上连接到MySQL数据库。如果无法连接,则说明问题不是代码,而是网络连接。这就是我的堆栈轨迹-网络连接问题,而不是其他任何问题。也许防火墙阻止了端口?
然后,我将尝试使用带有用户名和密码的MySQL连接工具。
连接字符串中可能缺少一些其他参数。
答案 1 :(得分:0)
strack跟踪显示Caused by: java.net.ConnectException: Connection refused: connect
,这意味着mysql服务器不在您通过的url
处运行,或者如果服务器处于活动状态,则它不会在您以{参数