Setting up a Launch Daemon for MongoDB on Mac OS X

Step 1:
Create a new .plist at /Library/LaunchDaemons/org.mongo.mongod.plist.

Step 2:
Add the below XML to the .plist created above and save.

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

Step 3:
Run the below in Terminal if you haven’t already created a mongodb log and database directory when you installed mongodb:

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

Step 4:
Run the below in a Bash shell:

$ 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

Advertisements