默認 max_connections=100 極易耗盡導致「Too many connections」錯誤。網上資料多針對舊版(修改 safe_mysqld),新版啟動腳本為 mysqld_safe,配置文件 my.cnf 直接改無效(被啟動腳本覆蓋)。
mysqld_safe(推薦,無需重編譯)
vi /usr/local/mysql/bin/mysqld_safe
# 找到兩處 mysqld 啟動命令行(if/else 分支),在參數末尾添加:
-O max_connections=1500
# 示例位置:
$NOHUP_NICENESS $ledir/$MYSQLD $defaults --basedir=... --datadir=... --pid-file=... --skip-external-locking \
-O max_connections=1500 >> $err_log 2>&1
# 另一分支同理添加
eval "$NOHUP_NICENESS $ledir/$MYSQLD $defaults ... $args \
-O max_connections=1500 >> $err_log 2>&1"
重啟驗證:
service mysqld restart
mysqladmin -uroot -p variables | grep max_connections
# 輸出 max_connections 1500 即生效
# 1. 解壓源碼,編輯 sql/mysqld.cc
# 找到 max_connections 定義行(約第幾行視版本而定):
{"max_connections", OPT_MAX_CONNECTIONS,
"The number of simultaneous clients allowed.",
(gptr*) &max_connections, (gptr*) &max_connections,
0, GET_ULONG, REQUIRED_ARG, 100, 1, 16384, 0, 1, 0},
# 將默認值 100 改為 1500:
{"max_connections", OPT_MAX_CONNECTIONS,
...
1500, 1, 16384, 0, 1, 0},
# 2. 重新編譯安裝
./configure [你的原配置參數]
make && make install
# 3. 替換二進制文件,重啟驗證
| 維度 | 修改啟動腳本 | 修改源碼編譯 |
|---|---|---|
| 生效速度 | 秒級(重啟即可) | 需完整編譯安裝流程 |
| 升級維護 | 升級後需重複修改腳本 | 一次修改,後續版本需重新打補丁 |
| 風險 | 極低 | 編譯環境依賴、潛在構建失敗 |
| 適用場景 | 生產環境快速調整、二進制部署 | 定制發佈版、嵌入式場景 |
強烈建議:優先使用方法一,並將 max_connections 納入配置管理/自動化部署腳本,避免升級遺漏。
mysqld_safe 腳本以命令行參數 -O max_connections=... 顯式啟動 mysqld,命令行優先級高於配置文件。若腳本未硬編碼,my.cnf 中 [mysqld] max_connections=1500 即可生效。