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

MySQL ERROR 1135 (HY000) 線程建立失敗:CentOS 6 ulimit 限制深度排查實錄

     編輯:MySQL 資料庫相關知識

故障現象

測試環境應用無法連線 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

排查過程:層層剝洋蔥

第一層:檢查 /etc/security/limits.conf

#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。

第三層:發現 CentOS 6 的隱藏配置

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

解決方案

方案一:修改 90-nproc.conf(永久生效,需重啟)

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

方案二:寫入 /etc/rc.local(開機自動生效)

echo "ulimit -u 65535" >> /etc/rc.local

方案三:以 root 身分啟動 mysqld_safe(臨時繞過)

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 無法覆蓋。生產環境建議統一由配置管理工具託管此類參數,避免手動修改遺漏。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved