首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫

CentOS 4.4 下 MySQL 5.0.33 修改預設 max_connections=100 為 1500 實戰

     編輯:MySQL 資料庫相關知識

問題背景

伺服器兩次崩潰,日誌顯示 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

補充建議

  • MySQL 5.1+ 直接在 my.cnf 設定 max_connections=1500 即可,無需改腳本/原始碼。
  • 同步調大 open_files_limit、table_open_cache、thread_cache_size,否則連接數上去了卻受限於檔案描述符或表快取。
  • 監控 Threads_connected / Max_used_connections 確認實際峰值,避免盲目設過大浪費記憶體。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved