简体   繁体   中英

How to effectively increase max number of open files of mongodb on Debian?

My mongodb server often crashed, with error "Too many open files".

OS is debian 8:

Linux VM-64-58-debian 3.16.0-4-amd64 #1 SMP Debian 3.16.51-3 (2017-12-13) x86_64 GNU/Linux

and MongoDB Version 3.4.5.

I install mongodb with these steps below:

  1. Download .tgz package from offical site, and unpacked to folder.
  2. Create mongodb as system user.
  3. Create script file mongo.auto, and chmod +x it.

     /mongodb/bin/mongos --config /mongodb/config/dbs.config --fork 
  4. Create /etc/init.d/mongo.sh, and chmod +x it.

     #! /bin/sh ### BEGIN INIT INFO # Provides: # Required-Start: # Required-Stop: # Should-Start: # Should-Stop: # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 ### END INIT INFO ############### EDIT ME ################## # path to app # user RUN_AS=mongodb sudo -u mongodb /mongodb/bin/mongo.auto 
  5. Set to service for autorun:

     insserv mongo.sh 
  6. Done.

For the "Too many open files", I check /proc/pid/limits, return:

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             63596                63596                processes
Max open files            1024                 4096                 files
Max locked memory         65536                65536                bytes
Max address space         unlimited            unlimited            bytes
Max file locks            unlimited            unlimited            locks
Max pending signals       63596                63596                signals
Max msgqueue size         819200               819200               bytes
Max nice priority         0                    0
Max realtime priority     0                    0
Max realtime timeout      unlimited            unlimited            us

I need to increase "Max open files" 1024 to 65536 or unlimited, refer mongodb document

I tried four mathod below:

  1. Add ulimit to /etc/rc.local

     ulimit -SHn 65535 
  2. Add ulimit to /etc/profile

     ulimit -SHn 65535 
  3. Add setting to /etc/security/limits.conf

     mongodb soft nofile 65535 mongodb hard nofile 65535 
  4. Add setting to /etc/sysctl.conf

     fs.file-max=65535 

And I have checked /etc/pam.d/login,

session    required   pam_limits.so

the line is unmasked.

I reboot server after every try.

With these, the session/shell's "Max open files" updated to 65535, while /proc/pid/limits NEVER changed, still keeps 1024.

Any advise ?

After days study, I have found solution.

I use /etc/inid.d/mongo.sh as autorun script, and use

sudo -u mongodb /mongodb/bin/mongo.auto

as startup script. So I need set pam auth with sudo.

So the solution is - in /etc/pam.d, it need append below to /etc/pam.d/sudo

session    required   pam_limits.so

Then reboot, it DONE.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM