java 通过 JDBC 连接到 SQL 到 IP 托管 MySQL 服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15446837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Connecting to SQL via JDBC to an IP hosting MySQL Server
提问by damned
I am trying to connect to an MYSQL database using JDBC. In my.conf file I have commented out the bind-address option. I am using phpmyadmin. In MYSQL privileges table, it shows that
我正在尝试使用 JDBC 连接到 MYSQL 数据库。在 my.conf 文件中,我注释掉了 bind-address 选项。我正在使用 phpmyadmin。在MYSQL权限表中,它显示
user : sumitb
用户:sumitb
host : %
主持人 : %
Password : Yes
密码 : 是
Global Priveleges : ALL PRIVILEGES
全球特权:所有特权
Grant : Yes
格兰特:是的
But the following code gives me error :
但以下代码给了我错误:
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) {}
}
}
I get the following error which I am not able to understand. If I change the ip to localhost or 127.0.0.1, it works fine.
我收到以下我无法理解的错误。如果我将 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
Any help would be appreciated.
任何帮助,将不胜感激。
Update : On trying from terminal, I get the following error
更新:从终端尝试时,我收到以下错误
$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
回答by Tucker
did you restart mysql after commenting out "the bind-address option"?
注释掉“绑定地址选项”后您是否重新启动了mysql?
if your mysql server is running on the same machine as your client, why not use 127.0.0.1 as the ip instead of 172.27.20.124?
如果您的 mysql 服务器与您的客户端在同一台机器上运行,为什么不使用 127.0.0.1 而不是 172.27.20.124 作为 IP?
if you insist on using 172.27.20.124, check that the mysql port is open. you can use telnet from the command line:
如果坚持使用172.27.20.124,请检查mysql端口是否打开。您可以从命令行使用 telnet:
telnet 172.27.20.124 3306
and see if it's open. or use nmap. the error you are getting says that your client isn't able to even talk to your mysql server.
看看它是否打开。或使用 nmap。您收到的错误表明您的客户端甚至无法与您的 mysql 服务器通信。
also, as Abhinav Sarkar said, try to connect to mysql from the command line specifying your host like you do in java and see if that works. try specifying the host as 127.0.01 if you are indeed running mysql locally as well (mysql -u sumitb -h 127.0.0.1 -p
)
此外,正如 Abhinav Sarkar 所说,尝试从命令行连接到 mysql,指定您的主机,就像在 Java 中一样,看看是否有效。如果您确实也在本地运行 mysql,请尝试将主机指定为 127.0.01 ( mysql -u sumitb -h 127.0.0.1 -p
)