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" "">  
<plist version="1.0">  

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.