MySQL 預設最大連線數 100,高併發場景下極易耗盡。本文介紹兩種調大 max_connections 的方法:啟動腳本修改與原始碼重編譯,並說明驗證步驟。
safe_mysqld(通常位於 /usr/bin/ 或 MySQL 安裝目錄 bin/)。mysqld 啟動的兩行命令(含 if/else 分支),在 --skip-locking 後追加 -O max_connections=1000。
--- 原始
nohup $ledir/mysqld --basedir=$MY_BASEDIR_VERSION --datadir=$DATADIR --skip-locking >> $err_log 2>&1
+++ 修改後
nohup $ledir/mysqld --basedir=$MY_BASEDIR_VERSION --datadir=$DATADIR --skip-locking -O max_connections=1000 >> $err_log 2>&1
service mysql restart 或 systemctl restart mysqld。
mysqladmin -u root -p variables | grep max_connections
# 輸出應顯示 | max_connections | 1000 |
sql/ 目錄編輯 mysqld.cc。
{ "max_connections", (long*) &max_connections, 100, 1, 16384, 0, 1 },
100 改為 1000:
{ "max_connections", (long*) &max_connections, 1000, 1, 16384, 0, 1 },
./configure && make && make install
max_connections 上限受限於作業系統檔案描述符限制(ulimit -n)與記憶體大小,盲目設大反而導致 OOM。wait_timeout、max_user_connections 綜合調優。my.cnf([mysqld] max_connections=1000)是最標準做法,本文兩法適用於無法動態修改配置檔的舊版或受限環境。