我的 Java 程序无法连接到 MySQL 服务器

My Java Program can't connect to the MySQL server

昨天我花了几个小时试图解决这个问题。 当我尝试从我的 Java 程序连接到 MySQL 服务器时,我得到了 CommunicationsException: Communications link failure。 我在 Whosebug 上遇到过几个具有类似问题的问题,但答案中提供的 none 解决方案对我有用。例如,我读过一个答案,建议在 Driver.getConnection 方法中将“localhost”替换为“127.0.0.1”或我的私有 IP 地址,但这并没有解决 problem.I 已阅读的问题建议检查服务器是否为 运行 的答案。因此,我输入 sudo service mysql status 并看到服务器处于活动状态并且可以运行。我已经阅读了一个答案,告诉我添加行 Class.forName("com.mysql.jdbc.Driver"); 所以我这样做了,但这也没有帮助。

这是我的 Java 代码

package com.mainpackage;
import java.sql.*;

public class Main {

    public static void main(String[] args) throws SQLException, ClassNotFoundException{
   
        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db1?user=userName&passwword=userPassword");
    }
}

这是我收到的来自终端的完整消息:

java -classpath .:/home/javaUtil/MySQL/mysql-connector.jar com/mainpackage/Main

Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is com.mysql.cj.jdbc.Driver. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
Exception in thread "main" com.mysql.cj.jdbc.exceptions.CommunicationsException: 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 com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:828)
    at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:448)
    at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:241)
    at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:681)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:252)
    at com.mainpackage.Main.main(Main.java:9)
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 java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151)
    at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167)
    at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:89)
    at com.mysql.cj.NativeSession.connect(NativeSession.java:120)
    at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:948)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:818)
    ... 6 more
Caused by: java.net.ConnectException: Connection refused
    at java.base/sun.nio.ch.Net.connect0(Native Method)
    at java.base/sun.nio.ch.Net.connect(Net.java:579)
    at java.base/sun.nio.ch.Net.connect(Net.java:568)
    at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:588)
    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327)
    at java.base/java.net.Socket.connect(Socket.java:633)
    at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:153)
    at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:63)
    ... 9 more

编辑:我的声望不够大,无法在评论中做出回应,因此我将在此处添加其他信息。我可以通过写入 mysql -u username -p 从终端连接到服务器。那么一切正常。

我找到了问题的解决方案,我想分享解决方案以防其他人遇到同样的问题。正如 Jon Skeet 在他的评论中所建议的那样,无论出于何种原因,服务器都没有在端口 3306 上侦听。 我通过终端 (mysql -u root -p) 登录 mysql 服务器确认了这一点,然后我写了
SHOW GLOBAL VARIABLES LIKE 'PORT';
它说端口是 0 而不是 3306。这是我更改端口的方法。
我去了/etc/mysql/mysql.conf.d/
然后我编辑了名为 mysqld.cnf 的文件。
在我的电脑上,该文件是一个 read-only 文件,所以我需要先更改权限。
我写 sudo chmod +rw mysqld.cnf 在终端中,而我在 /etc/mysql/mysql.conf.d/ 目录中。
之后,我通过输入 sudo gedit mysqld.cnf 编辑了文件 在终端。
在文件中,我添加了 port = 3306 换行,在 [mysqd] 下面,这样看起来像这样

[mysqld]
port = 3306

我通过在终端中写入 sudo service mysql restart 重新启动了 mysql 服务器。

快速说明一下,如果你想要 运行 带有 skip-grant-tables 选项的服务器,这是你添加 skip-grant-tables 的地方,你将它添加到我添加的同一个地方端口 = 3306.