mysql本地链接一段时间就需要重启项目,否则就链接不上了呢

ops/2024/9/25 23:23:40/
#数据库
spring.datasource.url= jdbc:mysql://XXXXXX:3306/statistics?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&serverTimezone=GMT%2B8&useSSL=false&allowPublicKeyRetrieval=true&rewriteBatchedStatements=true
spring.datasource.username= root
spring.datasource.password= hanxiao418
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# for initial,min,max
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=100
# sql for validating
spring.datasource.validationQuery=SELECT 1
# this will validate idle connection.if validation failed, will remove from pool
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
# max wait time
spring.datasource.maxWait=60000
spring.datasource.timeBetweenEvictionRunsMillis=30000

这个MySQL数据库配置在格式上是正确的,但是你遇到的问题可能由几个潜在原因导致。我可以帮你分析一些常见的原因和可能的解决方案:

  1. 连接超时设置:一些MySQL服务器配置可能会自动关闭空闲连接。你的配置文件中没有显示设置autoReconnect=true。你可以尝试在数据库连接URL中添加这个参数,如下所示:

    spring.datasource.url= jdbc:mysql://XXXXXX:3306/statistics?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&serverTimezone=GMT%2B8&useSSL=false&allowPublicKeyRetrieval=true&rewriteBatchedStatements=true&autoReconnect=true
    
  2. 连接池管理:你使用的连接池配置参数(如maxWait, timeBetweenEvictionRunsMillis, testWhileIdle等)影响连接的有效性检查和回收。可能需要调整这些设置来更好地适应你的使用场景。例如,如果连接在闲置一段时间后经常关闭,可能需要减少timeBetweenEvictionRunsMillis的值或启用testOnBorrow=true来在每次借用连接时验证连接的有效性。

  3. 服务器设置:服务器端也可能有设置影响连接,如wait_timeoutinteractive_timeout。可以登录MySQL服务器,通过以下命令查看这些设置:

    SHOW VARIABLES LIKE 'wait_timeout';
    SHOW VARIABLES LIKE 'interactive_timeout';
    

    如果这些值设置得比较低(例如几分钟),服务器可能会自动关闭空闲连接。

  4. 网络问题:偶尔的网络中断或不稳定也可能导致连接失败。检查网络稳定性和是否有防火墙或安全组设置可能阻止持续的数据库连接。

  5. 资源限制:如果服务器资源有限(如CPU、内存使用高),也可能导致连接问题。监控服务器资源使用情况,必要时优化查询或升级服务器资源。

综合以上,建议从调整连接字符串开始,并观察问题是否解决。如果问题仍然存在,可以考虑调整连接池设置或检查服务器端的超时设置。

# 数据库连接配置
spring.datasource.url= jdbc:mysql://XXXXX:3306/statistics?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&serverTimezone=GMT%2B8&useSSL=false&allowPublicKeyRetrieval=true&rewriteBatchedStatements=true&autoReconnect=true
spring.datasource.username= root
spring.datasource.password= hanxiao418
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver# 连接池配置
spring.datasource.initialSize=5     # 初始化连接数
spring.datasource.minIdle=5         # 最小空闲连接数
spring.datasource.maxActive=100     # 最大活跃连接数# 连接验证配置
spring.datasource.validationQuery=SELECT 1
spring.datasource.testWhileIdle=true        # 空闲时测试连接
spring.datasource.testOnBorrow=true         # 借用连接时测试
spring.datasource.testOnReturn=false        # 归还连接时不测试# 超时和回收配置
spring.datasource.maxWait=60000                               # 最大等待时间(毫秒)
spring.datasource.timeBetweenEvictionRunsMillis=30000         # 连接回收时间间隔(毫秒)# 日志和其他配置(根据需要添加)
logging.level.org.springframework.jdbc.datasource=DEBUG       # 启用详细日志以监控数据库操作和连接池状态

http://www.ppmy.cn/ops/23620.html

相关文章

Centos7 yum报错 Could not resolve host: mirrorlist.centos.org

yum install报如下错误 应该是网络问题,检查是不是这个文件配置错了导致连不上网 /etc/sysconfig/network-scripts/ifcfg-ens33 注意里面的DNS配置 可以在服务器ping一下百度 ping wwww.baidu.com

外汇返佣是什么意思?是不是越高越好?

外汇返佣是外汇经纪商提供的一种激励措施,用于返还部分交易费用给交易者。外汇返佣的具体内容可以从以下几个方面进行详细解释: 1、返佣的形式: 返佣通常根据交易量来计算,可以是按照每手交易的数量来返还一定金额,或者…

乐观锁和悲观锁

概述 悲观锁 坏事一定会发生,所以先做预防(上锁) 写多读少 乐观锁:坏事不一定会发生,所以事后补偿 写少读多 悲观锁 select for update,sychronized等,乐观锁,乐观锁如cas和版本号 …

C语言 | Leetcode C语言题解之第46题全排列

题目: 题解: void swap(int * nums,int indexA,int indexB) {int temp nums[indexA];nums[indexA] nums[indexB];nums[indexB] temp; }void prem(int* nums, int numsSize, int* returnSize, int** returnColumnSizes,int** returnNums,int offset)…

C# [Flags]属性

在C#中,当你看到[Flags]这个属性被用于枚举(enum),它意味着这个枚举设计用来支持位运算,以便可以组合多个枚举值。这通常用于表示可以独立打开或关闭的多个选项或标志。 例如,考虑一个枚举,它代…

Dockerfile 构建上下文 build -f 选项 加快构建速度

理解构建上下文(Build Context) 当运行 docker build 命令时,当前工作目录被称为构建上下文,docker本身会将工作目录里面所有的文件都上传给docker daemon,在这个基础之上再去构建容器镜像。(如果你在根目…

c++在visual studio上的默认配置

右键 新建项 右键源文件 属性

photoshop如何使用PS中的吸管工具吸取软件外部的颜色?

第一步,打开PS,随意新建一个画布,或打开一个图片。 第二步,将PS窗口缩小,和外部窗口叠加放置,以露出后面的其它页面。 第三步,选中吸管工具,在PS窗口内单击一点吸取颜色,…