无法与 MySQL 建立 JDBC 连接(使用 Java、IntelliJ 和 Linux)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2102912/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 19:41:59  来源:igfitidea点击:

Can't make JDBC connection to MySQL (using Java, IntelliJ, and Linux)

javamysqllinuxjdbcjar

提问by olive

I am having issues trying to get a database connection using the code below:

我在尝试使用以下代码获取数据库连接时遇到问题:

  try {
        Class.forName("com.mysql.jdbc.Driver");
        Properties p = new Properties();
        p.put("user", user_name);
        p.put("password", password);
        connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1/jsp_test", p);
    } catch (SQLException ex) {
        // handle any errors
        ex.printStackTrace();
        System.out.println("SQLException: " + ex.getMessage());
        System.out.println("SQLState: " + ex.getSQLState());
        System.out.println("VendorError: " + ex.getErrorCode());
        return false;
    } catch (ClassNotFoundException e) {
        e.printStackTrace();  
    }

The error message that is outputted is:

输出的错误信息是:

/usr/lib/jvm/java-6-openjdk/bin/java -Didea.launcher.port=7532 -Didea.launcher.bin.path=/usr/bin/idea/bin -Dfile.encoding=UTF-8 -classpath /usr/lib/jvm/java-6-openjdk/jre/lib/jce.jar:/usr/lib/jvm/java-6-openjdk/jre/lib/about.jar:/usr/lib/jvm/java-6-openjdk/jre/lib/resources.jar:/usr/lib/jvm/java-6-openjdk/jre/lib/management-agent.jar:/usr/lib/jvm/java-6-openjdk/jre/lib/jsse.jar:/usr/lib/jvm/java-6-openjdk/jre/lib/charsets.jar:/usr/lib/jvm/java-6-openjdk/jre/lib/rt.jar:/usr/lib/jvm/java-6-openjdk/jre/lib/ext/localedata.jar:/usr/lib/jvm/java-6-openjdk/jre/lib/ext/sunjce_provider.jar:/usr/lib/jvm/java-6-openjdk/jre/lib/ext/sunpkcs11.jar:/usr/lib/jvm/java-6-openjdk/jre/lib/ext/dnsns.jar:/home/bedtimes/Java Projects/db_demo/out/production/db_demo:/opt/java/jre/lib/ext/mysql-connector-java-5.1.10-bin.jar:/usr/bin/idea/lib/idea_rt.jar com.intellij.rt.execution.application.AppMain 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:532) at com.mysql.jdbc.Util.handleNewInstance(Util.java:406) at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074) at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2214) at com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:781) at com.mysql.jdbc.JDBC4Connection.(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:532) at com.mysql.jdbc.Util.handleNewInstance(Util.java:406) at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:352) at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:284) at java.sql.DriverManager.getConnection(DriverManager.java:620) at java.sql.DriverManager.getConnection(DriverManager.java:169) at database.Database.connect(Database.java:80) at Main.main(Main.java:13) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:616) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:110) Caused by: 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:532) at com.mysql.jdbc.Util.handleNewInstance(Util.java:406) at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074) at com.mysql.jdbc.MysqlIO.(MysqlIO.java:343) at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2137) ... 18 more Caused by: java.net.ConnectException: Connection refused at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:310) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:176) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:163) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:384) at java.net.Socket.connect(Socket.java:542) at java.net.Socket.connect(Socket.java:492) at java.net.Socket.(Socket.java:389) at java.net.Socket.(Socket.java:232) at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:253) at com.mysql.jdbc.MysqlIO.(MysqlIO.java:292) ... 19 more SQLException: 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. SQLState: 08S01 VendorError: 0

Process finished with exit code 0

