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

MySQL 最大連接數調大指南:兩種修改 max_connections 的方法

     編輯:MySQL 資料庫相關知識

解決「Too many connections」錯誤:調整 MySQL 最大連接數

MySQL 預設 max_connections=100,高併發場景遠遠不足。提供兩種修改方法:

方法一:修改啟動腳本 safe_mysqld (推薦,無需重編譯)

  1. 編輯 safe_mysqld,找到 mysqld 啟動的兩行命令。
  2. 在參數後加入 -O max_connections=1000 (或更大值)。
  3. 範例修改:
# 原始
nohup $ledir/mysqld --basedir=... --datadir=... --skip-locking >> $err_log 2>&1
# 修改後
nohup $ledir/mysqld --basedir=... --datadir=... --skip-locking -O max_connections=1000 >> $err_log 2>&1

# 帶參數啟動的情況
nohup $ledir/mysqld --basedir=... --datadir=... --skip-locking "$@" -O max_connections=1000 >> $err_log 2>&1
  1. 重啟 MySQL,執行 mysqladmin -u root -p variables 驗證:
| max_connections | 1000 |

方法二:修改原始碼重新編譯 (永久生效)

  1. 解開原始碼,進入 sql 目錄編輯 mysqld.cc。
  2. 找到:{ "max_connections", (long*) &max_connections, 100, 1, 16384, 0, 1 },
  3. 將預設值 100 改為 1000 (或所需值)。
  4. 執行 ./configure; make; make install 重新編譯安裝。

注意事項

  • 最大值受限於作業系統檔案描述符限制 (ulimit -n)。
  • 過大連接數會消耗大量記憶體,請依硬體資源評估。
  • 方法一較靈活,升級 MySQL 版本時不需重複修改原始碼。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved