MySQL 預設 max_connections=100,稍有流量的應用瞬間打滿,報錯 Too many connections。三種修改方案按推薦度排序:
# 1. 找到配置文件
# Windows: my.ini (安裝目錄或 C:\ProgramData\MySQL\MySQL Server X.Y\)
# Linux: /etc/my.cnf 或 /etc/mysql/my.cnf
# 2. 編輯 [mysqld] 區段
[mysqld]
max_connections = 1000
# 3. 重啟生效
# Windows: 服務管理器重啟 MySQL
# Linux: systemctl restart mysqld / service mysql restart
# 4. 驗證
mysql -uroot -p -e "SHOW VARIABLES LIKE 'max_connections';"
# 輸出: max_connections 1000
# 1. 登入 MySQL
mysql -uroot -p
# 2. 修改全局變量 (立即生效)
mysql> SET GLOBAL max_connections = 2000;
Query OK, 0 rows affected (0.00 sec)
# 3. 驗證
mysql> SHOW VARIABLES LIKE 'max_connections';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 2000 |
+-----------------+-------+
# 4. 關鍵:必須同步寫入配置文件,否則重啟恢復預設
# 編輯 my.cnf [mysqld] 區段加入 max_connections = 2000
# 當前運行查詢
SHOW PROCESSLIST;
# 或詳細
SHOW FULL PROCESSLIST;
# 連接狀態統計
SHOW STATUS LIKE '%connect%';
# Threads_connected: 當前連接數
# Max_used_connections: 歷史峰值
# Aborted_connects: 失敗連接數
# Connection_errors_max_connections: 因達上限拒絕數
# 命令行查看變量
mysqladmin -uroot -p variables | grep max_connections
# 1. 編輯 mysqld_safe 啟動腳本
vi /usr/local/mysql/bin/mysqld_safe
# 2. 找到 mysqld 啟動行 (通常有兩處:前台/後台)
# 在 $args 之前添加 -O 參數 (舊版本語法)
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
⚠️ -O 是舊版本 (≤5.0) 語法,**MySQL 5.1+ 建議用 --max-connections=1500** 格式。
# 1. 解壓源碼,編輯 sql/mysqld.cc
# 找到:
{"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},
# 2. 修改預設值 100 → 1500 (第 8 個參數)
{"max_connections", OPT_MAX_CONNECTIONS,
"The number of simultaneous clients allowed.",
(gptr*) &max_connections, (gptr*) &max_connections,
0, GET_ULONG, REQUIRED_ARG, 1500, 1, 16384, 0, 1},
# 3. 重新編譯安裝
./configure [your_options]
make -j$(nproc)
make install
# 4. 重啟驗證
| 應用類型 | 建議 max_connections | 依據 |
|---|---|---|
| 小型內部系統 | 200-500 | 併發低、連接復用好 |
| 中型 Web 應用 | 1000-2000 | 連接池 50-200,峰值緩衝 |
| 高併發電商/社交 | 3000-5000+ | 需配合 thread_pool、連接池調優 |
總內存 ≈ global_buffers + (thread_buffers × max_connections)
global_buffers: innodb_buffer_pool_size + key_buffer_size + innodb_log_buffer_size + ...
thread_buffers: sort_buffer_size + join_buffer_size + read_buffer_size + read_rnd_buffer_size + thread_stack + ...
# 例: thread_buffers ≈ 2-4MB, max_connections=2000 → 需 4-8GB 線程內存
[mysqld]
max_connections = 2000
back_log = 500 # 等待隊列,突發緩衝
thread_cache_size = 200 # 線程緩存,減少創建銷毀開銷
open_files_limit = 65535 # 文件描述符上限
table_open_cache = 4000 # 表緩存
innodb_buffer_pool_size = 4G # 核心,視內存 70-80%
# 系統級
ulimit -n 65535
# /etc/security/limits.conf
mysql soft nofile 65535
mysql hard nofile 65535
修改 max_connections 只是第一步,**連接池配置、線程模型、內存規劃、文件描述符** 才是支撐高併發的完整體系。監控 Max_used_connections 峰值,預留 30% 緩衝,動態調整,才是運維正道。