Sunday, September 12, 2010

How to install MongoDB on OS X

Download, unpack, and install the pre-compiled 64-bit binaries:

curl -O
tar xzf mongodb-osx-x86_64-1.6.2.tgz
sudo mv mongodb-osx-x86_64-1.6.2 /opt/local/mongodb
sudo mkdir /opt/local/mongodb_data /var/log/mongodb
sudo chown -R root /opt/local/mongodb

(If you’re on a 32-bit machine, substitute in i386 for each x86_64 above.)

Next, you’ll want to make a config file so you can change the server’s options without fiddling with command-line arguments.

Save as: /opt/local/mongodb/mongod.conf

# Store data alongside MongoDB instead of the default, /data/db/
dbpath = /opt/local/mongodb_data

# Only accept local connections
bind_ip =

Now, we’ll make a launchd job to register the server as an OS X daemon. launchd will start the server at startup, stop it before shutdown, make sure it stays up, and redirect its output to a nice log file.

Save as: /Library/LaunchDaemons/org.mongodb.mongod.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
<plist version="1.0">






Now we just need to load the launchd job:

sudo launchctl load /Library/LaunchDaemons/org.mongodb.mongod.plist

And that should do it! Try visiting http://localhost:28017 to see the status console for your database.

One last thing: you should probably add /opt/local/mongodb/bin to your $PATH. That way you can use the other binaries that ship with MongoDB, like the mongo console, mongoexport, and so on.

You can adjust your path the regular way by editing your shell’s profile, or you can use this nice paths.d mechanism that OS X provides:

sudo sh -c 'echo "/opt/local/mongodb/bin" > /etc/paths.d/mongodb'