一、db.url=jdbc:mysql://127.0.0.1:3306/xxx数据库名称?characterEncoding=utf8mb4&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&serverTimezone=UTC
二、jdbc:mysql://localhost:3306/xxx数据库名称?useUnicode=true&characterEncoding=utf8mb4&serverTimezone=GMT
三、带允许检索公匙参数(mysql8.0.x需要此参数allowPublicKeyRetrieval=true)【推荐】
jdbc:mysql://服务ip:3306/db_name?useUnicode=true&characterEncoding=utf8mb4&useSSL=false&allowMultiQueries=true&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true&autoReconnectForPools=true&serverTimezone=Asia/Shanghai
四、使用p6spy代理,带公钥、时区、开启重新连接、关闭重连后只读等参数
#数据源配置
datasource:
name: mysql
type: com.alibaba.druid.pool.DruidDataSource
#druid相关配置
druid:
#监控统计拦截的filters
filters: stat
#使用p6spy代理类
driver-class-name: com.p6spy.engine.spy.P6SpyDriver
#基本属性
url: jdbc:p6spy:mysql://服务ip:3306/db_name?useUnicode=true&characterEncoding=utf8mb4&useSSL=false&allowMultiQueries=true&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true&autoReconnectForPools=true&serverTimezone=Asia/Shanghai
username: xxx
password: xxx
#配置初始化大小/最小/最大
initial-size: 1
min-idle: 1
max-active: 20
#获取连接等待超时时间
max-wait: 60000
mysql://服务ip:3306/db_name?useUnicode=true&characterEncoding=utf8mb4&useSSL=false&allowMultiQueries=true&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true&autoReconnectForPools=true&serverTimezone=Asia/Shanghai
参数解释:
allowPublicKeyRetrieval:允许检索公匙(mysql8.0.x需要设为true)
useSSL=false:是否使用密钥匹配,(mysql8.0.x需要设为false)
serverTimezone:时区,需要特别注意时区对应是否一致,例如shang_hai是否带下划线等
autoReconnect:使用针对数据库连接池的重连策略
failOverReadOnly:自动重连成功后,连接是否设置为只读?
rewriteBatchedStatements:支持批量写入语句整合,提高批量保存性能
备注:
如果数据库时区晚八个小时,可以执行如下代码查看和更改时区:
# 查看时区
show variables like '%time_zone%';
# 设置时区
set time_zone = 'Asia/Shanghai';
本文提供了多种MySQL数据库连接字符串的配置示例,包括不同场景下的参数设置,如时区、字符集及安全连接等。
3691

被折叠的 条评论
为什么被折叠?



