Linux – Getting MongoDB to use max open file descriptors in /etc/security/limits.conf

linuxmax-file-descriptorsmongodb

I've got a MongoDB server that runs out of file descriptors for open connections. I changed the settings in /etc/security/limits.conf so that every user gets a hard and soft limit of 65000 of open files and added session require pam_limits.so to /etc/pam.d/su, common-session, common-session-noninteractive, sshd. Server was restarted after changes made.

Afterwards, ulimit -a yields the desired result of having 65000 max file descriptors. Curiously though, while every process that gets started for any particular user uses the new file descriptor max of 65000, any MongoDB process that starts up (no matter the user) still only gets 1024 max file descriptors. I even tried opening a text file as the mongodb user, and that uses the 65000 limit.

System:

  • Ubuntu Maverick
  • MongoDB 1.6.5

I am using an upstart script to start the MongoDB instance, which is here:


    pre-start script
        PORT=27027
        mkdir -p /var/lib/mongodb${PORT}
        mkdir -p /var/log/mongodb
        limit nofile 65000 65000
        limit nproc 65000 65000
    end script

    start on runlevel [2345]
    stop on runlevel [06]

    script
      PORT=27027
      ENABLE_MONGODB="yes"
      if [ -e /var/lib/mongodb${PORT}/mongod.lock ]; then rm /var/lib/mongodb${PORT}/mongod.lock; fi
      if [ ! -d /var/lib/mongodb${PORT} ]; then mkdir -p /var/lib/mongodb${PORT}; fi
      chown -R mongodb:mongodb /var/lib/mongodb${PORT}
      if [ "x$ENABLE_MONGODB" = "xyes" ]; then
            exec start-stop-daemon --start --quiet --chuid mongodb \
            --pidfile /var/lib/mongodb${PORT}/${PORT}.pid \
            --exec /usr/bin/mongod -- \
            --port ${PORT} \
            --dbpath /var/lib/mongodb${PORT} \
            --logpath /var/log/mongodb/mongodb${PORT}.log \
            --logappend  \
            --maxConns = 50000 \
            --replSet oascluster \
            --oplogSize 200 \
            --rest
      fi
    end script

    respawn

    respawn limit 20 30 

Am I missing a setting somewhere?

Best Answer

So it seems MongoDB 1.6.5 doesn't work with the limit nofile line. You need to put ulimit -n X (where X is the file descriptors you want) just after the script keyword.

    script
        ulimit -n 60000
        PORT=27027
...

I guess the method using the pre-start block is for later versions of MongoDB.

Also for note, MongoDB has a hard limit of 20,000 connections, so it doesn't matter if you set file descriptors higher than that.

Another thing learned: starting and stopping using upstart scripts is not the same as REstarting. Starting and stopping threw errors on the limit nofile command, whereas restarting did not.