my.cnf / my.ini(永久生效,需重啟)[mysqld]
max_connections = 1000 # 預設 151 (5.7+) 或 100 (舊版)
重啟服務:`systemctl restart mysqld` / `service mysql restart`
mysql> SET GLOBAL max_connections = 2000;
-- 查看當前值
mysql> SHOW VARIABLES LIKE 'max_connections';
# 編輯 /usr/local/mysql/bin/mysqld_safe
# 找到 mysqld 啟動行,在參數後加入
-O max_connections=1500
# 例:
$NOHUP_NICENESS $ledir/$MYSQLD $defaults ... -O max_connections=1500 >> $err_log 2>&1
重啟驗證:`mysqladmin -u root -p variables | grep max_connections`
# sql/mysqld.cc
{"max_connections", OPT_MAX_CONNECTIONS,
"The number of simultaneous clients allowed.",
(gptr*) &max_connections, (gptr*) &max_connections,
0, GET_ULONG, REQUIRED_ARG, 1500, 1, 16384, 0, 1, 0},
再執行 `./configure && make && make install`。升級極其麻煩,強烈不建議。
| 參數 | 建議值 | 說明 |
|---|---|---|
open_files_limit | max_connections * 5 以上 | 每連線需多個檔案描述符 |
thread_cache_size | 參考連線數表 | 減少執行緒建立開銷 |
innodb_buffer_pool_size | 實體記憶體 60~80% | 核心效能參數 |
back_log | max_connections / 5 | TCP 連線佇列長度 |
mysqladmin -u root -p variables | grep max_connections
# 或
mysql> SHOW STATUS LIKE 'Max_used_connections'; -- 歷史峰值