SQLSTATE[HY000] [1135] Can't create a new thread (errno 12);
if you are not out of available memory, you can consult the manual for a possible OS-dependent bug
作業系統無法建立新執行緒,常見原因:
max_connections 或系統 ulimit -u 限制。wait_timeout、interactive_timeout 過大(預設 8 小時),閒置連線堆疊耗盡記憶體/執行緒配額。innodb_buffer_pool_size + max_connections * thread_stack 超過實體記憶體。/proc/sys/kernel/threads-max、/etc/security/limits.conf nproc 過小。SET GLOBAL innodb_thread_sleep_delay = 40; -- 降低閒置執行緒 CPU 爭用
SET GLOBAL wait_timeout = 100;
SET GLOBAL interactive_timeout = 100;
[mysqld]
innodb_thread_sleep_delay = 40
wait_timeout = 100
interactive_timeout = 100
# 同步調整 OS 限制
# ulimit -u 65535
# echo 65535 > /proc/sys/kernel/threads-max
mysql> SHOW VARIABLES LIKE '%timeout%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| connect_timeout | 10 |
| wait_timeout | 100 |
| interactive_timeout | 100 |
| innodb_thread_sleep_delay| 40 |
+--------------------------+-------+
注意:僅設 wait_timeout 不生效,必須同時設定 interactive_timeout,因客戶端連線類型決定實際套用哪個變數。
# 檢查目前連線數與睡眠連線
mysql> SHOW PROCESSLIST;
mysql> SELECT COUNT(*), COMMAND FROM information_schema.PROCESSLIST GROUP BY COMMAND;
# 檢查 OS 限制
ulimit -a
cat /proc/sys/kernel/threads-max
Threads_connected、Threads_running、Connection_errors_max_connections。FLUSH HOSTS 清理 DNS 快取導致的連線阻塞。