Automatically power up MongoDB service
If you leverage technology like Node.js in building applications that have to process a high volume of short messages needing low latency, then you might have had to start up MongoDB service. Additionally, it’s exceptionally frustrating when you have to dedicate a tab to it in the terminal knowing you won’t touch it. I thought there should be a way to run the service from startup item in Mac OS X.
Fortunately, I found a workaround of how I can automate the process when I power up my Mac. Here is how you can do it:
- You need to create a file called org.mongo.mongod.plist in /Library/LaunchDaemons/
If vim is your default editor, you can do something like this:
sudo vim /Library/LaunchDaemons/org.mongo.mongod.plist
If you don’t sudo into the root directory, it is likely going to output an error and not create the new file. You should enter your password if prompted to do so.
- Next, you should copy and paste the code below into vim:
<?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>/data/db/</string> <string>--logpath</string> <string>/var/log/mongodb.log</string> </array> </dict> </plist>
- You will need to create a log file and a directory where your database will reside. Use the command below:
sudo touch /var/log/mongodb.log sudo mkdir /data/db
- Lastly, you should run these commands in the 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
There you have it! You don’t have to worry about running MongoDB service; you can power up your Mac and start working.
Keep on, keeping on! ✌🏽