首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫

MySQL 連接數優化:監控 max_connections 與 Max_used_connections 設定合理上限

     編輯:MySQL 資料庫相關知識

現象

ERROR 1040 (HY000): Too many connections

兩大成因

  1. 訪問量真高 → 架構擴容 (讀寫分離、分庫分表、連接池調優)
  2. max_connections 設定過小 → 參數調優

關鍵指標查看

SHOW VARIABLES LIKE 'max_connections';        -- 服務器允許上限
SHOW GLOBAL STATUS LIKE 'Max_used_connections'; -- 歷史峰值併發

健康度判斷公式

使用率 = Max_used_connections / max_connections * 100%
理想區間:10% ~ 85%
< 10%  →  上限設過大,浪費內存
> 85%  →  風險區,需擴容或調優

實測案例

環境max_connectionsMax_used_connections使用率建議
本地測試15121.3%過大,可降至 50
Linode VPS1514429%健康,保持或微調

修改方法

動態生效 (重啟失效)

SET GLOBAL max_connections = 256;

持久化 (my.cnf)

[mysqld]
max_connections = 500
# 配套參數
wait_timeout = 60
thread_cache_size = 100
open_files_limit = 65535

修改後 systemctl restart mysqld。

關聯參數說明

  • wait_timeout:空閒連接回收時間,降低佔用
  • thread_cache_size:緩存線程,減少創建銷毀開銷
  • open_files_limit:配合大連接數調大文件描述符上限
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved