Installing MongoDB in Mac OS X

There are numerous posts, manuals and readme articles on installation of MongoDB in Mac OS X. This post documents the steps I went through to install the latest release of MongoDB on my Mac running Mountain Lion 10.8.5 & setting it up to run as a daemon. The steps outlined below closely follow the steps shared by Github user xiaolai in his repository MongoDB-OSX-Launchctl. One of the issue I faced following the steps in his readme was the a Bad file descriptor error raised by launchctl, possibly caused by a mistake I made in the paths of the directories required by MongDB.

To setup MongoDB to run as a daemon, download the latest version of MongoDB and decompress the dowloaded file –

1
2
wget http://downloads.mongodb.org/osx/mongodb-osx-x86_64-2.4.6.tgz 
tar -zxvf mongodb-osx-x86_64-2.4.6.tgz 

Copy the contents of the folder mongodb-osx-x86_64-2.4.6/bin/ to /opt/local/bin. Alternatively, you can use Mac Ports or Homebrew to install MongoDB. The next steps assume MongoDB binaries are present in /opt/local/bin.

Before you setup MongoDB to run as a daemon, you will need to create a data directory, a directory where log files will be written and a configuration file. Follow the steps below –

1
2
3
sudo mkdir -p /opt/local/var/db/mongodb
sudo mkdir -p /opt/local/var/log/
sudo touch /opt/local/var/log/mongodb.log

You can provide a configuration file to MongoDB or skip the following steps. Read more about the MongoDB configuration here – http://docs.mongodb.org/manual/reference/configuration-options/. A basic MongoDB configuration file (name it mongod.conf) looks like this –

1
2
bind_ip = 127.0.0.1
journal = true

If you have created a configuration file, copy it to the appropriate directory –

1
2
sudo mkdir -p /opt/local/etc/mongodb
sudo cp mongod.conf /opt/local/etc/mongodb/

The important step in setting up MongoDB to run as a daemon in Mac OS X is to create a Plist file and use it as a launchctl item. Create a file called org.mongo.mongod.plist with the following contents –

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?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.mongo.mongod</string>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <false/>
    <key>ProgramArguments</key>
    <array>
        <string>/opt/local/bin/mongod</string>
        <string>--dbpath</string>
        <string>/opt/local/var/db/mongodb</string>
        <string>--logpath</string>
        <string>/opt/local/var/log/mongodb.log</string>
        <string>--config</string>
        <string>/opt/local/etc/mongodb/mongod.conf</string>     
    </array>
</dict>
</plist>

Note that the path to the mongod and the directories created earlier should be correctly specified in the Plist file. Any errors in these program arguments result in errors.

Install the launchctl item like so –

1
2
3
4
sudo cp org.mongo.mongod.plist /Library/LaunchDaemons/.
sudo chown root:wheel /Library/LaunchDaemons/org.mongo.mongod.plist
sudo launchctl load /Library/LaunchDaemons/org.mongo.mongod.plist
sudo launchctl start org.mongo.mongod

Note that launchctl can be setup to run MongoDB with a Plist in /System/Library/LaunchDaemons, however, in my case, /Library/LaunchDaemons worked without any issues. With the release of OS X Mavericks in the horizon, I may need to update the post with any changes in the steps above.