MySQL 預設 max_connections = 151(5.7 前為 100),中高流量網站極易觸發 Too many connections 錯誤。
[mysqld]
max_connections = 1000
# 配套調大開啟檔案數
open_files_limit = 65535
路徑:Linux /etc/my.cnf 或 /etc/mysql/my.cnf;Windows my.ini。修改後 systemctl restart mysqld。
mysql -u root -p
SET GLOBAL max_connections = 2000;
SHOW VARIABLES LIKE 'max_connections';
適用: 緊急臨時擴容,配合方法一雙軌操作。
編輯 mysqld_safe 啟動腳本,在 mysqld 呼叫行後附加:
-O max_connections=1500
或修改原始碼 sql/mysqld.cc 重新編譯(極不推薦,升級困難)。
| 參數 | 建議值 | 說明 |
|---|---|---|
| open_files_limit | 65535 | OS 層面檔案描述符上限,需 ulimit -n 配合 |
| table_open_cache | 4096+ | 減少開關表開銷 |
| thread_cache_size | 100+ | 緩存執行緒,降低建立銷毀成本 |
| back_log | 200+ | TCP 連線佇列長度 |
SHOW STATUS LIKE 'Max_used_connections'; -- 歷史峰值
SHOW STATUS LIKE 'Threads_connected'; -- 當前連線
SHOW GLOBAL VARIABLES LIKE 'max_connections';
盲目調大至 10000+ 會耗盡記憶體(每連線 ~256KB+),導致 OOM Killer 殺掉 mysqld。建議依 Max_used_connections * 1.5 估算,配合 Performance Schema 監控實際用量。