Skip to content

Instantly share code, notes, and snippets.

@tamitutor
Last active August 21, 2024 17:50
Show Gist options
  • Save tamitutor/6a1e41eec0ce021a9718 to your computer and use it in GitHub Desktop.
Save tamitutor/6a1e41eec0ce021a9718 to your computer and use it in GitHub Desktop.
Fix Mongodb "soft rlimits" Warning On Mac OS X (Yosemite)

If you are seeing Mongo soft rlimits warnings in your logs, or a WARNING: soft rlimits too low. Number of files is 256, should be at least 1000 when you login to mongo shell via mongo from the commandline, or any mysterious/unexplained mongo connection errors... follow this how-to exactly and it will resolve the issue for you.

(Source of this how to found at basho/basho_docs#1402)

First file: sudo vi /Library/LaunchDaemons/limit.maxfiles.plist

...containing:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>limit.maxfiles</string>
<key>ProgramArguments</key>
<array>
<string>launchctl</string>
<string>limit</string>
<string>maxfiles</string>
<string>65536</string>
<string>65536</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>ServiceIPC</key>
<false/>
</dict>
</plist>

Second file: sudo vi /Library/LaunchDaemons/limit.maxproc.plist

...containing:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>limit.maxproc</string>
<key>ProgramArguments</key>
<array>
<string>launchctl</string>
<string>limit</string>
<string>maxproc</string>
<string>2048</string>
<string>2048</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>ServiceIPC</key>
<false/>
</dict>
</plist>

Please confirm that both these files are owned by root:wheel and have permissions -rw-r--r--. This should be the default but it's wise to check. The chmod command to do that is sudo chmod 644 <filename>.

Doing the above will cause system limits to set themselves correctly upon restart. This can be confirmed by typing launchctl limit into a terminal.

Once system limits are properly set, it remains necessary to set session limits. This is best done by editing bashrc: sudo vi /etc/bashrc

You need to add the following lines to the end of the file:

ulimit -n 65536
ulimit -u 2048

As with the other files above, make sure the bashrc file has -rw-r--r-- permissions.

After all this is done, restart the computer and type ulimit -n into a terminal. You should see confirmed that the maxfiles is 65536. You will no longer see soft rlimits warnings when you login into mongo shell with mongo from the command line!

The above method is suitable for computers that are happy to keep a high open files limit in the long run. However, if you wish to change the limit only for the current session, you may skip modifications to files and simply enter the following into the terminal:

sudo launchctl limit maxfiles 65536 65536
sudo launchctl limit maxproc 2048 2048
ulimit -n 65536
ulimit -u 2048

These settings will expire; the ulimit settings upon relaunch of the terminal and the launchctl settings upon restart of the computer.

@lucasmartins
Copy link

I'm getting this error even after all the steps:

$ ulimit -u 2048
ulimit: value exceeds hard limit

@wufangjian
Copy link

Thanks!

@pmatsinopoulos
Copy link

Thank you. It worked for me very well.

@kenmasters
Copy link

Thank you. It works.

@arrmixer
Copy link

arrmixer commented Jul 2, 2017

Thank you Tami. Great tutorial!

@dgpuranik
Copy link

I tried last 4 commands to test and I didnt want to make permanent change. But didnt work for me.

@korsmakolnikov
Copy link

korsmakolnikov commented Oct 22, 2017

Unfortunately this didn't work for me. Warning stays on osx El Capitan

@stonematt
Copy link

Is the process for High Sierra (10.13) still the same?

@pzrq
Copy link

pzrq commented Apr 24, 2020

For Catalina (10.15.4), I've added the LaunchDaemons changes which seemed to work.

NB: sudo reboot (or similar) is required, otherwise I see ulimit:111: setrlimit failed: invalid argument

If you don't want the reboot, I initially went with some lower values in vi ~/.zshrc:

ulimit -n 4096
ulimit -u 2048

Then source ~/.zshrc to have it take effect immediately, as reported by ulimit -a.

After reboot, I was able to raise the values without the setrlimit error:

ulimit -n 65536
ulimit -u 2048

@stella-lu
Copy link

Thank you! Worked for me on MacOS Catalina version 10.15.7

@0c3d7r4
Copy link

0c3d7r4 commented Nov 8, 2023

hey thanks that's the life-saver, my local mongo db on mac started crashing with message

{"t":{"$date":"2023-11-08T12:10:05.068+03:00"},"s":"W", "c":"CONTROL", "id":22184, "ctx":"initandlisten","msg":"Soft rlimits too low","attr":{"currentValue":256,"recommendedMinimum":64000},"tags":["startupWarnings"]}

so Soft rlimits too low was the problem even though in the brew's plist there was the setting of NumberOfFiles set to 64000 which wasn't helping but this helped and i didn't have to set ulimit afterward either due to using brew

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment