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

Mac(OS Catalina)下无法连接MongoDB服务器求助

Fixing MongoDB Connection & Command Not Found Issues on macOS Catalina

Hey there, let's work through your MongoDB problems on Catalina one by one—we'll start with the missing tools and then get your server connected smoothly.

Step 1: Reinstall Homebrew (since brew is missing)

First up, your system doesn't recognize the brew command, which means either Homebrew isn't installed or its path isn't set in your shell's environment variables. Let's fix that:

  1. Install Homebrew by running this in your terminal:

    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    

    If Catalina blocks the installation due to security settings, head to System Preferences > Security & Privacy and click "Allow" next to the message about the developer tool.

  2. Add Homebrew to your shell's PATH (Catalina uses zsh by default, so we'll update ~/.zshrc):

    • Open the config file:
      nano ~/.zshrc
      
    • Add this line at the bottom:
      export PATH="/usr/local/bin:$PATH"
      
    • Save and exit: press Ctrl+O, hit Enter, then Ctrl+X.
    • Reload the config to apply changes:
      source ~/.zshrc
      

    Test if brew works now by running brew --version—you should see its version number pop up.

Step 2: Install MongoDB Community Edition (to fix mongod/mongo not found)

Since your shell can't find mongod or mongo, we'll install the matching version (4.0.x) using Homebrew to align with your existing shell version:

  1. Tap MongoDB's official Homebrew tap:
    brew tap mongodb/brew
    
  2. Install the 4.0 community edition (matches your shell version v4.0.16):
    brew install mongodb-community@4.0
    
    This will install both mongod (the server) and mongo (the shell) and add them to your PATH automatically.

Step 3: Start MongoDB Service & Fix Connection Refused Error

The "Connection refused" error happens because the MongoDB server isn't running. Let's start it and verify everything's working:

  1. Start the MongoDB service with Homebrew:

    brew services start mongodb-community@4.0
    
  2. Check if the service is running:

    brew services list
    

    You should see mongodb-community@4.0 marked as started.

  3. Verify the mongod process is active:

    ps aux | grep mongod
    

    Look for a line starting with mongodb—that means the server is up and running.

  4. Test the connection now:

    mongo
    

    You should see the MongoDB shell connect successfully to 127.0.0.1:27017 without the "Connection refused" error.

Extra Troubleshooting If Issues Persist

  • If brew services start fails, try starting mongod manually to see detailed errors:
    mongod --config /usr/local/etc/mongod.conf
    
    Common issues here are missing data directories (check dbPath in the config file—default is /usr/local/var/mongodb) or permission problems. Fix permissions with:
    sudo chown -R $(whoami) /usr/local/var/mongodb
    
  • If you still can't find mongo/mongod, double-check your PATH by running echo $PATH—it should include /usr/local/bin.

内容的提问来源于stack exchange,提问作者Daniel Melo

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 15:24:07