Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
68 lines (48 sloc) 3.66 KB

+++ title = "How to setup MongoDB on MacOS" description = "A installation guide for a MongoDB on MacOS setup for having a NoSQL database available on your OS. It comes with everything that's needed to connect a Node.js application to your database in JavaScript ..." date = "2019-01-25T13:50:46+02:00" tags = ["Node", "JavaScript"] categories = ["Node", "JavaScript"] keywords = ["mongodb macos", "mongodb setup", "mongodb os setup"] news_keywords = ["mongodb macos", "mongodb setup", "mongodb os setup"] hashtag = "#NodeJs" card = "img/posts/mongodb-macos-setup/banner_640.jpg" banner = "img/posts/mongodb-macos-setup/banner.jpg" contribute = "" headline = "How to setup MongoDB on MacOS"

summary = "The article is a checklist for myself, but for anyone else setting up MongoDB on MacOS." +++

{{% sponsorship %}}

{{% pin_it_image "mongodb macos" "img/posts/mongodb-macos-setup/banner.jpg" "is-src-set" %}}

This is a tutorial for setting up MongoDB on MacOS. You might be wondering why there's a need for another setup guide for MongoDB in the first place, since there are a few across the web. I found many tutorials and guides on how to set it up, but found most of them to be unclear and outdated, based on old MongoDB versions. The checklist is not comprehensive, though, as it is used for several tutorials on my website, I keep it updated with the recent versions of MongoDB. If you spot any flaws in this guide, a comment below would be very helpful to keep it up to date for other developers.

If you want to run MongoDB on Windows instead, you will find guidance over here: How to setup MongoDB on Windows.

{{% chapter_header "MongoDB Installation on MacOS" "mongodb-macos-installation" %}}

I recommend {{% a_blank "Homebrew" "" %}} for installing and managing applications on MacOS. It is installed using the following command in the MacOS terminal. Open up the terminal and paste the command:

{{< highlight javascript >}} /usr/bin/ruby -e "$(curl -fsSL" {{< /highlight >}}

The terminal runs through a series of installation operations, and will probably create folders in your local machine to accommodate Homebrews storage requirements. You can find more detailed instructions here. After it's installed, update the Homebrew dependencies and install MongoDB on the command line:

{{< highlight javascript >}} brew update brew install mongodb {{< /highlight >}}

It will take a few seconds. Once it's done, create a directory so MongoDB can store its data.

{{< highlight javascript >}} sudo mkdir -p /data/db {{< /highlight >}}

Now to make sure this data directory have the right permissions, you'll run this command:

{{< highlight javascript >}} sudo chown -R id -un /data/db {{< /highlight >}}

Now our data directory is ready with right permissions. Next run mongo daemon. Which is a service which runs in the background and listens for connections on a given port. Run this command:

{{< highlight javascript >}} mongod {{< /highlight >}}

Now mongo daemon will be running in the background and can be used by your applications. Next, check your MongoDB version:

{{< highlight javascript >}} mongo --version MongoDB shell version: 4.0.5 {{< /highlight >}}

The command line results will show the version you have installed on your local machine. I recommend using the latest version of libraries and software whenever possible to avoid compatibility issues with client-side applications.

{{% read_more "MongoDB with Mongoose in Express Tutorial" "" %}}