MySQL 錯誤 ERROR 1203 (42000): User already has more than 'max_user_connections' active connections 表示該使用者同時連線數超過限制。本文分析成因並提供即時與永久解決方案。
ERROR 1203 (42000): User 'app_user' already has more than 'max_user_connections' active connections
max_user_connections 設定。max_connections 過小:間接限制單用戶上限。-- 查看全域連線設定
SHOW VARIABLES LIKE 'max_connections';
SHOW VARIABLES LIKE 'max_user_connections';
-- 查看當前連線狀態
SHOW STATUS LIKE 'Threads_connected';
SHOW PROCESSLIST;
-- 依用戶統計連線數
SELECT USER, COUNT(*) AS conn_count FROM information_schema.PROCESSLIST GROUP BY USER ORDER BY conn_count DESC;
-- 將特定用戶上限調大(0 表示不限制,受限於 max_connections)
SET GLOBAL max_user_connections = 1000;
-- 或針對特定用戶(MySQL 5.7+)
ALTER USER 'app_user'@'%' MAX_USER_CONNECTIONS 1000;
驗證:SELECT @@max_user_connections;
[mysqld]
max_connections = 2000
max_user_connections = 0 # 0 = 無單用戶限制,受 max_connections 總上限管控
重啟生效:systemctl restart mysqld
maximumPoolSize。idleTimeout、maxLifetime 避免殭屍連線。Threads_connected、Max_used_connections。-- 找出睡眠超過 60 秒的連線
SELECT CONCAT('KILL ', id, ';') FROM information_schema.PROCESSLIST
WHERE COMMAND = 'Sleep' AND TIME > 60 AND USER = 'app_user';
-- 批次執行輸出的 KILL 指令
最佳實踐:將
max_user_connections設為 0(不限單用戶),由應用層連線池嚴格控制上限;max_connections設為預估峰值連線數 + 20% 緩衝。