Set max open files on system reboot
Problem
MongoDB 4.4 shows a warning message that the “rlimit current value is 1024, suggest 64000”. So I want to increase this limit and make sure this new limit being set on system reboot.
To view the current “rlimit” (max open files) value, we can use:
1 | $ ulimit -Sa |
You can see the “open files” value is 1024.
Solution
We can first kill the mongodb process, then use ulimit -n 64000 to increase this number, then start mongodb again. This new value is only applied to this session.
And if we want to auto increase this number on start mongodb on system reboot, we can do this:
1, Add start mongodb in /etc/rc.local:
1 | sudo -H -u mongodb bash -c "/home/mongodb/mongodb4.4/bin/mongod -f /etc/mongodb44.conf" |
This will start a shell and run the command under user “mongodb”.
2, Set value in file /etc/security/limits.conf:
1 | mongodb soft nofile 64000 |
This will set “max open files” only for the user “mongodb”
3, Open file /etc/pam.d/sudo, add line:
1 | session required pam_limits.so |
The full file looks like this:
1 | #%PAM-1.0 |
This will make sure these new limits being read when we run the sudo command in /etc/rc.local
Now just restart your server, the mongod will be running with the new limit. Also, if you switch to mongodb user and run ulimit -Sa, you will see the new “open files” value is now 64000.