How to start MongoDB automatically when starting your Mac OS X

So, everytime when I have to start my node apps I often forgot to run my MongoDB service and it is a bit annoying where you have to have one tab dedicated to something you won't touch at all and I thought I should find a way to run this from my startup item on my Mac OS X.

Luckily, this post from Hunter Ford's blog explains how to run your MongoDB in your Startup item on Mac OS X and here is how:

Create a file call org.mongo.mongod.plist in /Library/LaunchDaemons/

If you use vim you can do like this:

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

The reason I have this as sudo because it is something from root directory and you may failed to create this new file in vi.

Copy and paste this:

<?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>  

Also, you will 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 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  

That's it! Now everytime you turn on your Mac and start doing your work you don't have to worry about running this again!

Thanks to Hunter Ford for his awesome blog post.