jdbc:mysql://[host:port],[host:port].../[database][?param1=value1¶m2=value2...]
| 參數 | 說明 | 預設值 | 最低版本 |
|---|---|---|---|
| user | 資料庫用戶名 | - | 所有 |
| password | 用戶密碼 | - | 所有 |
| useUnicode | 是否使用 Unicode 字元集 | false | 1.1g |
| characterEncoding | 字元編碼 (如 utf8, gbk),需 useUnicode=true | false | 1.1g |
| autoReconnect | 連線中斷自動重連 | false | 1.1 |
| autoReconnectForPools | 連線池專用重連策略 | false | 3.1.3 |
| failOverReadOnly | 重連成功後設為唯讀 | true | 3.0.12 |
| maxReconnects | 最大重試次數 (autoReconnect=true 時) | 3 | 1.1 |
| initialTimeout | 重試間隔秒數 | 2 | 1.1 |
| connectTimeout | 建立 Socket 超時毫秒數 (0=無限) | 0 | 3.0.1 |
| socketTimeout | 讀寫操作超時毫秒數 (0=無限) | 0 | 3.0.1 |
| useSSL | 是否使用 SSL | true (5.7+) | 5.1 |
| serverTimezone | 伺服器時區 (如 Asia/Taipei, UTC) | - | 5.1.35 |
| allowPublicKeyRetrieval | 允許獲取 RSA 公鑰 (caching_sha2_password) | false | 8.0 |
jdbc:mysql://localhost:3306/mydb?user=root&password=xxx&useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false&serverTimezone=Asia/Taipei&useSSL=false
# HikariCP / Druid / DBCP2 建議額外加上
autoReconnect=true
failOverReadOnly=false
maxReconnects=3
initialTimeout=2
connectTimeout=10000
socketTimeout=30000
在 server.xml、spring.xml 等 XML 中,& 必須轉義為 &:
<Resource name="jdbc/MyDB"
auth="Container"
type="javax.sql.DataSource"
url="jdbc:mysql://localhost:3306/mydb?user=root&password=xxx&useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false"
driverClassName="com.mysql.cj.jdbc.Driver"
... />
com.mysql.cj.jdbc.Driver (舊版 com.mysql.jdbc.Driver 已棄用)caching_sha2_password,需 allowPublicKeyRetrieval=true 或改用 mysql_native_passwordserverTimezone 在 8.0+ 強制要求,否則報錯