MariaDB CommunicationsException:通信链接失败

时间:2015-04-24 08:48:49

标签: mysql jdbc mariadb

在Stackoverflow上发布了与CommunicationsException相关的足够问题。但是没有一个能帮助我摆脱困境。

我在我的CentOS 7服务器中安装了MariaDB 5.5,并配置了所有内容,以便我可以通过eclipse中的MySQL Workbench或Data Source Explorer远程连接到数据库服务器。成功连接的屏幕截图如下所示。 enter image description here

但是,当我尝试通过JDBC连接数据库服务器时,它继续失败,出现异常" com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败"。

测试类如下所示。

package com.pratice;
import java.sql.*;
public class DatabaseConn {
    public static void main(String[] args) throws Exception{
        Connection conn = null;
        ResultSet rs = null;
        Statement stmt = null;
        String url = "jdbc:mysql://59.12.140.121:6330/myPrjs";//?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
        String username = "user";
        String password = "password";
        String driver = "com.mysql.jdbc.Driver";

        try{
            Class.forName(driver);

            conn = DriverManager.getConnection(url, username, password);
            stmt = conn.createStatement();
            rs = stmt.executeQuery("SELECT 1");

            while(rs.next()){
                System.out.print(rs.getObject(1) + "\t" + rs.getObject(2));
            }
        }finally{
            if(rs!=null) {  
                rs.close();  
            }  
            if(stmt!=null) {  
                stmt.close();  
            }  
            if (conn != null) {  
                conn.close();  
            }  
        }
    }
}

当我运行DatabaseConn时,我收到了错误消息。

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:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1127)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:356)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2502)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2539)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2321)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:832)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:417)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:344)
    at java.sql.DriverManager.getConnection(DriverManager.java:571)
    at java.sql.DriverManager.getConnection(DriverManager.java:215)
    at com.webjsp.pratice.DatabaseConn.main(DatabaseConn.java:18)
Caused by: java.net.SocketException: Bad address: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:579)
    at java.net.Socket.connect(Socket.java:528)
    at java.net.Socket.<init>(Socket.java:425)
    at java.net.Socket.<init>(Socket.java:241)
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:258)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:306)
    ... 15 more

我无法解决这个问题,所以我在互联网上搜索,我在here找到了一份清单。但是现在我可以通过IDE连接到我的数据库服务器,但我不知道可能出现的问题。

我也尝试使用org.mariadb.jdbc.driver而不是mysql驱动程序,我也收到了错误消息。

Exception in thread "main" java.sql.SQLNonTransientConnectionException: Could not connect to 59.12.140.121:6330 : Bad address: connect
        at org.mariadb.jdbc.internal.SQLExceptionMapper.get(SQLExceptionMapper.java:136)
        at org.mariadb.jdbc.internal.SQLExceptionMapper.throwException(SQLExceptionMapper.java:106)
        at org.mariadb.jdbc.Driver.connect(Driver.java:115)
        at java.sql.DriverManager.getConnection(Unknown Source)
        at java.sql.DriverManager.getConnection(Unknown Source)
        at DatabaseConn.main(DatabaseConn.java:16)
Caused by: org.mariadb.jdbc.internal.common.QueryException: Could not connect to 59.12.140.121:6330
: Bad address: connect
        at org.mariadb.jdbc.internal.mysql.MySQLProtocol.connect(MySQLProtocol.java:714)
        at org.mariadb.jdbc.internal.mysql.MySQLProtocol.<init>(MySQLProtocol.java:280)
        at org.mariadb.jdbc.Driver.connect(Driver.java:111)
        ... 3 more
Caused by: java.net.SocketException: Bad address: 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 java.net.Socket.connect(Unknown Source)
        at org.mariadb.jdbc.internal.mysql.MySQLProtocol.connect(MySQLProtocol.java:387)
        at org.mariadb.jdbc.internal.mysql.MySQLProtocol.connect(MySQLProtocol.java:709)
        ... 5 more

有人能告诉我如何解决这个问题,或者给我一些提示,说明为什么会这样?非常感谢你。

1 个答案:

答案 0 :(得分:0)

解决问题的关键是&#34; java.net.SocketException:错误地址:connect&#34;。

问题是由客户端(我的Windows PC)的防火墙规则引起的。我在客户端PC上启用了Ahnlab V3 Internet Security Personal Firewall。出站规则默认只允许某些类型的传出套接字。当我尝试使用工作台连接到我的远程服务器时,没有问题。但是当我尝试通过运行java类连接到服务器时,本地防火墙保留了套接字,因此连接请求甚至都没有通过我的网卡!我添加了一个允许mysql出站连接的新出站规则,然后我的程序正常运行。

希望这会帮助遇到同样问题的人。