![/etc/security/limits.conf 软 nproc 限制似乎被忽略](https://linux22.com/image/804583/%2Fetc%2Fsecurity%2Flimits.conf%20%E8%BD%AF%20nproc%20%E9%99%90%E5%88%B6%E4%BC%BC%E4%B9%8E%E8%A2%AB%E5%BF%BD%E7%95%A5.png)
我的 MongoDB 数据库在负载下遇到问题,日志中出现以下错误:
[initandlisten] pthread_create failed: errno:11 Resource temporarily unavailable
[initandlisten] can't create new thread, closing connection
我得出的结论是,我需要提高“ulimit -u”或“最大进程数”设置(当前为 1024),并且考虑到启动的 Web 前端,使用量可能已经超过该值(不确定如何检查这一点)。
我编辑了 /etc/security/limits.conf 以添加最后两行(前两行已经存在):
* soft nofile 350000
* hard nofile 350000
* soft nproc 30000
* hard nproc 30000
然后我重新启动了系统(顺便问一下,我应该这样做吗,还是重新启动 mongod 服务就足够了?)
重启后,检查 mongod 进程的进程限制,似乎软限制已被忽略:
$ cat /proc/2207/limits
Limit Soft Limit Hard Limit Units
Max cpu time unlimited unlimited seconds
Max file size unlimited unlimited bytes
Max data size unlimited unlimited bytes
Max stack size 8388608 unlimited bytes
Max core file size 0 unlimited bytes
Max resident set unlimited unlimited bytes
Max processes 1024 30000 processes
Max open files 350000 350000 files
Max locked memory 65536 65536 bytes
Max address space unlimited unlimited bytes
Max file locks unlimited unlimited locks
Max pending signals 273757 273757 signals
Max msgqueue size 819200 819200 bytes
Max nice priority 0 0
Max realtime priority 0 0
Max realtime timeout unlimited unlimited us
$ whoami
mongod
$ ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 273757
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 350000
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 1024
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
我预计,根据 /etc/security/limits.conf 文件,“最大进程数”的硬限制和软限制都将达到 30000,但只有硬限制是这样的。
我究竟做错了什么?
我在 AWS EC2 上运行 Amazon Linux。
bash-4.1$ cat /etc/*-release
Amazon Linux AMI release 2012.09
答案1
检查文件 /etc/security/limits.d/90-nproc.conf,因为这可能会覆盖你的设置。我去年写过关于这个问题的文章http://scott.cm/max-processes-1024-limits-conf/
答案2
您可以尝试在启动 mongodb 数据库的脚本开头更改 ulimit 值。
子进程从调用进程继承资源限制。