修改 my.cnf 設定 max_connections 後未生效,常因配置位置錯誤 (未放在 [mysqld] 區段)。本文說明正確配置位置、驗證方法、相關參數聯動調優。
# my.cnf 內容
max_connections=2000
[mysqld]
port = 3306
socket = /tmp/mysql.sock
-- 重啟後仍顯示 151 (預設值)
MySQL 配置文件採用 INI 格式,**參數必須放在對應的區段下**。max_connections 屬於伺服器端參數,必須在 [mysqld] 區段。
[mysqld]
max_connections = 2000
port = 3306
socket = /tmp/mysql.sock
-- 其他參數...
mysql> SHOW VARIABLES LIKE 'max_connections';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 2000 |
+-----------------+-------+
| 參數 | 建議值 | 說明 |
|---|---|---|
max_connect_errors | 100000 | 防誤封,配合防火牆 |
thread_cache_size | min(100, max_connections/10) | 減少線程創建銷毀開銷 |
open_files_limit | max_connections * 5 | 系統層面文件描述符限制 |
table_open_cache | max_connections * 2 | 表緩存,避免頻繁打開關閉 |
innodb_buffer_pool_size | 物理記憶體 60-80% | 核心緩衝池,與連線數無直接關係但需配合記憶體 |
# 1. 進程文件描述符限制
cat /proc/$(pidof mysqld)/limits | grep "Max open files"
# 需 >= open_files_limit
# 2. 系統全域限制
cat /proc/sys/fs/file-max
# 3. 用戶限制 (/etc/security/limits.conf)
mysql soft nofile 65535
mysql hard nofile 65535
# 4. systemd 服務限制 (/usr/lib/systemd/system/mysqld.service)
[Service]
LimitNOFILE=65535
LimitNPROC=65535
max_connections,預留 10-20% 給管理連線、備份、監控testOnBorrow=true、validationQuery=SELECT 1Threads_connected, Threads_running, Connection_errors_max_connections[mysqld] 區段?SHOW VARIABLES 是否生效?ulimit -n、systemd LimitNOFILE 是否足夠?max_user_connections 單用戶限制?