伺服器兩次崩潰,日誌顯示 Too many connections。檢查發現 MySQL 5.0.33 編譯安裝預設 max_connections=100,完全不夠用。
mysqld_safe(推薦、熱修改)vi /usr/local/mysql/bin/mysqld_safe
# 找到兩處 mysqld 啟動行(約第 400-500 行),在參數末尾加入:
-O max_connections=1500
# 範例位置:
then
$NOHUP_NICENESS $ledir/$MYSQLD \
$defaults --basedir=$MY_BASEDIR_VERSION \
--datadir=$DATADIR $USER_OPTION \
--pid-file=$pid_file \
--skip-external-locking \
-O max_connections=1500 \ # ← 新增
>> $err_log 2>&1
else
eval "$NOHUP_NICENESS $ledir/$MYSQLD \
$defaults --basedir=$MY_BASEDIR_VERSION \
--datadir=$DATADIR $USER_OPTION \
--pid-file=$pid_file \
--skip-external-locking $args \
-O max_connections=1500 \ # ← 新增
>> $err_log 2>&1"
fi
存檔後重啟:service mysqld restart
vi sql/mysqld.cc
# 找到:
{"max_connections", OPT_MAX_CONNECTIONS,
"The number of simultaneous clients allowed.",
(gptr*) &max_connections, (gptr*) &max_connections,
0, GET_ULL, REQUIRED_ARG, 100, 1, 16384, 0, 1}, # 100 為預設值
# 改為:
{"max_connections", OPT_MAX_CONNECTIONS,
"The number of simultaneous clients allowed.",
(gptr*) &max_connections, (gptr*) &max_connections,
0, GET_ULL, REQUIRED_ARG, 1500, 1, 16384, 0, 1}, # 1500 為新預設值
# 重新編譯安裝
./configure && make && make install
/usr/local/mysql/bin/mysqladmin -u root -p variables | grep max_connections
# 輸出應顯示:max_connections 1500
my.cnf 設定 max_connections=1500 即可,無需改腳本/原始碼。open_files_limit、table_open_cache、thread_cache_size,否則連接數上去了卻受限於檔案描述符或表快取。Threads_connected / Max_used_connections 確認實際峰值,避免盲目設過大浪費記憶體。