Installing MongoDB on macOS Catalina
A tricky problem due to Catalina’s read-only root folder
Published on
Nov 23, 2019
Read time
2 min read
Introduction
Recently, I started a new developer job and switched from a Windows to a Mac. The switch was mostly very smooth, with one main problem: setting up MongoDB. The problem is related to the latest major macOS release, Catalina, and in this article, I’ll share my solution.
Setting up MongoDB to run on Catalina is more time-consuming than it should be, but hopefully, this article will save you from some of the headaches I went through!
The Problem
By default, MongoDB stores database information in the root folder, in data/db
. But the Catalina update provides read-only access to the root. Based on this suggestion from Dom Berk, my solution was to move data/db
to /System/Volumes/Data/data/db
.
You could, in theory, place data/db
wherever you like. But /System/Volumes/Data/
seems like a more secure option than somewhere like Documents
.
1. Disable SIP
To access /System/
, however, requires disabling macOS’s SIP (System Integrity Protection), which prevents modification to that directory. To disable SIP, you need to:
- Boot into recovery mode by restarting and holding
CMD + R
during the boot-up. - Open up the terminal (which can be found under ‘Utilities’ in the top menu).
- Inside the terminal, run
csrutil disable
. You can now reboot in normal mode.
2. Move Your /data/db Folder out of the Root
If you have data inside /data/db
in the root, you can move it by providing temporary write-access to the root.
Once SIP is disabled, open the terminal and type sudo mount -uw /
.
You can now move your folder out of the root and into /System/Volumes/Data/
. This read-write ability will only last for the current session.
3. (Re)Install MongoDB
Next, I wanted to do a clean install of MongoDB. I tried a handful of different options, but Brew was the most convenient. For those who have tried to follow older tutorials, mongodb
has been removed from homebrew-core
.
Now, you’ll need to use mongo-community
, which can be accessed as follows:
brew tap mongodb/brew brew install mongodb-community brew services start mongodb-community
4. Run MongoDB From the Terminal
If everything’s worked correctly, you should now be able to run mongod
and mongo
— and see something other than a command not found
error!
If we just run mongod
, we’ll get errors that tell us the database directory is read-only. That’s because, by default, the command assumes that our database folder is in the root.
So, when running mongod
, we need to specific --dbpath /System/Volumes/Data/data/db
. Because we’re using /System/
, we also need to use sudo
. The full command is:
sudo mongod --dbpath /System/Volumes/Data/data/db
5. Create a Terminal Alias
That’s a bit tiresome to type every time. So, if you’re mostly using the same database path, I’d recommend setting up an alias.
If you use Zsh, you can add the following to ~/.zshrc
:
alias mongod="sudo mongod --dbpath /System/Volumes/Data/data/db"
This means mongod
will work pretty much as you’d expect. Just be aware that, if you ever need to change the --dbpath
, you’ll need to disable this alias.
I hope that’s saved you some time and some frustration!
Credit is due to Dom Berk, whose solution I’m building on in this article.
Related articles
You might also enjoy...
How to Containerize a Rust Web Server with MongoDB
Using Docker to create a container for a Rust web server with MongoDB as the database
5 min read
How to build an API server with Rust
A step-by-step tutorial for building a scalable Rust HTTP server using Actix and MongoDB
16 min read
Using TypeScript with MongoDB
Combine the power of TypeScript and MongoDB to create a scalable, production-grade database
8 min read