Debian 上 MySQL 连接池配置指南
一 架构与总体原则
二 应用侧连接池配置示例
spring:
datasource:
url: jdbc:mysql://localhost:3306/mydb?useSSL=false&characterEncoding=UTF-8&cachePrepStmts=true&prepStmtCacheSize=250&prepStmtCacheSqlLimit=2048
username: app
password: StrongPass!
hikari:
maximum-pool-size: 20
minimum-idle: 5
connection-timeout: 30000 # 30秒
idle-timeout: 600000 # 10分钟
max-lifetime: 1800000 # 30分钟
leak-detection-threshold: 60000 # 可选:泄露检测(毫秒)
要点:启用预处理语句缓存、设置连接/空闲/生命周期超时,避免空闲连接过期与泄漏;连接串可按需开启 SSL 与指定字符集。<Context>
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<Resource name="jdbc/myDataSource" auth="Container"
type="javax.sql.DataSource"
maxTotal="100" maxIdle="50" minIdle="10"
maxWaitMillis="30000"
testOnBorrow="true" testOnReturn="false"
validationQuery="SELECT 1" />
</Context>
要点:容器管理数据源;获取连接时校验有效性,避免拿到已失效连接;超时与池大小按并发量调优。三 MySQL 服务器端关键配置
[mysqld]
max_connections = 200
innodb_buffer_pool_size = 1G
innodb_log_file_size = 256M
innodb_flush_log_at_trx_commit = 2
query_cache_size = 64M
query_cache_type = 1
要点:提高 max_connections 以容纳应用池与运维连接;增大 innodb_buffer_pool_size 提升缓冲命中;根据一致性/性能权衡设置 innodb_flush_log_at_trx_commit;适度开启查询缓存(MySQL 5.7 及以下;8.0 起默认移除)。四 容量估算与调优步骤
五 常见问题与排查