jdbc:mysql://[host:port],[host:port].../[database][?param1=value1¶m2=value2...]
| 參數名 | 說明 | 預設值 | 最低版本 | 建議生產值 |
|---|---|---|---|---|
| user | 資料庫用戶名 | 無 | 所有 | 業務專用帳號(非 root) |
| password | 用戶密碼 | 無 | 所有 | 配置中心加密存儲 |
| useUnicode | 啟用 Unicode 字符集 | false | 1.1g | true |
| characterEncoding | 字符編碼(配合 useUnicode=true) | false | 1.1g | utf8 / utf8mb4 |
| autoReconnect | 連線中斷自動重連 | false | 1.1 | false(見下文) |
| autoReconnectForPools | 連線池專用重連策略 | false | 3.1.3 | true |
| failOverReadOnly | 重連成功後是否唯讀 | true | 3.0.12 | false |
| maxReconnects | 最大重試次數 | 3 | 1.1 | 3~5 |
| initialTimeout | 重連間隔秒數 | 2 | 1.1 | 2~5 |
| connectTimeout | 建立 Socket 連線超時(ms) | 0(無限) | 3.0.1 | 10000 (10s) |
| socketTimeout | Socket 讀寫超時(ms) | 0(無限) | 3.0.1 | 30000 (30s) |
| useSSL | 啟用 SSL/TLS | true(5.7+) | 5.1 | 內網可 false,外網 true |
| verifyServerCertificate | 驗證伺服器憑證 | true | 5.1 | 生產 true |
| serverTimezone | 伺服器時區(8.0+ 必填) | 無 | 8.0 | Asia/Shanghai 或 UTC |
| rewriteBatchedStatements | 批次重寫為多值 INSERT | false | 5.1 | true(大幅提升批次寫入) |
| cachePrepStmts | 緩存 PreparedStatement | false | 5.1 | true |
| prepStmtCacheSize | 預編譯緩存大小 | 25 | 5.1 | 250~500 |
| prepStmtCacheSqlLimit | 單條 SQL 緩存長度上限 | 256 | 5.1 | 2048 |
| useServerPrepStmts | 使用服務端預編譯 | false | 5.1 | true |
| maintainTimeStats | 維護查詢時間統計 | true | 5.1 | false(減少開銷) |
jdbc:mysql://localhost:3306/test?user=root&password=xxx&useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true&cachePrepStmts=true
# 必須:解決連線池「僵屍連線」問題
autoReconnect=true&failOverReadOnly=false&autoReconnectForPools=true
# 效能優化:批次寫入、預編譯緩存
rewriteBatchedStatements=true&cachePrepStmts=true&prepStmtCacheSize=250&prepStmtCacheSqlLimit=2048&useServerPrepStmts=true
# 穩健性:超時控制
connectTimeout=10000&socketTimeout=30000
XML 中 `&` 必須轉義為 `&`,否則解析報錯。
<Resource name="jdbc/MySQLDS"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.cj.jdbc.Driver"
url="jdbc:mysql://localhost:3306/test?user=root&password=xxx&useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true"
maxTotal="100"
maxIdle="20"
minIdle="5"
maxWaitMillis="10000"
testWhileIdle="true"
validationQuery="SELECT 1"
/>
| 現象 | 原因 | 解決 |
|---|---|---|
| 中文亂碼 | 未設 `useUnicode=true&characterEncoding=utf8` 或資料表非 utf8mb4 | URL 加參數、表/庫/欄位統一 `CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci` |
| 連線池報 Communications link failure | MySQL `wait_timeout`(預設 8h) < 連線池 `idleTimeout`,連線被服務端斷開 | 連線池設 `maxLifetime` < `wait_timeout`,或啟用 `testWhileIdle` + `validationQuery` |
| SSLHandshakeException | 5.7+ 預設 `useSSL=true` 但無憑證 | 內網加 `useSSL=false`,外網配置信任憑證 |
| Public Key Retrieval is not allowed | 8.0+ `caching_sha2_password` 需 RSA 公鑰 | 加 `allowPublicKeyRetrieval=true` 或改用 `mysql_native_password` |
| 時區報錯 CST/UTC 混淆 | 8.0+ 必須顯式指定 `serverTimezone` | 加 `serverTimezone=Asia/Shanghai` |
JDBC 連線參數直接影響應用穩定性與效能。核心原則:**顯式指定編碼與時區、開啟批次與預編譯緩存、設定合理超時、連線池存活期短於資料庫 wait_timeout、XML 嚴格轉義**。建議將上述推薦參數封裝為組織內部「標準 JDBC URL 模板」,減少個別專案踩坑。