Install MongoDB on Mac OS X With Examples

Today we will install MongoDB on Mac OS X. MongoDB is one of the leading NoSQL database. MySql and Oracle are termed as “Relation Databases”, similarly we can say that MongoDB is a “Document Database”. We can store any valid JSON in MongoDB database. MongoDB is open source and written in C++ language.

MongoDB stores JSON documents in Binary format termed as BSON (Binary JSON). Some of the features of MongoDB are:

  1. Full indexing support for faster query processing.
  2. Master-Slave replication for making our system fault tolerant.
  3. Rich Document based query to get the stored data.
  4. Auto Sharding to achieve horizontal scaling.

MongoDB is also very popular NoSQL database because we have drivers for all the major programming languages such as Java, PHP, Perl, Python, Ruby, Scala, C, C++ etc.

Install MongoDB on Mac

This is the first tutorial in MongoDB series and we will learn how to install MongoDB on Mac OS X. For this tutorial, I am using following softwares:

  1. Max OS X 10.12.6
  2. MongoDB Server 3.4.7 that you can download from MongoDB Downloads Page

Follow below commands in order to install MongoDB and start it.

First two commands are to download the tar ball using curl command and then extract it with tar command. Then I am renaming the extracted directory to remember the path easily.

If we don’t provide data directory, MongoDB server tries to access it at “/data/db”, so I am overriding it with --dbpath option with mongod executable file.

mongod is the server program that we need to execute to start the MongoDB server and I am ending it with & so that it runs in background and doesn’t get terminated if I close the terminal.

MongoDB default port is 27017 but we can change it by providing command line option or through configuration file.

That’s it, our MongoDB server is up and running, now we can test it with MongoDB terminal client.


show dbs: Command to get the list of databases

use DBNAME: Command to create new DB or switch to an existing DB, note that until unless you do any save or find operation on new DB, it wont be created.{}): Command to save JSON document in current DB. MongoDB store documents in terms of collections. In this command, names is the collection.

db.names.find(): Command to list all the documents in the names collection.

show collections: Command to list all the collections in the current database.

You might notice that I am accessing mongodb executables through relative path starting with ./, you can add mongodb bin location to PATH variable to access mongodb executables directly.

Now you can access mongodb executables from anywhere without providing the directory location.

Running MongoDB as Mac OS X Service

To run MongoDB as service, we need to create PList file in /Library/LaunchDaemons directory.


You might notice that rather than providing options as command line argument, I am using config file to pass arguments to mongod. Some of the commonly used configurations in MongoDB configuration file can be:

Note that you need to create mongo.log file manually and it should have the correct permission. For example, in above configuration I am using “pankaj” user to run the process, so the log file should be created by same user. If you don’t provide any user information in the plist file, it will be started with root user.

Now when you will restart the Mac OS X, you will notice that MongoDB has already been started as service and you don’t need to start it manually.


MongoDB Official Website
Running MongoDB as Service on Mac OS X

By admin

Leave a Reply

%d bloggers like this: