Through this tutorial we will see how to install MongoDB on a Mac operating system using Homebrew with just a few simple steps and make sure it’s up and running successfully.
MongoDB is an opensource general purpose NoSQL document-based program. It is different from the traditional databases and is built to be used for the advanced apps and cloud. As compared to traditional database documentation it uses JSON-like documentation very useful dealing with larger sets of distributed data with optional schemes.
We can use this MongoDB program to store or retrieve and manage large data sets as NoSQL is very effective when working with large bulks of distributed data.
Ways to Install MongoDB on Mac
Here is how to Install MongoDB on Mac using Homebrew. Please note we can also install it using manual files but for this tutorial we will use Homebrew.
In this tutorial we will use it as an on-premise installation but we can also use it as a cloud deployment. So generally, when anyone installs MongoDB as an on-premise, it can be installed as the free Community Server or as an Enterprise Server.
Why Use Homebrew to Install MongoDB on Mac?
When we install MongoDB or any other program of this kind, we are also required to install the dependencies along with so is the case here. That’s why we are using Homebrew it sill automatically install the required dependencies with a mongod.conf example file to begin with. Besides, the future upgrades and maintenance in general are also simplified.
In order to install MangoDB using Homebrew we need to install it first on our Mac. Before doing anything else install Homebrew on the system or use
brew help command in your Terminal window to check if it’s already installed or not.
If it’s already installed on your Mac it will return information about it, otherwise install it. This is the best way to check if it’s installed.
For installing Homebrew use brew.sh and follow the commands as described on its homepage. So please not this, for installing Homebrew you need to understand it also requires to install its dependency Xcode first on the Mac only then you can further move with installing Homebrew. We have already installed Homebrew so we are going to install MongoDB now.
Let’s move on to install MongoDB using Homebrew by running this command in the Terminal window:
brew tap mongodb/brew
It will download its formulae for MongoDB and its tools to manage databases.
Now run the following command, it will check the system compatibility of your Mac. If everything is good the brew tap(mongodb/brew) will be listed
brew tap | grep mongodb
Otherwise check the system requirements mentioned for installing it.
Then run this command
brew install [email protected]
That’s the part that actually installs MongoDB. In this case we’re installing version 4.4. Feel free to change that part to match the actual version that you’re installing.
This one is very important because it installs MongoDB. However, if there’s a new release we can modify the code. Multiple versions can be run beside it.
Run MongoDB Now
Now that MongoDB is installed, you will need to start it up.
Once it installs the MongoDB we need to start it. So when we run MongoDB we need to run the mongod process the primary daemon for MongoDB.
We have mentioned the ways to run MongoDB below (i.e. the mongodb process). There are two ways basically to do that:
Option 1: Run it as a MacOS service
Option 2: Run it manually as a background process
Run the following command to run it as a MacOS service
brew services start [email protected]
However if you wan to stop it just type the following command:
brew services stop [email protected]
We can also run it as a background process manually. To do that type the following command in the terminal:
mongod --config /usr/local/etc/mongod.conf --fork
If you have to stop it as a background process use mongo shell to connect to the mongod and type the shutdown command.
Verifying that MongoDB is Running on Mac
We can always verify whether or not MongoDB is running. To do that we can use one of these commands depending how you have started it:
If you started MongoDB as a MacOS service type this command
brew services list
You should see the service mongodb-community listed as started.
If it’s running as background process use this command:
ps aux | grep -v grep | grep mongod
It will show the mongod process as an output.
Connecting The MongoDB
Since we have already set up MongoDB on Mac we need to connect it to start it:
Open a new Terminal window and type the following command:
mongoIt will connect a mongo shell to our running MongoDB instance:
So it will show some text and at the end of that you will see this prompt
If you see this, it ensures that we are all good and it’s ready to accept our our related commands.
Type Your First Command
As we have installed MongoDB on a Mac using Homebrew and connected it successfully, we need to enter our very first command like the following one to display a list of databases for instance:
It will show return the results as this and it should return the databases as as this:
admin 0.000GB config 0.000GB local 0.000GB
So this is how we can install MongoDB on our Mac. It’s always advised to use Homebrew for that once again as it does not miss any files and is a very easy process get through. Let us know in the comments for any difficulties or errors you have faced.