一切都源于mongoDB的这条warning

** WARNING: soft rlimits too low. rlimits set to 4096 processes, 64000 files. Number of processes should be at least 32000 : 0.5 times number of files.

修改ulimit本来也不难,不过还是有不少坑。mongoDB官方文档http://docs.mongodb.org/manual/reference/ulimit/#memory-size , 虽然给了方法,不过linux这个ulimit可是用户相关的,所以这种做法不靠谱。

正确的做法是修改/etc/security/limits.conf文件,这个文件是有man文档,可以man limit.conf文件查看。先给个sample:

<domain><type><item><value>
           *               soft    core            0
           *               hard    nofile          512
           @student        hard    nproc           20
           @faculty        soft    nproc           20
           @faculty        hard    nproc           50
           ftp             hard    nproc           0
           @student        -       maxlogins       4
           :123            hard    cpu             5000
           @500:           soft    cpu             10000
           600:700         hard    locks           10

根据说明,我们添加如下内容,这些数值来自于mongoDB文档里面的推荐数值。有个要求就是nproc要大于0.5nofile。

mongod soft fsize unlimited
mongod hard fsize unlimited
mongod soft cpu unlimited
mongod hard cpu unlimited
mongod soft as unlimited
mongod hard as unlimited
mongod soft nofile 64000
mongod hard nofile 64000
mongod soft nproc 64000
mongod hard nproc 64000

重启后就搞定了。这里要留意的是,请指明用户,不要用*代替。谁启动mongoDB就用户就填谁,可以用ps命令查询。

[yucanlin@localhost ~]$ ps -ef | grep mongod
mongod      945      1  1 12:11 ?        00:00:14 /usr/bin/mongod -f /etc/mongod.conf
yucanlin   3040   2649  0 12:32 pts/0    00:00:00 grep --color=auto mongod

另外,如果想查询mongod的limits,可以使用以下命令。其中945是pid。有上面的ps命令查询得到。

[yucanlin@localhost ~]$ cat /proc/945/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             64000                64000                processes 
Max open files            64000                64000                files     
Max locked memory         65536                65536                bytes     
Max address space         unlimited            unlimited            bytes     
Max file locks            unlimited            unlimited            locks     
Max pending signals       7859                 7859                 signals   
Max msgqueue size         819200               819200               bytes     
Max nice priority         0                    0                    
Max realtime priority     0                    0                    
Max realtime timeout      unlimited            unlimited            us 

 

By charlie

2 thoughts on “mongoDB 修改 ulimit”
    1. 官方建议的是,Number of processes should be at least 0.5 times number of files. processes至少是files的一半。如果不够,请把两个值同时调大。

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注