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

Linux 下 MySQL 5.0 修改默認最大連接數兩種方法:啟動腳本與源碼編譯

     編輯:MySQL 資料庫相關知識

背景

默認 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 納入配置管理/自動化部署腳本,避免升級遺漏。

補充:my.cnf 為何不生效?

mysqld_safe 腳本以命令行參數 -O max_connections=... 顯式啟動 mysqld,命令行優先級高於配置文件。若腳本未硬編碼,my.cnf 中 [mysqld] max_connections=1500 即可生效。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved