TroyGrosfield.com TroyGrosfield.com

Headline

Installing and Running MongoDB on a Mac

Author
by Troy Grosfield
Date
March 21st, 2011
Category
Developer
Story

MongoDB is a very powerful schema-free, document oriented database.  The following steps will show you how to get MongoDB up and running.

Install MongoDB

I used the Homebrew package manager to install mongodb. If you have Homebrew, run the following commands:

$ brew update
$ brew install mongodb

You will see output like the following:

$ brew install mongodb
==> Downloading http://fastdl.mongodb.org/osx/mongodb-osx-x86_64-1.8.0.tgz
######################################################################## 100.0%
==> Caveats
If this is your first install, automatically load on login with:
    mkdir -p ~/Library/LaunchAgents
    cp /usr/local/Cellar/mongodb/1.8.0-x86_64/org.mongodb.mongod.plist ~/Library/LaunchAgents/
    launchctl load -w ~/Library/LaunchAgents/org.mongodb.mongod.plist

If this is an upgrade and you already have the org.mongodb.mongod.plist loaded:
    launchctl unload -w ~/Library/LaunchAgents/org.mongodb.mongod.plist
    cp /usr/local/Cellar/mongodb/1.8.0-x86_64/org.mongodb.mongod.plist ~/Library/LaunchAgents/
    launchctl load -w ~/Library/LaunchAgents/org.mongodb.mongod.plist

Or start it manually:
    mongod run --config /usr/local/Cellar/mongodb/1.8.0-x86_64/mongod.conf
MongoDB 1.8+ includes a feature for Write Ahead Logging (Journaling), which has been enabled by default.
This is not the default in production (Journaling is disabled); to disable journaling, use --nojournal.
==> Summary
/usr/local/Cellar/mongodb/1.8.0-x86_64: 16 files, 93M, built in 2 seconds

The last line of the install output shows you the install location:

/usr/local/Cellar/mongodb/1.8.0-x86_64

Create Data Directory

MongoDB stores its data in the /data/db directory by default. However, it doesn’t generate that folder structure for you. So, let’s create that directory:

$ mkdir -p /data/db

Run MongoDB

Run MongoDB using the following command:

$ ./usr/local/Cellar/mongodb/1.8.0-x86_64/bin/mongod

Your terminal should now be running MongoDB.

Note: Your path to mongod may be different. Adjust your path accordingly.

Use MongoDB

In a new terminal, open the mongo shell:

$ ./usr/local/Cellar/mongodb/1.8.0-x86_64/bin/mongo
> db.my_collection.save( { 'hello' : 'world' } )
> db.my_collection.find()

Your output should read:

{ "_id" : ObjectId("4d877751bba9e417ff3bb1ae"), "hello" : "world" }

Side Notes

If you try and start mongod and it’s not working try running:

$ sudo ./usr/local/Cellar/mongodb/1.8.0-x86_64/bin/mongod

Resources

Much of this process is located at: Quickstart OS X

Tags
Comments
3 Comments »

3 Comments

Leave a reply

 
  1. Author
    pinar
    Date
    November 11th, 2011 at 2:37 pm
    Comment

    thanks you so much! This was super helpful!

  2. Author
    Adam
    Date
    August 3rd, 2011 at 4:53 am
    Comment

    Thank you greatly for this post, much appreciated!! Been a Win/.Net/SQL Server dev for 15 years – ouch! -finally bought my first Mac last week, OSX/Lion –> what a joy! Loving and suffering the learning curve!

  3. Author
    My first steps into Mac land « Claudio Lassala's Blog
    Date
    July 21st, 2011 at 12:00 pm
    Comment

    […] been using MongoDB on my Rails projects. In order to get it going, I’ve followed the instructions here. Which has led me to Homebrew. In order to get Homebrew going, I needed XCode (5 bucks). After […]