測試環境應用無法連線 MySQL,錯誤日誌顯示:
ERROR 1135 (HY000): Can't create a new thread (errno 11); if you are not out of available memory, you can consult the manual for a possible OS-dependent bug
#for root root soft nofile 65535 root hard nofile 65535 # End of file mysql soft nproc 65536 mysql hard nproc 65536 mysql soft nofile 65535 mysql hard nofile 65535
配置看似正確,mysql 用戶的 nproc(最大進程數)與 nofile(最大開啟檔案數)均已放開。
sudo -u root bash -c "ulimit -a" # 輸出關鍵行: max user processes (-u) 1024 # 仍被限制在 1024!
雖然 limits.conf 設了 65535,但實際生效值仍為 1024。
cat /etc/security/limits.d/90-nproc.conf # 內容: * soft nproc 1024
關鍵發現: CentOS 6 引入 /etc/security/limits.d/ 目錄,90-nproc.conf 中的 * soft nproc 1024 針對所有非 root 用戶強制限制最大進程數為 1024,且優先級高於 limits.conf。
# 普通用戶嘗試提升失敗 [oracle@fztest ~]$ ulimit -u 65535 -bash: ulimit: max user processes: cannot modify limit: Operation not permitted # root 可臨時修改(僅當前會話) [root@fztest ~]# ulimit -u 65535 [root@fztest ~]# ulimit -u 65535
sed -i 's/1024/65535/' /etc/security/limits.d/90-nproc.conf # 驗證 sudo -u root bash -c "ulimit -a" | grep 'max user processes' # 輸出:max user processes (-u) 65535
echo "ulimit -u 65535" >> /etc/rc.local
ulimit -u 65535 mysqld_safe --user=mysql &
| 層級 | 配置檔 | 作用範圍 | 優先級 |
|---|---|---|---|
| 系統預設 | /etc/security/limits.d/90-nproc.conf | 所有非 root 用戶 | 最高 |
| 通用配置 | /etc/security/limits.conf | 指定用戶/群組 | 次之 |
| 會話臨時 | ulimit 指令 | 當前 Shell 及子進程 | 最低(受上層限制) |
避坑指南: CentOS/RHEL 6/7 系列務必檢查
/etc/security/limits.d/目錄下的*-nproc.conf,此為系統級硬性限制,limits.conf無法覆蓋。生產環境建議統一由配置管理工具託管此類參數,避免手動修改遺漏。