MongoDB Logo

Setting Up a Local MongoDB Server on MacOS

Most guides that I have seen use the MongoDB Cloud instead of MongoDB locally installed for development and testing. This is just a short guide to get you up and running with a local version of MongoDB on your machine. I find this easier and faster than navigating MongoDB’s cloud interface.

Step one is to download the community version of the MongoDB server. You can do this by heading over to the download center and selecting server. Ensure you select the current release and hit download.

The application doesn’t come with a wizard or auto installer so we have a few things to do to get it setup. Unzip the downloaded .tgz file and move the unzipped folder to your home directory (~). Re-name the folder to mongodb. We use the home folder to avoid issues with read/write permissions. Inside your home folder, make a new directory named mongodb-data. The MongoDB server will require a directory to save the data to, and we again want to use a location in our home directory to avoid permission issues.

Now we are ready to fire up the server. In your terminal navigate to your home directory (cd ~). Use the pwd command to get an absolute path to your home directory. Copy it for use in the next command. Now we will start the server.

/Users/kody/mongodb/bin/mongod --dbpath=/Users/kody/mongodb-data

We add the –dbpath flag to tell MongoDB where to save our data. We are going to choose the folder we created earlier. Once you start the server, you can navigate to the mongodb-data folder and you will see a bunch of files and folders have been added. Near the end of the terminal output you should see “[initandlisten] waiting for connections on port 27017”.

Running this command every time you want to start the server is a bit tedious, so I recommend adding it as an alias to your .zshrc or .bashrc file. You can then just run your alias in the terminal to start your server.

alias startmongo='/Users/kody/mongodb/bin/mongod --dbpath=/Users/kody/mongodb-data';

GUI Tool for MongoDB

Robo 3T is a handy tool for you to check out as well. It will let you easily view data within your database (lightweight GUI). You can download it here. When connecting use the port listed in your terminal output (default is 27017) and localhost.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.