MongoDB init.d script for Linux

If you’re thinking of trying MongoDB, one thing you’ll notice that doesn’t come with the binary downloads is an mongodb init.d script for Linux distributions, so here’s mine. It’s based on having mongodb installed in /usr/local/mongodb with the binaries in the /bin directory under there, but if you’ve got the binaries somewhere else you just need to change one line at the top of the file.

All you need to do is download the file to your Linux server, rename it from mongodb.txt to mongodb and copy it to /etc/init.d, then fix the permissions. The following commands should do it

cp mongodb.txt /etc/init.d/mongodb

chmod 755 /etc/init.d/mongodb

chown root:root /etc/init.d mongodb

Finally, you just need to run the following 2 commands to set mongodb to start on boot:

chkconfig —add mongodb

chkconfig mongodb on

The script works on Centos, but should work fine on Ubuntu, RHEL, etc with minimal changes.