连接mysql数据库使用mysql cli而不是java程序

时间:2016-01-28 12:22:06

标签: java mysql jdbc database-connection

我已经通过this question了解了与java程序的连接问题和许多其他问题,但它没有解决我的问题。在所有其他问题中,由于db未启动或端口错误等原因无法连接。这里DB服务器正在运行,我可以看到进程,也可以像下面的mysql cli一样连接

mysql -umysql -p mydb

然后使用相同的用户名,密码,主机和端口(默认情况下我的情况为localhost:3306),我无法通过java程序连接并获得以下错误

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:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1117)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:355)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2461)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2498)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2283)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:822)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:404)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at Test.main(Test.java:24)
Caused by: java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at java.net.Socket.connect(Socket.java:538)
    at java.net.Socket.<init>(Socket.java:434)
    at java.net.Socket.<init>(Socket.java:244)
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:259)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:305)
    ... 15 more
ERRROR Communications link failure

可能是什么原因?我正在尝试使用以下简单的java代码连接我的java程序。

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


public class Test {

public static void main(String[] args) {

    Connection conn = null;
    try {               
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection("jdbc:mysql://localhost/mydb","mysql","password");
        System.out.println("Successfully got connection");

    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("ERRROR "+e.getMessage());
    }

    finally
    {
        if(conn!= null)
            try {
                System.out.println("Closing connection");
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
                System.out.println("ERRROR "+e.getMessage());
            }
    }



}

}

如果防火墙可能是罪魁祸首,如果有任何此类事情中断对DB的请求,如何检查linux机器。

更新 - 我发现此命令检查我的防火墙并停止它。

service iptables stop

我看到没有使用以下命令部署防火墙规则

iptables -L -n

因此没有防火墙中断但仍然存在问题。

0 个答案:

没有答案