How to Set Up MongoDB to Auto-Restart Using Systemd

Introduction

To set up MongoDB to automatically restart when the service stops or fails, you can use systemd service configuration. Systemd allows you to define restart behavior for the MongoDB service and ensure that MongoDB starts automatically after a system reboot.

This guide explains how to configure MongoDB to auto-restart on Ubuntu or CentOS using Systemd.

Prerequisites

  • Server root login credentials.
  • SSH access to the Ubuntu or CentOS server.
  • MongoDB installed and configured as a systemd service.

Implementation

Step 1: Log in to the Server

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 or Edit the MongoDB Systemd Service

Create a Systemd service unit file and edit your MongoDB service.

$ vi /etc/systemd/system/mongod.service

Add the following line under the [Service] section in mongod.service:

Restart=always

The Restart=always option instructs Systemd to automatically restart the MongoDB service whenever it stops.

Step 3: Reload the Systemd Configuration

After modifying the service file, reload the systemd configuration so that the changes are recognized.

$ sudo systemctl daemon-reload

Step 4: Enable the MongoDB Service

Enable the MongoDB service so that it starts automatically when the server boots.

$ systemctl enable mongod.service

Step 5: Start the MongoDB Service

Start the MongoDB service using the following command:

$ sudo systemctl start mongod

Step 6: Verify the MongoDB Service

Check the status of the MongoDB service to verify that it is running correctly.

$ sudo systemctl status mongod

If the service is running successfully, the status should indicate that mongod.service is active.

Conclusion

Following the steps mentioned above, you can configure MongoDB to automatically restart using Systemd. The Restart=always option helps keep the MongoDB service running when it unexpectedly stops, while enabling the service ensures that MongoDB starts automatically after a system reboot.

FAQs

1. Why does MongoDB need an auto-restart configuration?

An auto-restart configuration helps MongoDB recover automatically if the service unexpectedly stops.

2. What does Restart=always do?

Restart=always instructs Systemd to restart the MongoDB service whenever it stops.

3. How can I check whether MongoDB is running?

Use the following command:

sudo systemctl status mongod

4. How can I make MongoDB start after a server reboot?

Enable the MongoDB service using:

systemctl enable mongod.service

5. Does this configuration work on Ubuntu and CentOS?

Yes. Both Ubuntu and CentOS use Systemd on modern versions, so the systemctl commands can be used to manage the MongoDB service.

How to Clear the Data from MongoDB Collection

Learn how to clear data from a MongoDB collection when you need to remove existing records.

Read the MongoDB collection data guide

How to Connect to an Authenticated MongoDB Database Using MongoDB Compass

Learn how to connect to an authenticated MongoDB database through MongoDB Compass using an SSH connection.

Read the MongoDB Compass connection guide

Talk to our experts

Looking for the right technology solution for your business? Our team of experts can help you with development, cloud, DevOps, design, and a wide range of other technology needs. Get in touch with our team here.

ragupathi N

Writes about Containers & Kubernetes at Pheonix Solutions.

Leave a Reply

Scroll to Top