MySQL连接数据源(数据库)常用的配置写法方式

本文提供了多种MySQL数据库连接字符串的配置示例,包括不同场景下的参数设置,如时区、字符集及安全连接等。

一、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';

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值