com.mysql.jdbc.exceptions.jdbc4.communicationsexception:通信链路故障

brgchamk  于 2021-06-20  发布在  Mysql
关注(0)|答案(23)|浏览(461)

我正在努力让我的数据库和我的java程序对话。
有人能给我一个使用jdbc的快速而肮脏的示例程序吗?
我得到了一个相当惊人的错误:

Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.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 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1122)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2260)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:787)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:49)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:357)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
    at java.sql.DriverManager.getConnection(DriverManager.java:582)
    at java.sql.DriverManager.getConnection(DriverManager.java:207)
    at SqlTest.main(SqlTest.java:22)
Caused by: com.mysql.jdbc.exceptions.jdbc4.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 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1122)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:344)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2181)
    ... 12 more
Caused by: java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:432)
    at java.net.Socket.connect(Socket.java:529)
    at java.net.Socket.connect(Socket.java:478)
    at java.net.Socket.<init>(Socket.java:375)
    at java.net.Socket.<init>(Socket.java:218)
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:293)
    ... 13 more

测试文件的内容:

import com.mysql.jdbc.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class SqlTest {

    public static void main(String [] args) throws Exception {
        // Class.forName( "com.mysql.jdbc.Driver" ); // do this in init
        // // edit the jdbc url 
        Connection conn = DriverManager.getConnection( 
            "jdbc:mysql://localhost:3306/projects?user=user1&password=123");
        // Statement st = conn.createStatement();
        // ResultSet rs = st.executeQuery( "select * from table" );

        System.out.println("Connected?");
    }
}
zysjyyx4

zysjyyx416#

我得到了同样的错误,因为我试图运行我的程序而不启动mysql服务器。
启动mysql服务器后,一切正常。

7xllpg7q

7xllpg7q17#

如果您更改了端口,则会出现这样的错误“com.mysql.jdbc.exceptions.jdbc4.communicationexception:communications link failure”,请检查您的端口号

weylhg0b

weylhg0b18#

在我的例子中,我需要将localhost替换为实际的数据库服务器ip地址
而不是

Connection con = DriverManager.getConnection(
 "jdbc:mysql://localhost:3306/DBname", "root", "root");

我需要

Connection con = DriverManager.getConnection(
 "jdbc:mysql://192.100.0.000:3306/DBname", "root", "root");
h6my8fg2

h6my8fg219#

在我的情况下,结果是 mysql-connector-java 太老了。
在我的演示中,我使用 mysql-connector-java 这样地:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.9</version>
</dependency>

但在开发环境中,我使用以下方法:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.31</version>
</dependency>

我的mysql版本是5.1.48(是的,它是旧的,只是为了模仿产品版本)。所以我遇到了同样的错误。
既然找到了原因,也就找到了解决办法。匹配版本!

kjthegm6

kjthegm620#

我的问题可以通过以下步骤解决:
my.cnf ```
vi /etc/mysql/my.cnf

修改其绑定地址

"bind-address = 0.0.0.0"

重启mysql

sudo /etc/init.d/mysql restart

i1icjdpr

i1icjdpr21#

我已经有同样的问题好几个小时了。我用的是mamp服务器
不要使用localhost:[apache port],而是使用mysql端口。
下面是mamp服务器的默认mysql端口。

String url = "jdbc:mysql://localhost:8889/db_name";

Connection conn = DriverManager.getConnection(url, dbUsername, dbPassword);
fivyi3re

fivyi3re22#

试着改变 localhost127.0.0.1 .
localhost将解析为 ::1 . 默认情况下mysql不能通过ipv6连接。
这是 telnet localhost 3306 :

$ telnet localhost 3306
Trying ::1...

mysql服务器没有响应。
当然,请确保您的mysql服务器正在运行。

7jmck4yq

7jmck4yq23#

早先的回答是恰当的。但是,我还要指出一个更一般的问题。
我也面临类似的问题,原因是我公司的网络限制。
当我在任何其他网络中时,同样的连接也成功了。

相关问题