一、db.url=jdbc:mysql://127.0.0.1:3306/xxx数据库名称?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&serverTimezone=UTC
二、jdbc:mysql://localhost:3306/xxx数据库名称?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT
三、jdbc:mysql://127.0.0.1:3306/库名?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&serverTimezone=UTC
四、带允许检索公匙参数(mysql8.0.x需要此参数allowPublicKeyRetrieval=true)【推荐】
jdbc:mysql://IP:3306/库名?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true&autoReconnectForPools=true&allowPublicKeyRetrieval=true
五、使用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://ipxxx:3306/xxx库名?useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=GMT&autoReconnect=true&failOverReadOnly=false
username: xxx
password: xxx
#配置初始化大小/最小/最大
initial-size: 1
min-idle: 1
max-active: 20
#获取连接等待超时时间
max-wait: 60000
jdbc:mysql://xxxip:3306/xxx库名?useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=GMT&autoReconnect=true&failOverReadOnly=false
参数解释:
allowPublicKeyRetrieval:允许检索公匙(mysql8.0.x需要设为true)
useSSL=false:是否使用密钥匹配,(mysql8.0.x需要设为false)
serverTimezone:时区,需要特别注意时区对应是否一致,例如shang_hai是否带下划线等
autoReconnect:使用针对数据库连接池的重连策略
failOverReadOnly:自动重连成功后,连接是否设置为只读?
六、相对完善的连接写法
1、方式一
jdbc:mysql://127.0.0.1:3306/xxx数据库名称?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&autoReconnect=true&autoReconnectForPools=true&serverTimezone=Asia/Shanghai
2、方式二
jdbc:mysql://服务IP:3306/库名?useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowMultiQueries=true&allowPublicKeyRetrieval=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true&autoReconnectForPools=true&serverTimezone=Asia/Shanghai
3、方式三(JK_Group写法)
jdbc:mysql://IP:端口/库名?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&useSSL=false
//如果mysql是8.x以上版本,携带此参数
allowPublicKeyRetrieval=true
备注:
如果数据库时区晚八个小时,可以执行如下代码查看和更改时区:
# 查看时区
show variables like '%time_zone%';
# 设置时区
set time_zone = 'Asia/Shanghai';