我正在尝试设置MySQL 5.6数据库以使用我的应用程序。然而,在使用它时,我似乎无法获得任何类型的连接。这是我得到的错误消息......
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(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:408)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1137)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:356)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2504)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2541)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2323)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:832)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:408)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:417)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:344)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at ServerDatabase.<init>(ServerDatabase.java:25)
at Server.main(Server.java:60)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:258)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:306)
... 16 more
我已经广泛查找了这个问题,但是我找不到解决我的具体问题的解决方案。我找到的最接近的是this但是我按照步骤进行了操作并没有得到任何结果。这是一个帮助诊断的代码片段......
public ServerDatabase() {
Connection conn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/dbname", "dev", "**********");
我也有该用户帐户的MySQL信息......
+------------------------------------------------------------------------------------------------------------------------------+
| Grants for dev@% |
+-------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'dev'@'%' IDENTIFIED BY PASSWORD '*C52E324F3EA43A030DCB940689107C77F44391E4' WITH GRANT OPTION |
+-------------------------------------------------------------------------------------------------------------------------------+
其他一些快速的旁白,希望能够缩小回复......
的[mysqld]
#接下来的三个选项与SERVER_PORT互斥 下方。
#skip-networking
#enable-named-pipe
#MySQL服务器将使用的管道 #socket = mysql#MySQL服务器将侦听的TCP / IP端口
端口= 9118#bind-address = 127.0.0.1
3. I am able to connect using the MySQL command line prompt no problem, and setup the database already.
答案 0 :(得分:0)
修改my.cnf中的绑定地址
"#bind-address = 127.0.0.1"
重启mysql
sudo /etc/init.d/mysql restart
答案 1 :(得分:0)
我找到了一个适合我的解决方案,并且可能适用于任何对编程方面有兴趣或遇到问题的人。我想澄清一下,这是一个简单的方法,可能无法满足每个人的需求。我是一名Java开发人员,我可以非常熟练地发出SQL命令。但是,我不是服务器管理员或类似的任何东西。对我来说,这很有效。
第1步。 下载WAMP服务器。 http://www.wampserver.com/en/#download-wrapper
第2步。 安装+设置。请按照其网站上的说明了解更多详情。
步骤3. 右键单击WAMP图标将服务器设置为在线。之后,您可以使用提供的MySQL控制台登录它(发布时的密码/用户名是root //密码)。默认值可能已更改。
在这之后,我能够使用JDBC立即连接到MySQL数据库。