Starting MongoDB automatically on Mac OS X

Quick and easy tip to launch MongoDB automatically on Mac OS X.

Let's start by creating a file, you can do this with Vim in Terminal.

sudo vim /Library/LaunchDaemons/org.mongo.mongod.plist  

Copy and paste this into the new file you just created.

<?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>ProgramArguments</key>
    <array>
        <string>/usr/local/bin/mongod</string>
        <string>--dbpath</string>
        <string>/var/lib/mongodb/</string>
        <string>--logpath</string>
        <string>/var/log/mongodb.log</string>
    </array>
</dict>  
</plist>  

You will also need to create a file for the log and a directory for the database.

sudo touch /var/log/mongodb.log  
sudo mkdir /var/lib/mongodb  

And now you can run this in your terminal:

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  

And that's it. Now everytime you turn on your Mac you don't have to worry about running this again.