Error: Can't create a new thread (errno 12)
Errno: 1135
SQLSTATE: HY000
errno 12 = ENOMEM(Out of memory),系統無法為新線程分配內存/棧空間。
# CentOS/RHEL
yum update glibc -y
# Ubuntu/Debian
apt-get update && apt-get install -y libc6
舊版 glibc 存在線程創建內存洩漏/碎片化 Bug,升級可顯著改善高併發下線程創建穩定性。
[mysqld]
## 降低線程棧與緩存壓力
thread_stack = 192K # 默認 256K,降低減少單線程內存
thread_cache_size = 200 # 復用線程,減少創建/銷毀開銷
## 縮短空閒連接存活,釋放線程資源
wait_timeout = 100 # 非交互式連接 100 秒無操作斷開
interactive_timeout = 100 # 交互式連接同理
## InnoDB 線程調度優化(5.5+)
innodb_thread_sleep_delay = 40 # 微秒,高併發下減少自旋等待 CPU 消耗
innodb_thread_concurrency = 0 # 0=自動,避免人為限制並發
## 文件描述符上限(配合 OS ulimit)
open_files_limit = 65535
/etc/security/limits.conf
mysql soft nofile 65535
mysql hard nofile 65535
mysql soft nproc 65535
mysql hard nproc 65535
# 生效需重新登錄或重啟服務
ulimit -n 65535
ulimit -u 65535
# 內核參數 /etc/sysctl.conf
vm.max_map_count = 262144
fs.file-max = 1000000
kernel.pid_max = 4194303
# sysctl -p 生效
SHOW GLOBAL STATUS LIKE 'Threads_connected';SHOW GLOBAL STATUS LIKE 'Max_used_connections';SHOW VARIABLES LIKE 'max_connections'; 若峰值接近上限,必須調大或優化連接池。dmesg | grep -i memory、grep -i 'out of memory' /var/log/messages 確認 OOM Killer 是否介入。ps -eo pid,user,rss,cmd | grep mysqld 確認 mysqld 進程 RSS 是否佔用過多物理內存。Threads_connected / max_connections > 80% 觸發告警,自動擴容或限流。