You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

本地Mongo Schema构建失败:报错8:'24: Too many open files',ulimit无效

Fix "Too many open files" Error When Running MongoDB Schema Script on macOS High Sierra 10.13.3

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:

  1. Create the required directory if it doesn’t exist:
    sudo mkdir -p /Library/LaunchDaemons
    
  2. Create a file named limit.maxfiles.plist with 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>
    
  3. Set correct permissions for the file:
    sudo chown root:wheel /Library/LaunchDaemons/limit.maxfiles.plist
    sudo chmod 644 /Library/LaunchDaemons/limit.maxfiles.plist
    
  4. 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 -n without sudo fails because you’re hitting the system-enforced hard limit for your user.
  • Using sudo ulimit -n only 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 09:08:47