我正在尝试使用JDBC连接到MYSQL数据库。在my.conf文件中,我注释掉了bind-address选项。我正在使用phpmyadmin。在MYSQL权限表中,它显示
user:sumitb
主持人:%
密码:是
Global Priveleges:ALL PRIVILEGES
格兰特:是的
但是下面的代码给了我错误:
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
答案 0 :(得分: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(mysql -u sumitb -h 127.0.0.1 -p
)