Install MongoDB on Mac OS X

Filed Under: MongoDB

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.


pankaj@Pankajs-MacBook-Pro:~/Downloads/$ curl -O http://fastdl.mongodb.org/osx/mongodb-linux-x86_64-3.4.7.tgz
pankaj:~ pankaj$ tar -xvf mongodb-linux-x86_64-3.4.7.tgz
x mongodb-linux-x86_64-3.4.7/README
x mongodb-linux-x86_64-3.4.7/THIRD-PARTY-NOTICES
x mongodb-linux-x86_64-3.4.7/MPL-2
x mongodb-linux-x86_64-3.4.7/GNU-AGPL-3.0
x mongodb-linux-x86_64-3.4.7/bin/mongodump
x mongodb-linux-x86_64-3.4.7/bin/mongorestore
x mongodb-linux-x86_64-3.4.7/bin/mongoexport
x mongodb-linux-x86_64-3.4.7/bin/mongoimport
x mongodb-linux-x86_64-3.4.7/bin/mongostat
x mongodb-linux-x86_64-3.4.7/bin/mongotop
x mongodb-linux-x86_64-3.4.7/bin/bsondump
x mongodb-linux-x86_64-3.4.7/bin/mongofiles
x mongodb-linux-x86_64-3.4.7/bin/mongooplog
x mongodb-linux-x86_64-3.4.7/bin/mongoreplay
x mongodb-linux-x86_64-3.4.7/bin/mongoperf
x mongodb-linux-x86_64-3.4.7/bin/mongod
x mongodb-linux-x86_64-3.4.7/bin/mongos
x mongodb-linux-x86_64-3.4.7/bin/mongo
pankaj@Pankajs-MacBook-Pro:~/Downloads/$ mv mongodb-linux-x86_64-3.4.7 mongodb
pankaj@Pankajs-MacBook-Pro:~/Downloads/$ cd mongodb
pankaj@Pankajs-MacBook-Pro:~/Downloads/mongodb$ mkdir data
pankaj@Pankajs-MacBook-Pro:~/Downloads/mongodb$ cd bin
pankaj@Pankajs-MacBook-Pro:~/Downloads/mongodb/bin$ ./mongod --dbpath /Users/pankaj/Downloads/mongodb/data &
Mon Oct 14 15:55:52.131 [initandlisten] MongoDB starting : pid=950 port=27017 dbpath=/Users/pankaj/Downloads/mongodb/data 64-bit host=Pankajs-MacBook-Pro.local
Mon Oct 14 15:55:52.131 [initandlisten] 
Mon Oct 14 15:55:52.131 [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
Mon Oct 14 15:55:52.131 [initandlisten] db version v3.4.7
Mon Oct 14 15:55:52.131 [initandlisten] git version: b9925db5eac369d77a3a5f5d98a145eaaacd9673
Mon Oct 14 15:55:52.131 [initandlisten] build info: Darwin bs-osx-106-x86-64-2.10gen.cc 10.8.0 Darwin Kernel Version 10.8.0: Tue Jun  7 16:32:41 PDT 2011; root:xnu-1504.15.3~1/RELEASE_X86_64 x86_64 BOOST_LIB_VERSION=1_49
Mon Oct 14 15:55:52.131 [initandlisten] allocator: system
Mon Oct 14 15:55:52.131 [initandlisten] options: { dbpath: "/Users/pankaj/Downloads/mongodb/data" }
Mon Oct 14 15:55:52.132 [initandlisten] journal dir=/Users/pankaj/Downloads/mongodb/data/journal
Mon Oct 14 15:55:52.132 [initandlisten] recover : no journal files present, no recovery needed
Mon Oct 14 15:55:52.238 [FileAllocator] allocating new datafile /Users/pankaj/Downloads/mongodb/data/local.ns, filling with zeroes...
Mon Oct 14 15:55:52.238 [FileAllocator] creating directory /Users/pankaj/Downloads/mongodb/data/_tmp
Mon Oct 14 15:55:52.347 [FileAllocator] done allocating datafile /Users/pankaj/Downloads/mongodb/data/local.ns, size: 16MB,  took 0.108 secs
Mon Oct 14 15:55:52.646 [FileAllocator] allocating new datafile /Users/pankaj/Downloads/mongodb/data/local.0, filling with zeroes...
Mon Oct 14 15:55:53.578 [FileAllocator] done allocating datafile /Users/pankaj/Downloads/mongodb/data/local.0, size: 64MB,  took 0.931 secs
Mon Oct 14 15:55:53.871 [initandlisten] command local.$cmd command: { create: "startup_log", size: 10485760, capped: true } ntoreturn:1 keyUpdates:0  reslen:37 1633ms
Mon Oct 14 15:55:53.872 [initandlisten] couldn't unlink socket file /tmp/mongodb-27017.sockerrno:13 Permission denied skipping
Mon Oct 14 15:55:53.872 [websvr] admin web console waiting for connections on port 28017
Mon Oct 14 15:55:53.872 [initandlisten] waiting for connections on port 27017
pankaj@Pankajs-MacBook-Pro:~/Downloads/mongodb/bin$ 

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.
install mongodb on mac


pankaj@Pankajs-MacBook-Pro:~/Downloads/mongodb/bin$ ./mongo
MongoDB shell version: 3.4.7
connecting to: test
Server has startup warnings: 
Mon Oct 14 16:11:35.330 [initandlisten] 
Mon Oct 14 16:11:35.330 [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
> show dbs
local	0.078125GB
> use journaldev
switched to db journaldev
> db.names.save({"id":123,"name":"Pankaj"})
> db.names.find()
{ "_id" : ObjectId("525c7af46e7301702bbfea1e"), "id" : 123, "name" : "Pankaj" }
> db.datas.save({})
> show collections
datas
names
system.indexes
> show dbs
journaldev	0.203125GB
local	0.078125GB
> exit
bye
pankaj@Pankajs-MacBook-Pro:~/Downloads/mongodb/bin$ 

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.

db.names.save({}): 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.


pankaj@Pankajs-MacBook-Pro:~/Downloads/mongodb/bin$ vi ~/.bash_profile 
//add below line and save it
export PATH=$PATH:/Users/pankaj/Downloads/mongodb/bin
pankaj@Pankajs-MacBook-Pro:~/Downloads/mongodb/bin$ source ~/.bash_profile 

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.

org.mongodb.mongod.plist


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>org.mongodb.mongod</string>
  <key>ProgramArguments</key>
  <array>
    <string>/Users/pankaj/Downloads/mongodb/bin/mongod</string>
    <string>run</string>
    <string>--config</string>
    <string>/Users/pankaj/Downloads/mongodb/mongod.conf</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
<key>UserName</key>
<string>pankaj</string>
</dict>
</plist>

pankaj@Pankajs-MacBook-Pro:~$ cd /Library/LaunchDaemons
pankaj@Pankajs-MacBook-Pro:/Library/LaunchDaemons$ sudo vi org.mongodb.mongod.plist 
pankaj@Pankajs-MacBook-Pro:/Library/LaunchDaemons$ sudo launchctl load org.mongodb.mongod.plist 

#below commands to start and stop service through launchctl command
pankaj@Pankajs-MacBook-Pro:/Library/LaunchDaemons$ sudo launchctl start org.mongodb.mongod
pankaj@Pankajs-MacBook-Pro:/Library/LaunchDaemons$ sudo launchctl stop org.mongodb.mongod

pankaj@Pankajs-MacBook-Pro:/Library/LaunchDaemons$ ps -eaf | grep mongod
    0    61     1   0  3:04PM ??         0:06.31 /Users/pankaj/Downloads/mongodb/bin/mongod run --config /Users/pankaj/Downloads/mongodb/mongod.conf
pankaj@Pankajs-MacBook-Pro:/Library/LaunchDaemons$

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:


pankaj@Pankajs-MacBook-Pro:~/Downloads/mongodb$ cat mongod.conf 
#we can change port, I am setting default port
port=27017

#we can enable verbose mode, by default its false
verbose=true

#limit max connections, cant set more than 20000
maxConns=50

#setting up log path
logpath=/Users/pankaj/Downloads/mongodb/logs/mongo.log
logappend=true

#create PID file
pidfilepath=/Users/pankaj/Downloads/mongodb/mongo.pid

#setting dbpath, dont want to use default /data/db path
dbpath=/Users/pankaj/Downloads/mongodb/data

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.

References:

MongoDB Official Website
Running MongoDB as Service on Mac OS X

Comments

  1. Shoeb says:

    Excellent tutorial. Great work.

  2. Prem Tiwari says:

    Great Sharing…..

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages