How to restart a NoSQL Database service like MongoDB?

If we're going to use a NoSQL DB for our app, then we want something fast and easy to use.

We learn that "NoSQL" doesn't necessarily imply "no maintenance". We think about using managed hosting services like MongoDB's Atlas or Amazon's DynamoDB, but we choose to host it ourselves, whether on our premises or in our own cloud instance. We evaluate several NoSQL options including Redis and Cassandra and pick MongoDB.

We may be able to get it installed by installing it from our Linux distribution, using Mongo’s repositories, or using a snap. But if something goes wrong, we might need to restart it.

We’ll look at the different methods for restarting MongoDB database.


We may be able to locate references to the legacy script services. They provide a single standard command that works regardless of whether our Linux installations use systemd, upstart, or some other type of startup service. It doesn't matter if you use services or not; it's just not necessary anymore.

Let's start up our local instance of MongoDB again −

$ service mongod restart

During the Great Init Wars, when different groups were arguing over different methods for starting up and controlling parts of a Unix or linux system, this program became popular.

Red Hat Linux contributed a centralized script called "system-config-kickstart" which takes care of running these scripts at startup and stopping them when they're no longer needed.

Canonical attempted to replace these scripts with a system known as Upstart.

The service script evolved to handle both of the competing start, stop, and status tools — and now, the service script also wraps around systemctl. Red Hat adopted SystemD in their 7.0 version. Canonical adopted SystemD in Ubuntu 20.04

Recently, systemd has become the default way for us to start services in Linux environments. And service is no longer necessary.

Use systemctl

We’ll be using the systemctl tool to manage MongoDB and any dependencies.

When we initially install MongoDB, all of its binary and configuration file locations are already set up, but it isn't currently running.

To see if we can use the status command to check whether our service has started, we can run the following command −

$ systemctl status mongod
mongod.service - MongoDB Database Server
   Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor
preset: enabled)
   Active: inactive (dead)

Systemd provides us with several commands for starting, stopping, and restarting services.

Let’s restart using following command −


$ sudo service mongod restart $ sudo service mongod status


mongod.service - MongoDB Database Server
   Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor
   preset: enabled)
   Active: active (running) since Tue 2022-10-11 07:45:50 HST; 7s ago
   Main PID: 124287 (mongod)
   Memory: 135.4M
   CGroup: /system.slice/mongod.service
   └─154987 /usr/bin/mongod --config /etc/mongod.conf
   Oct 11 07:45:50 shoes systemd[1]: Started MongoDB Database Server.

We can check the status to get recent log entries for the date and time they occurred and the hostname where they were logged.

To restart up our MongoDB server again, we just need to run −

$ systemctl mongod restart

Setting MongoDB to Launch on system Boot With enable

However, even though we've started MongoDB using systemd, that doesn't necessarily mean it will always be running when our system boots up.

We can use systemctl “enable” command to ensure that MongoDB starts up with our system.

$ sudo systemctl enable mongod
Created symlink /etc/systemd/system/ → /lib/systemd/system/mongod.service.

Now that we've seen how systemd manages service files, let's take a quick look at the links under etc/systemd for a listing of those service files.

Now that we've set up our Dockerfile for the application, let's run it! We want to ensure that MongoDB starts up when the container boots. To do this, we need to tell Docker to launch MongoDB after all of its dependencies have been met.


Here, we're using the systemd service management tool to check the status of our MongoDB server and then to start it again if necessary.

There are other ways to do it, but we don’t need to know about them unless we’re working on an older system.

Finally, we remember to "enable" our MongoDB server if we want it to start automatically when we restart our computer.