当前位置:网站首页>Resolved: JDBC connection to MySQL failed with an error:'The last packet sent successfully to the server was 0 milliseconds ago. '

Resolved: JDBC connection to MySQL failed with an error:'The last packet sent successfully to the server was 0 milliseconds ago. '

2022-06-11 03:08:00 CSDN Q & A

First attempt JDBC Connect Mysql, And then keep reporting the error .
Error code :
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failureThe last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.    at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:172)    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:862)    at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:444)    at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:230)    at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:226)    at connection.connectionTest.connectionTest1(connectionTest.java:44)    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)    at java.lang.reflect.Method.invoke(Method.java:498)    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failureThe 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:62)    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:59)    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:103)    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:149)    at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:165)    at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:92)    at com.mysql.cj.NativeSession.connect(NativeSession.java:152)    at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:982)    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:852)    ... 26 moreCaused by: java.net.ConnectException: Connection refused (Connection refused)    at java.net.PlainSocketImpl.socketConnect(Native Method)    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:476)    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:218)    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:200)    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:394)    at java.net.Socket.connect(Socket.java:606)    at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:173)    at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:66)    ... 29 more
Operation results and error reporting contents
Tried to modify the time zone 、 Character set 、wait_timeout And so on. , But it didn't work . Then the driver uses :“mysql-connector-java-8.0.17.jar”, mysql It's also 8.0 Version above , There should be no problem with matching .
url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&userUnicode=true&charsetEncoding=utf-8
url=jdbc:mysql://localhost:3306/testdriverClass=com.mysql.cj.jdbc.Driver
I hope I can help you see , Is learning JDBC, But the first step failed to connect .
Problems encountered later :
  1. ping no 3306
  2. sudo /usr/local/mysql/support-files/mysql.server startStarting MySQLSUCCESS! 
    Start mysql after , The connection still failed .
    open Navicat And found out : Can't connect to local server through socket '/tmp/mysql.sock' (2)
    wait . Is a series of problems , So I decided to uninstall today mysql.

Cannot delete... From the command line mysql after , I am directly in the system preferences Mysql Choose uninstall. Then on the official website https://downloads.mysql.com/archives/community/
Downloaded the computer version of dmg file , Direct installation . The choice is Strong password, The choice before was legacy password( It's the old way ). First download before mysql after , Try to Navicat On the establishment of mysql Connections often fail , Later, I checked the use set file and wrote the location of the host as “127.0.0.1”, The connection name is written as "localhost" To succeed .

Now uninstall and reinstall , actually JDBC Connect mysql succeed ! Thank you. !

原网站

版权声明
本文为[CSDN Q & A]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206041136449098.html