mysql查询报错
今天出现一个很奇怪的问题,启动项目连接mysql没有任何报错,但是查询语句就报错如下
2023-08-18 11:13:06.313 [http-nio-8080-exec-2] ERROR com.alibaba.druid.pool.DruidDataSource :1954 {conn-10010} discard
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
The last packet successfully received from the server was 13,930 milliseconds ago. The last packet sent successfully to the server was 13,930 milliseconds ago.
重点问题就是
Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
按照这个报错在网上查了很久。
下面就是我的排查流程
yml的配置排查,yml配置如下
# 数据库配置
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
platform: mysql
type: com.alibaba.druid.pool.DruidDataSource
druid:
initial-size: 10 # 连接池初始化大小
min-idle: 10 # 最小空闲连接数
max-active: 30 # 最大连接数
maxWait: 6000000
web-stat-filter:
exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*" # 不统计这些请求数据
stat-view-servlet: # 访问监控网页的登录用户名和密码
login-username: druid
login-password: druid
其中jdbc的放在了dev中
spring:
datasource:
url: jdbc:mysql://localhost:3306/test_ea?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&useSSL=false&serverTimezone=Asia/Shanghai
username: username
password: password
这是pom的配置
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
<scope>runtime</scope>
</dependency>
<!--druid连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.16</version>
</dependency>
这里首先我是检查了url是否正确,我换成了mysql自带的驱动
# 数据库配置
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.zaxxer.hikari.HikariDataSource
hikari:
maximum-pool-size: 60
换成自带的是能正常查询
这里就排查url的问题
这时候去百度了好久怀疑是不是mysql的版本问题
一查发现我的mysql版本是
8.0.18
这时候发现我得pom是8.0.30
改了对应的版本号,
问题解决了
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
<scope>runtime</scope>
</dependency>
这个问题困扰我好久,没想到是这个问题,由于这个pom是直接从其他项目复制的,根本没想到这个问题。
希望对大家有所帮助。