/usr/lib/jvm/java-6-openjdk/bin/java -Didea.launcher.port=7532 -Didea.launcher.bin.path=/usr/bin/idea/bin -Dfile.encoding=UTF-8 -类路径 /usr/lib/jvm/java-6-openjdk/jre/lib/jce.jar:/usr/lib/jvm/java-6-openjdk/jre/lib/about.jar:/usr/lib/jvm/ java-6-openjdk/jre/lib/resources.jar:/usr/lib/jvm/java-6-openjdk/jre/lib/management-agent.jar:/usr/lib/jvm/java-6-openjdk/ jre/lib/jsse.jar:/usr/lib/jvm/java-6-openjdk/jre/lib/charsets.jar:/usr/lib/jvm/java-6-openjdk/jre/lib/rt.jar: /usr/lib/jvm/java-6-openjdk/jre/lib/ext/localedata.jar:/usr/lib/jvm/java-6-openjdk/jre/lib/ext/sunjce_provider.jar:/usr/lib /jvm/java-6-openjdk/jre/lib/ext/sunpkcs11.jar:/usr/lib/jvm/java-6-openjdk/jre/lib/ext/dnsns.jar:/home/bedtimes/Java Projects/ db_demo/out/production/db_demo:/opt/java/jre/lib/ext/mysql-connector-java-5.1.10-bin.jar:/usr/bin/idea/lib/idea_rt.jar com.intellij。rt.execution.application.AppMain Main com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败

最后一个成功发送到服务器的数据包是 0 毫秒前。驱动程序没有收到来自服务器的任何数据包。在 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 在 sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) 在 sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 在 java.reflectConstructor .newInstance(Constructor.java:532) at com.mysql.jdbc.Util.handleNewInstance(Util.java:406) at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074) at com.mysql.jdbc。 ConnectionImpl.createNewIO(ConnectionImpl.java:2214) at com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:781) at com.mysql.jdbc.JDBC4Connection.(JDBC4Connection.java:

最后一个成功发送到服务器的数据包是 0 毫秒前。驱动程序没有收到来自服务器的任何数据包。在 sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 在 sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) 在 sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 在 java.reflectConstructor .newInstance(Constructor.java:532) at com.mysql.jdbc.Util.handleNewInstance(Util.java:406) at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074) at com.mysql.jdbc。 MysqlIO.(MysqlIO.java:343) at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2137) ... 18 更多 引起:java.net.ConnectException:连接被拒绝在 java。

最后一个成功发送到服务器的数据包是 0 毫秒前。驱动程序没有收到来自服务器的任何数据包。SQLState: 08S01 供应商错误: 0

进程以退出代码 0 结束

I've, literally, no idea how to troubleshoot this error message. The database exists. The username and password exists. I've currently not added any tables to the database but I don't think that can be the issue, since I'm only making a connection after all...

从字面上看,我不知道如何解决此错误消息。数据库存在。用户名和密码存在。我目前还没有向数据库添加任何表,但我认为这不是问题,因为我毕竟只是在建立连接......

I can provide extra information if needs be. I feel like I've tried a lot. Does anybody know any ways of getting further information on how and why it's failing?

如果需要,我可以提供额外的信息。我觉得我已经尝试了很多。有没有人知道有什么方法可以获取有关它失败的方式和原因的更多信息?

Thanks for your help! :)

谢谢你的帮助!:)

采纳答案by Browsera

From your connection string, this database should be on the local machine. Can you try running this command to ensure the socket is open for connections?

从您的连接字符串来看,这个数据库应该在本地机器上。您可以尝试运行此命令以确保套接字已打开以进行连接吗?

telnet 127.0.0.1 3306

远程登录 127.0.0.1 3306

and ensure that it connects? You may not have configured your mysql instance to listen for connections from this machine or on this interface address. If the connection fails, you need to modify your mysql config, for example:

并确保它连接?你可能没有配置你的 mysql 实例来监听来自这台机器或这个接口地址的连接。如果连接失败,你需要修改你的mysql配置,例如:

http://www.cyberciti.biz/tips/how-do-i-enable-remote-access-to-mysql-database-server.html

http://www.cyberciti.biz/tips/how-do-i-enable-remote-access-to-mysql-database-server.html

回答by Jochem

I had the same symptoms but for me the solution was to change the bind-address to 0.0.0.0 in /etc/mysql/my.cnf

我有同样的症状,但对我来说,解决方案是将 /etc/mysql/my.cnf 中的绑定地址更改为 0.0.0.0

(Just posting this for others seeking an answer to this question)

(只是张贴这个给其他人寻求这个问题的答案)

回答by abdulhakeem

You can try this instead:

你可以试试这个:

connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/jsp_test","user_name","password");

回答by ARNAB2012

you can try one of the following

您可以尝试以下方法之一

"jdbc:mysql://localhost:3306/jsp_test","username","password"

or

或者

"jdbc:mysql://'your machines ip (without quote)':3306/jsp_test","username","password"

or

或者

"jdbc:mysql://127.0.0.1:3306/jsp_test","username","password"

as a connection string.

作为连接字符串。