To set up MongoDB to auto-restart, you can use system-level services and configuration MongoDB files.


1. Server root login credentials.

Step 1:

Log in to your Ubuntu or Centos server as a user with sudo privileges. You can use SSH or directly access the server.

$ ssh root@Ip

Step 2:
Create a Systemd service unit file and edit your MongoDB service.
$ vi /etc/systemd/system/mongod.service
Add the below lines in mongod.service

Under service, add the below lines..

Step 3:
Reload the Systemd configuration
$ sudo systemctl daemon-reload

Step 4:
Then enable the monod.service
$ systemctl enable mongod.service

Step 5:
Start the MongoDB service:
$ sudo systemctl start mongod


Following the steps mentioned above, we can set up the auto-restart MongoDB.

Leave a Reply