How to Automatically Restart MongoDB in Ubuntu Server
Jasser Mark Arioste
Introduction #
In this guide, you'll learn how to automatically restart MongoDB in an Ubuntu Server such as a digital ocean droplet. One advantage of using MongoDB in a digital ocean droplet or any VPS server rather than a fully managed service like MongoDB atlas is that it's much cheaper. However, it also comes with more responsibilities and headaches.
One of the problems is that if your MongoDB service is not set up correctly, the mongod.service
process can terminate and you won't have access to your database for a couple of minutes at least. To increase the reliability of your MongoDB server, you have to set up the auto restart configuration.
Step 1: Modifying mongod.service
file
#
I assume you've already installed MongoDB on your Ubuntu server. When you install MongoDB on Ubuntu using the official MongoDB packages, it typically comes with a pre-configured systemd service unit file, so you don't need to create one manually. The mongod.service
file is located in the /lib/systemd/system/
directory. Though sometimes it would also be in /etc/systemd/system
directory.
It's best to confirm this by running the command:
systemctl status mongod.service
It would show the following output. See line 2:
● mongod.service - MongoDB Database Server
Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2023-11-05 00:00:02 UTC; 6 days ago
Docs: https://docs.mongodb.org/manual
Main PID: 2596627 (mongod)
Memory: 125.5M
CGroup: /system.slice/mongod.service
└─2596627 /usr/bin/mongod --config /etc/mongod.conf
First, let's edit the mongod.service by using the following command:
sudo nano /lib/systemd/system/mongod.service
#or sudo nano /etc/systemd/system/mongod.service
Next, insert the following line under [Service]:
Restart=always
This will ensure that the mongod.service
process will auto restart no matter what happens. This explicitly states that MongoDB should always be restarted by systemd when it stops, regardless of the reason. It's more explicit in its intent.
Step 2: Reloading systemctl daemon
#
The next step is to run the following command:
sudo systemctl daemon-reload
The `sudo systemctl daemon-reload`
command is used to reload the systemd
manager configuration without stopping any running services. When you modify systemd
service unit files or other systemd-related configuration files, you should run this command to notify systemd
of the changes.
Here's what it does in more detail:
- Updates systemd's internal configuration: When you make changes to systemd service unit files, timers, or other related configuration files, systemd doesn't immediately apply those changes. Instead, it keeps its configuration cached in memory.
systemctl daemon-reload
forces systemd to re-read and update its internal configuration to reflect any recent changes. - Ensures the changes take effect: After reloading the systemd daemon, any new or modified service units will be recognized by systemd, and it will apply the changes accordingly. This ensures that systemd is aware of your modifications and can manage services based on the updated configuration.
- Doesn't affect running services: Importantly,
sudo systemctl daemon-reload
doesn't stop or restart any running services. It's a non-disruptive operation that only refreshes systemd's knowledge of available service units and their configuration.
Running this command after modifying the MongoDB systemd service unit file is necessary to ensure that systemd recognizes and applies your changes to the MongoDB service without needing to restart the entire systemd manager.
Step 3: Auto restart mongod.service process on reboot #
Finally, let's run the following command to auto-restart mongod.service
process whenever the Ubuntu server reboots:
sudo systemctl enable mongod.service
Here's what this command does in detail:
- Enables automatic startup: When you run this command, it creates symbolic links in the appropriate systemd target directories, indicating that the
mongod.service
unit should be started automatically when the system boots up. - Ensures persistence: By enabling a service, you ensure that it remains enabled across reboots. Even after a system restart, systemd will start MongoDB as part of the system initialization process.
- Simplifies service management: Enabling a service makes it easier to manage. You can start, stop, restart, or check the status of the MongoDB service using simple
systemctl
commands.
This is a crucial step when configuring a service to ensure that it starts automatically when your system boots up, making it readily available without manual intervention.
Conclusion #
You just learned how to automatically restart a MongoDB server in Ubuntu server to make it more reliable and robust, regardless of the reason for its termination.
Credits: Image by alandsmann from Pixabay