Mac(OS Catalina)下无法连接MongoDB服务器求助
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:
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.
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, thenCtrl+X. - Reload the config to apply changes:
source ~/.zshrc
Test if
brewworks now by runningbrew --version—you should see its version number pop up.- Open the config file:
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:
- Tap MongoDB's official Homebrew tap:
brew tap mongodb/brew - Install the 4.0 community edition (matches your shell version v4.0.16):
This will install bothbrew install mongodb-community@4.0mongod(the server) andmongo(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:
Start the MongoDB service with Homebrew:
brew services start mongodb-community@4.0Check if the service is running:
brew services listYou should see
mongodb-community@4.0marked asstarted.Verify the
mongodprocess is active:ps aux | grep mongodLook for a line starting with
mongodb—that means the server is up and running.Test the connection now:
mongoYou should see the MongoDB shell connect successfully to
127.0.0.1:27017without the "Connection refused" error.
Extra Troubleshooting If Issues Persist
- If
brew services startfails, try startingmongodmanually to see detailed errors:
Common issues here are missing data directories (checkmongod --config /usr/local/etc/mongod.confdbPathin 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 runningecho $PATH—it should include/usr/local/bin.
内容的提问来源于stack exchange,提问作者Daniel Melo

