ERROR 1040 (00000): Too many connections
預設 max_connections = 151(舊版 100),高併發場景極易觸頂。
mysql> SET GLOBAL max_connections = 5000;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT @@GLOBAL.max_connections;
+--------------------------+
| @@GLOBAL.max_connections |
+--------------------------+
| 5000 |
+--------------------------+
注意: 語法為SET GLOBAL max_connections = N或SET @@GLOBAL.max_connections = N,兩者等價。本文原文嘗試的set @@global.max_connections=5000;同樣正確。
編輯 /etc/my.cnf(Linux)或 my.ini(Windows),在 [mysqld] 區段加入:
[mysqld]
max_connections = 2000 # 建議 1000~5000 視記憶體而定
重啟服務:
systemctl restart mysqld # 或 service mysqld restart
| 參數 | 建議值 | 說明 |
|---|---|---|
thread_cache_size | min(100, max_connections/10) | 緩存執行緒,減少建立/銷毀開銷 |
open_files_limit | max_connections * 5 | OS 層面檔案描述符上限,需同步 ulimit -n |
table_open_cache | max_connections * 2 | 表快取,避免頻繁開關表 |
innodb_buffer_pool_size | 物理記憶體 60~70% | InnoDB 核心快取,防止磁碟 I/O 成為瓶頸 |