当前位置:网站首页>已解决: JDBC连接Mysql失败报错: 'The last packet sent successfully to the server was 0 milliseconds ago. '
已解决: JDBC连接Mysql失败报错: 'The last packet sent successfully to the server was 0 milliseconds ago. '
2022-06-11 02:40:00 【CSDN问答】
第一次尝试JDBC连接Mysql, 然后一直报错。
报错代码:
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运行结果及报错内容
尝试过修改时区、字符集、wait_timeout之类的,但是没有效果。然后驱动使用的是:“mysql-connector-java-8.0.17.jar”, mysql也是8.0版本以上的, 匹配应该没有问题。
url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&userUnicode=true&charsetEncoding=utf-8url=jdbc:mysql://localhost:3306/testdriverClass=com.mysql.cj.jdbc.Driver希望可以帮忙看看, 正在学习JDBC,但是第一步连接失败。
后来遇到的问题:
- ping不通3306
开始mysql之后,连接仍然失败。sudo /usr/local/mysql/support-files/mysql.server startStarting MySQLSUCCESS!
打开Navicat又发现: Can't connect to local server through socket '/tmp/mysql.sock' (2)
等等。 就是一系列的问题, 于是决定今天卸载mysql。
通过命令行无法删除mysql后,我直接在系统偏好设置的Mysql下选择uninstall。 然后在官网https://downloads.mysql.com/archives/community/
下载了符合电脑版本的dmg文件,直接安装。 选择的是Strong password, 之前选择的是legacy password(就是旧的方法). 之前第一次下载mysql后,尝试在Navicat上建立mysql连接经常失败, 后来勾选了使用套文件又把主机的位置那里写成“127.0.0.1”, 连接名写成"localhost"才成功。
现在卸载后重新安装后,竟然JDBC连接mysql成功了!谢谢大家!
边栏推荐
- How to handle error code 30204-44 when installing office 2016 in win10?
- 第七章 常用的协议简介(1)
- Array full permutation
- Net core Tianma XingKong series - Interface Implementation for dependency injection and mutual conversion of database tables and C entity classes
- OpenJudge NOI 1.13 18:Tomorrow never knows?
- Problems with JDBC tool classes
- [189. rotation array]
- Shell 按行读取文件
- 重磅直播!ORB-SLAM3系列之特征匹配(MLPnP、词袋模型等)。
- jdbc工具類的問題
猜你喜欢

Navicat premium 15 tool is automatically deleted by anti-virus protection software solution

B_QuRT_User_Guide(18)

Help you distinguish GNU, GCC, GCC and G++

【新晋开源项目】动态配置化任务编排框架 Gobrs-Async 加入Dromara开源社区

GraphAcademy 课程讲解:《Neo4j 图数据科学基础》

那些笑着离开“北上广”的人,为何最后都哭了?

Necessity for banks to choose electronic bidding procurement

【大咖秀】博睿数据眼中的AIOps,选择正确的赛道正确的人
![[Fibonacci series]](/img/03/70b1363e91142a8600d97c59b01b0f.png)
[Fibonacci series]

Google Gmail mailbox marks all unread messages as read at once
随机推荐
The two departments jointly issued the nine provisions on fire safety management of off campus training institutions
Can Xiaoxiang life become the "Yonghui" in the discount industry after the completion of the round a financing of tens of millions of yuan?
Three ways of reflection
GraphAcademy 課程講解:《Neo4j 圖數據科學基礎》
jdbc工具類的問題
Construction of Flink development environment and wordcount
微信小程序
富络经典源码富络经典系统开发原理分享
[resolved] how to fix another update in progress WordPress upgrade error
HOSATAPD_ CLI debug using WiFi whitelist
数组全全排列
ROS基础 - 使用 launch 文件(一) - 批量启动多个ROS节点
Will your company choose to develop data center?
AOSP - Developer mode is enabled by default
求MySQL先按大于等于当前时间升序排序,再按小于当前时间降序排序
Go quick start of go language (I): the first go program
CPT 102_ LEC 18
jdbc工具类的问题
Longest increasing subsequence
巴歇尔槽流量计远程采集物联网关在明渠流量监测的应用