How to install MongoDB on Mac OS

In this article, we see how to install MongoDB on Mac OS. We are going to install using .tgz tarball.

Install MongoDB on Mac OS:

1. Open browser then go to https://www.mongodb.com/download-center/enterprise.

2. Then, select version and operating system as shown below.

How to install MongoDB on Mac OS

3. Provide your information and click on Download.

4. Then, go to Downloads direct and extract the files from the download archive.

tar -zxvf mongodb-macos-x86_64-enterprise-4.2.1.tgz

5. Copy extracted bin directory fiels to /usr/local/bin/ as shown below.

sudo cp /usr/r2schools/.../bin/* /usr/local/bin/

Run the MongoDB:

1. Create the data directory.

Before we start MongoDB for the first time, we must create the directory to which the mongod process will write data.

sudo mkdir -p /usr/local/var/mongodb

2. Create the log directory.

We must also create the directory in which the mongod process will write its log file:

sudo mkdir -p /usr/local/var/log/mongodb

3. Set permissions for the data and log directories.

sudo chown r2schools /usr/local/var/mongodb sudo chown r2schools /usr/local/var/log/mongodb

Where r2schools is the user name.

4. Run the MongoDB.

To run MongoDB, run the mongod process at the system prompt, providing the two parameters dbpath and logpath from above, and the fork parameter to run mongod in the background.

mongod --dbpath /usr/local/var/mongodb --logpath /usr/local/var/log/mongodb/mongo.log --fork

5. Verify MongoDB server started successfully or not.

ps aux | grep -v grep | grep mongod

6. Connect to MongoDB server. From the console type “mongo”

mongo