Install and Run MongoDB on Mac
A simple step-by-step guide to get mongoDB on your Mac:
-including some problems I faced and the solutions that worked for me.
In a perfect world, these 7 steps should work for you. I envy you if they do. If they don’t, you’re in good company. I have included multiple sources and solutions that I found while experiencing hours worth of issues. I hope I save you multiple hours of frustration!
Step 1:
Install Homebrew
-paste the command below in your macOS terminal
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Step 2:
Install mongoDB
brew install mongodb
Step 3:
Create the db directory***
mkdir -p /data/db
Step 4:
Give permissions for /data/db directory
sudo chown -R `id -un` /data/db
# (enter password)
- ** This command will not work if you are running macOS Catalina or newer, see problems/solution section for fix.
Step 5:
Run the Mongo daemon
-this will start the mongo server
mongod
Step 6:
Run the mongo shell
-while the Mongo daemon (mongod) is running, open another terminal and run
mongo
Step 7:
When finished, make sure to exit Mongo
quit() or exit
and stop the Mongo daemon
*ctrl-c*
It is important to close out of the Mongo shell anytime you aren’t actively using it. This is because if mongoDB does not shutdown correctly (power outage, accidental unplug, etc.) it could lead to data corruption.
Problems and their Solutions
maybe
Next up is a look at each step and the errors I faced on each.
Step 1 issue(s):
I did not face any errors when installing Homebrew. This was certainly the easiest part for me, personally.
Here is a link to the Homebrew Issues Documenation
Step 2 issue(s):
I received a few errors that went something like:
“error no formulae found in taps”.
Once I found the right Stack Overflow question the solution was discovered.
Each line is a different command*
brew tap mongodb/brewbrew install mongodb-communitybrew services start mongodb-community
Step 3/4 issue(s):
macOS Catalina and newer: the root folder is no longer writable.
This is the cause of the “read-only file system” error.
sudo mkdir -p /System/Volumes/Data/data/db
give this permissions:
sudo chown -R user_id:user_name /System/Volumes/Data/data/dbexample below. the user_id = blankenstein.dev and the user_name = admin because that is my user status on this computer.
Step 5 issue(s):
Running the mongoDB daemon
sudo mongod --dbpath /System/Volumes/Data/data/db
Step 6 issue(s):
Running the mongoDB shell
If running
mongo
give you an error
admin@Admins-MacBook-7 ~ % mongoMongoDB shell version v4.4.3connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodbError: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :connect@src/mongo/shell/mongo.js:374:17@(connect):2:6exception: connect failedexiting with code 1admin@Admins-MacBook-7 ~ %
Double check to make sure that you are running the daemon.
mongod must be running before you try to run the shell.
Other issue(s):
A possible quick fix to the daemon throwing a
“fassert() failure” error could be to :
turn it off and turn it back on again
brew services stop mongodb-communitythenbrew services start mongodb-community
Live troubleshooting document:
I plan on keeping this document updated as I run into problems or if I am ever asked to help solve a problem that isn’t already on this list.