MySQL 預設的最大連接數為 100,當應用程式併發較高時容易出現 "Too many connections" 錯誤。本文詳細說明三種修改最大連接數的方法,涵蓋 Windows 與 Linux 環境,並包含驗證步驟與注意事項。
登入 MySQL 後執行以下指令:
SHOW VARIABLES LIKE '%max_connections%';
SET GLOBAL max_connections = 1000;
SHOW VARIABLES LIKE '%max_connections%';
注意:此方式不需重啟服務,但 MySQL 重啟後會恢復預設值。MySQL 允許的最大值為 16384。
my.ini[mysqld] 區段max_connections = 1000/etc/my.cnf 或 /etc/mysql/my.cnf[mysqld] 下設定 max_connections = 1000systemctl restart mysqld 重啟服務適用於 CentOS 4.4 + MySQL 5.0.33 等舊版手動編譯安裝:
vi /usr/local/mysql/bin/mysqld_safe$NOHUP_NICENESS $ledir/$MYSQLD 那兩行啟動命令-O max_connections=1500service mysqld restart/usr/local/mysql/bin/mysqladmin -uroot -p variableswait_timeout、interactive_timeout 避免閒置連接佔用資源Threads_connected 與 Max_used_connections 指標評估適當上限