MongoDB Installation on Mac OS X

I wanted to learn NoSQL database and so I tried to install MongoDB and learn it. However, before I could start learning, I had to learn how to install it correctly! So I have tried to write down and share whatever I faced while trying to install MongoDB.

This document applies to following:

OS X Version: 10.8.4

MongoDB version: 2.4.6

HomeBrew version: 0.9.4

To install MongoDB on Mac, I used HomeBrew package manager. I read reviews of HomeBrew and like it so chose to install it. However, you could be using MacPorts or Fink.

To install HomeBrew Package Manager, type in following on Terminal:

ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"

Once installed, run following to make sure nothing else needs to be downloaded.

$ brew doctor

Then update and upgrade.

$ brew update
$ brew upgrade mongodb

Then you can do either of the following:

To have launchd start mongodb at login:

$ sudo ln -sfv /usr/local/opt/mongodb/*.plist ~/Library/LaunchAgents

Then to load mongodb now:

$ sudo launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist

Or, if you don’t want/need launchctl, you can just run:

$ sudo mongod

If you are lucky(!!), like me, then you will see following when you try to run using ‘mongod’ or ‘mongo’ command respectively.

MongoDB shell version: 2.4.6
connecting to: test
Tue Sep 3 14:28:06.411 Error: couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:145
exception: connect failed

OR

ERROR: could not read from config file

After spending lots of time looking for solutions online, I have found a way. There are many steps which are not mentioned in the installation manual on MongoDB website. If you are stuck just like me, then follow the following steps:

  1. Update your $PATH variable to point to your MongoDB bin file. In my case brew install MongoDB to this folder:
    /usr/local/Cellar/mongodb/2.4.6/
    
  2. In order to update your $PATH variable, do following:
    $ sudo vi /etc/paths
    

    Then, press ‘i’ to insert text in Vi and append the your MongoDB path to the end of the ‘paths’ file and restart the terminal.

    /usr/local/Cellar/mongodb/2.4.6/bin
    

    Use ‘Esc : w q’ to save and exit from Vi editor.

  3. Use echo to display your path variable:
    $ echo $PATH
    /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/Cellar/mongodb/2.4.6/bin
    
  4. Now try to check the Mongo version, if you get following, then you are on the right track!
    $ mongo --version
    MongoDB shell version: 2.4.6
    
  5. Now we need to create the database directory. I used the default ‘/data/db’ location suggested in MongoDB docs. I also created a log directory to avoid any permission issues while Mongo tries to create any logs. Change ownership and that will do the job.
    $ sudo mkdir /data/db
    $ sudo mkdir /data/log
    $ whoami
    username
    $ chown -R username /data
    
  6. Now, we will create a default config file for MongoDB to be provided for the first time we run ‘mongod’ command. Now, I will also like to point out that ‘mongod’ will start a service, which will listen for incoming data connections. This is similar having ‘$service mysqld start’ executed.Let’s go ahead and create the config file. Please keep in mind that I have created very basic config file. However, you can add many other variables to configure MongoDB. This is the first time I am playing with MongoDB, so I just know as much as I read on MongoDB docs!I created ‘mongodb.conf’.
    $ sudo vi /etc/mongodb.conf
    

    Add following:

    fork = true
    port = 27017
    quiet = true
    dbpath = /data/db
    logpath = /data/log/mongod.log
    logappend = true
    journal = true
    

    Please note that the default port for MongoDB server is 27017. Use your own path for dbpath and logpath you created in Step – 5. Don’t forget to close and save the conf file.

  7. Now we are all set to start our MongoDB service. Open two instances of Terminal.In Terminal 1, type in:
    $ sudo mongod -f /etc/mongodb.conf
    about to fork child process, waiting until server is ready for connections.
    forked process: 3516
    all output going to: /data/log/mongod.log
    child process started successfully, parent exiting
    

    If you get above message, then know that you have successfully started your Mongod service.

    Now, to connect to it, in Terminal 2 type following:

    $mongo test
    MongoDB shell version: 2.4.6
    connecting to: test
    Server has startup warnings:
    Tue Sep 3 16:55:43.527 [initandlisten]
    Tue Sep 3 16:55:43.527 [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
    >
    

    Ignore the warnings, but you are successfully connected to the ‘test’ database! Cool!

    Do a small test like below:

    > db.test.save( { a: 1 } )
    > db.test.find()
    { "_id" : ObjectId("522588b9d283d1fb90a43c17"), "a" : 1 }
    

    Congratulations! You just added a key-value pair in your ‘test’ database and retrieved it!

Alright, so this was my experience installing and running MongoDB and HomeBrew on my Mac. I hope you guys will find it helpful.

The following two tabs change content below.

Chinmay

Love to explore Web Technologies. SharePoint enthusiast. Love making websites, apps and plugins.

Latest posts by Chinmay (see all)