Spring Boot 连接 MySQL 配置参数详解

前言

在 Spring Boot 中使用 Druid 连接池配置 MySQL 数据库连接时,URL 中 ? 后面的参数用于指定连接的各种属性。以下是常见参数及其含义:

参数及含义

常用参数及讲解和示例

在这里插入图片描述

useUnicode 参数说明:

useUnicode=true 表示启用 Unicode 字符集传输,通常与 characterEncoding 一起使用:

useUnicode=true&characterEncoding=UTF-8

这组参数确保:

数据库能正确处理非 ASCII 字符(如中文)

客户端和服务器之间使用 UTF-8 编码传输数据

完整配置示例

spring.datasource.druid.master.url=jdbc:mysql://localhost:3306/db_name?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
spring.datasource.druid.master.username=root
spring.datasource.druid.master.password=123456
spring.datasource.druid.master.driver-class-name=com.mysql.cj.jdbc.Driver

注意事项

MySQL 8.0+ 需要添加时区设置 serverTimezone

生产环境应考虑使用 SSL 连接 (useSSL=true)

批量操作时可启用 rewriteBatchedStatements 提高性能

高并发场景可调整连接池参数和超时设置

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值