mysql 连接出现The last packet sent successfully to the server was 0 milliseconds ago.问题解决

最近自己在学习mysql,在使用jdbc连接mysql的时候。出现了这个错误。

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:425)
	at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:989)
	at com.mysql.jdbc.MysqlIO.(MysqlIO.java:341)
	at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2196)
	at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2229)
	at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2024)
	at com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:779)
	at com.mysql.jdbc.JDBC4Connection.(JDBC4Connection.java:47)
	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:425)
	at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:389)
	at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:330)
	at java.sql.DriverManager.getConnection(Unknown Source)
	at java.sql.DriverManager.getConnection(Unknown Source)
	at com.ad.MysqlDemo.main(MysqlDemo.java:32)
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 com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:211)
	at com.mysql.jdbc.MysqlIO.(MysqlIO.java:300)
	... 15 more
Exception in thread "main" java.lang.NullPointerException
	at com.ad.MysqlDemo.main(MysqlDemo.java:57)

拒绝连接了,差看了半天url发现都是正确的,去百度查找答案,发现尝试了设置回收时间
  [mysqld]
  wait_timeout=86400
仍然解决不了问题,然后又想到使用Navicat连接试试,果然报错
2003-can't connect to mysql server on '******'(10061)

然后又去查找这个问题解决办法,修改mysql安装目录中my.cnf文件

修改bind-address = 127.0.0.1
为 bind-address = 0.0.0.0
输入命令
sudo service mysql restart
重新启动mysql后又使用Navicat连接后,发现可以成功连接,然后又尝试用之前的jdbc连接,原先问题也解决了。

【总结】

The last packet sent successfully to the server was 0 milliseconds ago,也不一定都是连接回收时间的问题,还有可能是其他问题遇到这种情况也可以尝试使用上述修改配置的方法来尝试重新解决。




你可能感兴趣的:(mysql 连接出现The last packet sent successfully to the server was 0 milliseconds ago.问题解决)