本地Mongo Schema构建失败:报错8:'24: Too many open files',ulimit无效
I’ve dealt with this exact file limit headache on older macOS versions before—this error pops up because macOS enforces open file restrictions across multiple layers, not just the quick ulimit tweak. Let’s walk through the proper fixes step by step:
1. Break Down the Root Cause
The 8:'24: Too many open files' error happens when your system or MongoDB hits the maximum allowed open file descriptors. On macOS, there are three critical layers of limits you need to adjust:
- Temporary shell session limits (
ulimit) - Persistent user-level limits (managed by
launchctl) - System-wide kernel limits
2. Quick Temporary Fix for the Current Session
If you just need to get your schema script running right now, use launchctl to adjust your user’s limit (this works better than plain ulimit on macOS):
launchctl limit maxfiles 65536 200000
Verify the change took effect:
launchctl limit maxfiles
You should see the soft limit set to 65536 and the hard limit to 200000. Try running your MongoDB script again.
3. Permanent User-Level Limit Fix
To make the change stick across reboots, create a launchctl property list:
- Create the required directory if it doesn’t exist:
sudo mkdir -p /Library/LaunchDaemons - Create a file named
limit.maxfiles.plistwith this content:<?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>200000</string> </array> <key>RunAtLoad</key> <true/> <key>ServiceIPC</key> <false/> </dict> </plist> - Set correct permissions for the file:
sudo chown root:wheel /Library/LaunchDaemons/limit.maxfiles.plist sudo chmod 644 /Library/LaunchDaemons/limit.maxfiles.plist - Load the plist and confirm the change:
sudo launchctl load -w /Library/LaunchDaemons/limit.maxfiles.plist launchctl limit maxfiles
4. Adjust MongoDB’s Internal File Limit
MongoDB might have its own internal limit that overrides system settings. Edit your mongod.conf file (usually at /usr/local/etc/mongod.conf if installed via Homebrew) and add this line under the processManagement section:
processManagement: maxOpenFiles: 65536
Restart MongoDB to apply the change:
brew services restart mongodb-community
5. Verify All Limits Are Correct
After rebooting your system, run these commands to confirm everything is set properly:
- Check system kernel limits:
sysctl kern.maxfiles kern.maxfilesperproc - Check user-level limits:
launchctl limit maxfiles - Check MongoDB’s current open files count:
lsof -p $(pgrep mongod) | wc -l
Why Your Previous Attempts Failed
- Running
ulimit -nwithoutsudofails because you’re hitting the system-enforced hard limit for your user. - Using
sudo ulimit -nonly changes the limit for the root session, not your regular user account where you’re running MongoDB. - Closing apps frees up a few file handles, but MongoDB can easily exceed macOS’s default low limit (usually 256 or 1024) during schema building with multiple operations.
内容的提问来源于stack exchange,提问作者thurmc

