Druid报错:The last packet successfully received from the server was 377,359 milliseconds ago.

遇到的问题:

        每次查询数据库,druid都会报错,数据还能查出来,但写入会失败

报的错:

com.mysql.cj.jdbc.exceptions.CommunicationsException: The last packet successfully received from the server was 30,053 milliseconds ago. The last packet sent successfully to the server was 30,090 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.

Caused by: com.mysql.cj.exceptions.CJCommunicationsException: The last packet successfully received from the server was 30,053 milliseconds ago. The last packet sent successfully to the server was 30,090 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem. 

Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.

解决方案:

1.数据库配置连接存活时间太短(一般不会是这个的错)

        查看连接状态:

SHOW VARIABLES LIKE 'wait_timeout';

 

         修改默认连接存活时间:

  1. 打开MySQL配置文件 my.cnf(在Windows上可能是 my.ini)。通常,这个文件位于 MySQL 安装目录的 /etc/ 或 /etc/mysql/ 目录下。
  2. 在配置文件中找到 [mysqld] 部分。
  3. 在 [mysqld] 部分中添加或修改以下行(如果不存在):

wait_timeout = 600

  • 这里的值 600 表示连接的超时时间为 600 秒,您可以根据需要进行调整。

         保存并关闭配置文件,重启 MySQL 服务,以使更改生效。

2.druid配置连接存活时间太长

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    dynamic:
      strict: true
      primary: main
      datasource:
        main:
          url: 
          username: 
          password: 
      druid:
      # 初始化大小,最小,最大
        initial-size: 5
        min-idle: 5
        max-active: 20
        # 配置获取连接等待超时的时间
        max-wait: 60000
        # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
        time-between-eviction-runs-millis: 6000
        # 配置一个连接在池中最小生存的时间,单位是毫秒
        min-evictable-idle-time-millis: 200000
        max-evictable-idle-time-millis: 240000
        validation-query: SELECT 1 FROM DUAL
        test-while-idle: true
        test-on-return: true
        # 打开PSCache,并且指定每个连接上PSCache的大小
        pool-prepared-statements: true
        max-pool-prepared-statement-per-connection-size: 20
        # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,wall用于防火墙
        filters: stat,wall,slf4j
  • min-evictable-idle-time-millis: 连接最小存活时间
  • max-evictable-idle-time-millis: 连接最大存活时间

3.数据库版本问题(我是这个问题)

        数据库5.6版本的,有可能与 springboot3 + druid3 兼容有问题,我换了5.7或5.8的数据库都不会报这个错了

  • 19
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值