简体   繁体   English

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

[英]My Java Program can't connect to the MySQL server

Yesterday I spent several hours trying to solve this problem.昨天我花了几个小时试图解决这个问题。 When I try to connect to MySQL server from my Java program I get the CommunicationsException: Communications link failure .当我尝试从我的 Java 程序连接到 MySQL 服务器时,出现CommunicationsException: Communications link failure I have encountered several questions on Stackoverflow with similar problems but none of the solutions that were provided in the answers worked for me.我在 Stackoverflow 上遇到过几个具有类似问题的问题,但答案中提供的解决方案都不适合我。 For instance I have read an answer which advises to replace "localhost" with "127.0.0.1" or with my private IP address in the Driver.getConnection method, but that didn't solve the problem.I have read an answer which suggested that it should be checked whether the server is running.例如,我读过一个答案,建议在Driver.getConnection方法中用“127.0.0.1”或我的私有地址 IP 替换“localhost”,但这并没有解决问题。我读过一个答案,建议应该检查服务器是否正在运行。 So, I typed sudo service mysql status and I saw that the server was active and operational.因此,我输入了sudo service mysql status ,我看到服务器处于活动状态并且可以运行。 I have read an answer telling me to add the line Class.forName("com.mysql.jdbc.Driver");我读过一个答案告诉我添加行Class.forName("com.mysql.jdbc.Driver"); so I did that but that didn't help either.所以我这样做了,但这也无济于事。

Here is my Java code这是我的 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");
    }
}

Here is the entire message from the terminal that I have received:这是我收到的来自终端的完整消息:

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

Edit: I don't have big enough reputation to respond in the comment, so I will add additional information here.编辑:我的声誉不够大,无法在评论中做出回应,因此我将在此处添加其他信息。 I am able to connect to the server from terminal by writting mysql -u username -p .我可以通过mysql -u username -p从终端连接到服务器。 Everything works fine then.然后一切正常。

I have found the solution to my problem and I want to share the solution in case someone else faces the same problem.我找到了解决我的问题的方法,我想分享解决方案以防其他人遇到同样的问题。 As Jon Skeet suggested in his comment, the server, for whatever reason, wasn't listening on port 3306. I confirmed this by logging into the mysql server via terminal ( mysql -u root -p ) and then I wrote正如 Jon Skeet 在他的评论中所建议的那样,无论出于何种原因,服务器都没有监听端口 3306。我通过终端 ( mysql -u root -p ) 登录到 mysql 服务器来确认这一点,然后我写了
SHOW GLOBAL VARIABLES LIKE 'PORT';
It said that port was 0 instead of 3306. Here is how I changed the port.它说端口是 0 而不是 3306。这是我更改端口的方法。
I went to /etc/mysql/mysql.conf.d/我去了/etc/mysql/mysql.conf.d/
then I edited the file which is named mysqld.cnf .然后我编辑了名为mysqld.cnf的文件。
On my PC, that file was a read-only file, so I needed to first change the permissions.在我的 PC 上,该文件是只读文件,因此我需要先更改权限。
I wrote sudo chmod +rw mysqld.cnf in the terminal while I was in the /etc/mysql/mysql.conf.d/ directory.当我在 /etc/mysql/mysql.conf.d/ 目录中时,我在终端中写了sudo chmod +rw mysqld.cnf /etc/mysql/mysql.conf.d/
After that, I edited the file by typing sudo gedit mysqld.cnf in the terminal.之后,我通过在终端中键入sudo gedit mysqld.cnf来编辑文件。
In the file, I added port = 3306 in a new line, below [mysqd], so that it looks like this在文件中,我在 [mysqd] 下面的新行中添加了port = 3306 ,这样它看起来像这样

[mysqld]
port = 3306

I restarted the mysql server by writting sudo service mysql restart in terminal.我通过在终端中sudo service mysql restart来重启 mysql 服务器。

Just as a quick note, if you want to run the server with skip-grant-tables option, this is where you add skip-grant-tables, you add it on the same place that I added port = 3306.快速说明一下,如果你想使用 skip-grant-tables 选项运行服务器,这是你添加 skip-grant-tables 的地方,你将它添加到我添加端口 = 3306 的相同位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM