mongo db startup init.d script on CentOS/Redhat

Here we are going to create init script for mongodb, so that mongodb can start at the time of boot. So follow below steps to configure mongodb startup script.


(1) Create a file /etc/init.d/mongodb with below content. You may need to change path of mongod. In below script the path is "/opt/mongo/bin/mongod". But you need to change according to your installation of mongodb. Also you may need to change "mongodb_user". This will run mongo db as specified user.
(2) Now make it executable using below command.

chmod a+x /etc/init.d/mongodb

(3) Now you need to make it runable in runlevel 3,4 & 5. So use below command.

chkconfig --level 345 mongodb on

chkconfig --list mongodb

You can see mongodb service is configured to run at runlevel 3 4 & 5.

(4) Once you are done with all above 3 steps, you can start & stop mongodb service.
Note: please make sure to kill the currently running mongodb process.