Thursday, 9 July 2020

Start mongo database(mongod) automatically after system reboot

After OS patching process we need a database administrator support to stop and start database.To avoid such small dependency we can write small shell script which will take care of database startup process after system Reboot. In below example we are creating a shell script and seting it in crontab, so database will be start automatically after system reboot.  

1] creating a test.js file for specify database commands to test connectivity.

[mongo@localhost ~]$ cat test.js
print("Successfully connect to Test database ");
db.getName();

2] creating main shell script db_startup.sh

[mongo@localhost ~]$ cat db_startup.sh
#!/bin/bash

export PATH=/u01/mongo/mongodb-linux-x86_64-rhel70-4.2.8/bin:$PATH

export MONGO_HOME=/u01/mongo/mongodb-linux-x86_64-rhel70-4.2.8

export LOG_FILE="/home/mongo/STARTUP_LOG.txt"
export MAIL_FROM=abc@axmple.com
export MAIL_TO=pqr@example.com
cd /u01/mongo/mongodb-linux-x86_64-rhel70-4.2.8/bin

echo "===========Starting Database========" > $LOG_FILE
echo " " >> $LOG_FILE
echo " " >> $LOG_FILE

mongod -f /u01/DATA/appdb/mongod.conf >> $LOG_FILE

echo  " "  >> $LOG_FILE
echo  " "  >> $LOG_FILE
echo "-------checking database status ------" >> $LOG_FILE

echo  " " >> $LOG_FILE
echo  " " >> $LOG_FILE

if [ `ps -ef | grep -i mongod.conf | wc -l` -le 1 ]; then
    echo "mongodb not running" >> $LOG_FILE
    exit 1
else
    echo "database started successfully" >> $LOG_FILE
fi

echo "=======checking connectivity of database======" >> $LOG_FILE
echo " ">> $LOG_FILE
echo " ">> $LOG_FILE
./mongo -u admin -p Admin1234 < /home/mongo/test.js  >> $LOG_FILE

cat $LOG_FILE | mailx -r $MAIL_FROM -s "mongodb status After server reboot" -a $LOG_FILE $MAIL_TO

exit

3] specify the entry in crontab for above shell script. 

[mongo@localhost ~]$ crontab -e
crontab: installing new crontab

[mongo@localhost ~]$ crontab -l
@reboot sleep 60 && /home/mongo/db_startup.sh > /home/mongo/db_startup.log
[mongo@localhost ~]$

4] reboot the system for test the script.

5] Output of log file:

[mongo@localhost ~]$ cat STARTUP_LOG.txt

===========Starting Database========


about to fork child process, waiting until server is ready for connections.
forked process: 10178
child process started successfully, parent exiting


-------checking database status ------


database started successfully
=======checking connectivity of database======


MongoDB shell version v4.2.8
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("bbd16589-fc43-48ca-a16b-1b65cd77c9f0") }
MongoDB server version: 4.2.8
Successfully connect to Test database
test
bye

No comments:

Post a Comment

work on autovacuum postgreSQL parameter

 In This blog, we are discussing the auto vacuum parameter on a small scale. we will understand the below parameters and will see how to mod...