Restarting/Stopping MongoDB Service

MongoDB service starts automatically after you successfully install it on your machine. To manually stop or restart MongoDB service, read the following instructions.

Restarting/Stopping MongoDB Service by Script

1.Run the stoporstartmongodb.sh command under the MongoDB directory after extracting installation files from the MongoDB.tar file.

2.Enter the MongoDB service name and press the Enter Key.

[root@localhost MongoDB]# . stoporstartmongodb.sh
Linux localhost.localdomain 3.10.0-327.e17.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64 GUN/Linux
You are root user
Please enter the name of mongodb service(default:mongodnetbrain)
mongodnetbrain
Please enter your choice
1 for stop mongodb service
2 for start mongodb service
1
dbservicename is: mongodnetbrain
succeed to stop mongodb service:mongodnetbrain

3.To stop the MongoDB service, press 1; To restart the MongoDB service, press 2.

Stopping MongoDB Service Manually

1.Stop the crontab -e task, which is used to check the MongoDB service timely (1 minute by default) and restart the service when it is down.

1)Open a command prompt and run the crontab -e command to edit the auto script.

[root@localhost ~]# crontab -e

*/1 * * * * /bin/bash -c 'if /usr/sbin/service mongodnetbrain status|grep -q -E "(dead)|failed"; 
then /usr/sbin/service mongodnetbrain start; fi' >/dev/null 2>&1

2)Press the Insert key to enter the editing mode of the script.

3)Add a pound sign (#) at the beginning of the auto script to comment it and press the ESC key to exit the editing mode.

#*/1 * * * * /bin/bash -c 'if /usr/sbin/service mongodnetbrain status|grep -q -E "(dead)|failed"; 
then /usr/sbin/service mongodnetbrain start; fi' >/dev/null 2>&1

4)Type the :wq! command at the command line and press the Enter key to save the changes.

2.Manually stop the MongoDB service by using one of the following methods:

Run the /etc/init.d/mongodnetbrain stop command at the command line.

Run the service mongodnetbrain stop command at the command line.

Restarting MongoDB Service Manually

1.Start the crontab -e task.

1)Run the crontab -e command to show the task script.

2)Press the Insert key to enter the editing mode of the script.

3)Remove the pound sign (#) from the beginning of the auto script and press the ESC key to exit the editing mode.

4)Type the :wq! command at the command line and press the Enter key to save the changes.

2.Manually restart the MongoDB service by using either of the following methods:

Run the /etc/init.d/mongodnetbrain start command at the command line.

Run the service mongodnetbrain start command at the command line.