Files
SuperBizAgent-java/knowledge_base/infrastructure/mysql-connection-pool.md
T
zhuyongxin 3956426c97 docs(knowledge): 添加测试知识库文档
新增 6 个知识库文档,用于测试 L0+L1 混合检索功能:

API 类:
- payment-errors.md - 支付网关错误码定义

领域知识类:
- spring-ai-tool-best-practices.md - Spring AI 工具定义最佳实践

基础设施类:
- redis-config.md - Redis 缓存配置指南
- mysql-connection-pool.md - MySQL 连接池配置
- flyway-best-practices.md - Flyway 数据库迁移最佳实践

故障排查类:
- fault-diagnosis-process.md - 故障诊断流程规范

所有文档均包含:
- 标准 frontmatter 元数据 (title, keywords, summary, category)
- 实用配置示例和代码片段
- 支持 L0 精确匹配的关键词
2026-06-24 16:19:10 +08:00

100 lines
2.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: MySQL 数据库连接池配置
keywords: [MySQL, HikariCP, 连接池, 数据库, 性能优化]
summary: MySQL 连接池的配置参数、性能调优和故障排查指南
category: infrastructure
---
# MySQL 数据库连接池配置
## HikariCP 配置
### 基础配置
```yaml
spring:
datasource:
url: jdbc:mysql://localhost:3306/superbiz_agent?useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: password
driver-class-name: com.mysql.cj.jdbc.Driver
hikari:
maximum-pool-size: 10
minimum-idle: 5
connection-timeout: 30000
idle-timeout: 600000
max-lifetime: 1800000
```
## 关键参数说明
### maximum-pool-size
- **默认值**:10
- **建议值**:根据并发量调整
- **公式**:connections = ((core_count * 2) + effective_spindle_count)
- **注意**:不是越大越好,过大会增加数据库负担
### connection-timeout
- **默认值**:30000ms (30秒)
- **说明**:等待连接的最大时间
- **建议**:根据业务超时要求调整
### idle-timeout
- **默认值**:600000ms (10分钟)
- **说明**:连接空闲多久后被释放
- **建议**:小于 MySQL wait_timeout
## 常见问题
### 连接泄漏
**症状**:
- 应用无法获取数据库连接
- 日志显示 "Connection is not available"
**排查**:
```java
// 检查是否有未关闭的连接
try (Connection conn = dataSource.getConnection()) {
// 使用连接
} // 自动关闭
```
**解决**:
- 使用 try-with-resources
- 检查事务是否正常提交/回滚
### wait_timeout 超时
**症状**:MySQL 错误 "The last packet successfully received from the server was X milliseconds ago"
**排查**:
```sql
SHOW VARIABLES LIKE 'wait_timeout';
```
**解决**:
```yaml
hikari:
max-lifetime: 1800000 # 小于 MySQL wait_timeout
```
## 性能监控
### HikariCP 指标
```java
HikariPoolMXBean poolMXBean = hikariDataSource.getHikariPoolMXBean();
int active = poolMXBean.getActiveConnections();
int idle = poolMXBean.getIdleConnections();
int total = poolMXBean.getTotalConnections();
```
### 慢查询监控
```sql
-- 开启慢查询日志
SET GLOBAL slow_query_log = 'ON';
SET GLOBAL long_query_time = 2;
-- 查看慢查询
SELECT * FROM mysql.slow_log ORDER BY start_time DESC LIMIT 10;
```