0

我正在尝试使用 JDBC 连接到 MYSQL 数据库。在 my.conf 文件中,我已经注释掉了 bind-address 选项。我正在使用 phpmyadmin。在 MYSQL 权限表中,它显示

用户: sumitb

主持人 : %

密码:是

全球特权:所有特权

格兰特:是的

但是下面的代码给了我错误:

   try {
        System.out.println("Loading driver...");
        Class.forName("com.mysql.jdbc.Driver");
        System.out.println("Driver loaded!");
    } catch (ClassNotFoundException e) {
        throw new RuntimeException("Cannot find the driver in the classpath!", e);
    }

    String url = "jdbc:mysql://172.27.20.124:3306/synsetSimilarity"; // Important
    String username = sumitb;
    String password = StaticValues.sqlPassword;
    Connection connection = null;
    try {
        System.out.println("Connecting database...");
        connection = DriverManager.getConnection(url, username, password);
        System.out.println("Database connected!");                      
    } catch (SQLException e) {
        e.printStackTrace();
        throw new RuntimeException("Cannot connect the database!", e);
    } finally {
        System.out.println("Closing the connection.");
        if (connection != null) try { connection.close(); } catch (SQLException ignore) {}
    }
}

我收到以下我无法理解的错误。如果我将 ip 更改为 localhost 或 127.0.0.1,它可以正常工作。

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:525)
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:350)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2408)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2445)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2230)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:813)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
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:525)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:399)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:334)
at java.sql.DriverManager.getConnection(DriverManager.java:579)
at java.sql.DriverManager.getConnection(DriverManager.java:221)
at krsystem.ontology.wekaClustering.PopulateSimilarityDB.test(PopulateSimilarityDB.java:137)
at krsystem.ontology.wekaClustering.PopulateSimilarityDB.main(PopulateSimilarityDB.java:243)
Caused by: java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
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.SocksSocketImpl.connect(SocksSocketImpl.java:391)
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:259)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:300)
... 16 more

任何帮助,将不胜感激。

更新:从终端尝试时,我收到以下错误

 $mysql -u sumitb -h 172.27.20.124 -p
 Enter password: 
 ERROR 2003 (HY000): Can't connect to MySQL server on '172.27.20.124' (111
4

1 回答 1

2

注释掉“绑定地址选项”后,您是否重新启动了 mysql?

如果您的 mysql 服务器与您的客户端在同一台机器上运行,为什么不使用 127.0.0.1 作为 ip 而不是 172.27.20.124?

如果您坚持使用172.27.20.124,请检查mysql端口是否打开。您可以从命令行使用 telnet:

telnet 172.27.20.124 3306

看看它是否打开。或使用 nmap。您收到的错误表明您的客户端甚至无法与您的 mysql 服务器通信。

另外,正如 Abhinav Sarkar 所说,尝试从命令行连接到 mysql,指定您的主机,就像您在 java 中所做的那样,看看是否可行。如果您确实也在本地运行 mysql,请尝试将主机指定为 127.0.01 ( mysql -u sumitb -h 127.0.0.1 -p)

于 2013-03-16T08:00:42.067 回答