1. Overview

Say we’re using a NoSQL database for our application — perfect for quick access to lots of key-value pairs, without all the overhead of designing tables and writing queries.

We discover, however, that “NoSQL” doesn’t mean “no maintenance”. We consider using managed hosting, like MongoDB’s Atlas or Amazon’s DynamoDB, but we decide to host it ourselves, either on our premises or in our own cloud instances. We look at a few NoSQL choices like Redis and Cassandra and decide on MongoDB.

We install it (maybe from our Linux distribution, maybe from Mongo’s repository, maybe from a snap) but something goes wrong, and we need to restart it.

In this tutorial, we’ll examine the various ways to restart our MongoDB Database.

2. service With a Smile?

When searching for how to restart our database, we may find references to the legacy script service. It works by providing one standard command that works no matter what sort of startup system our Linux installation uses. There’s nothing wrong with using service, but it’s no longer necessary.

We could restart our local installation of MongoDB with it:

service mongod restart

This tool grew popular during the Great Init Wars, during which people argued over different ways to start up and control parts of a Unix or Linux system.

Red Hat Linux contributed this script as a centralized wrapper for the scripts in /etc/init.d. These scripts would run on boot and take care of starting (and restarting) system services like the cron daemon and Apache HTTP server.

Canonical tried to replace these scripts for Ubuntu with a system called Upstart.

The service script (we can take a look at it in /usr/sbin if we like!) evolved to handle both of these competing startup, status, and shutdown tools — and now, it also wraps around systemd. Red Hat switched to systemd in their 7.0 release. Canonical switched in Ubuntu 15.04.

More recently, systemd has become the standard we can expect in any Linux environment, and service is unnecessary.

3. Everything Is Under systemctl

We’ll use the systemctl tool to work with systemd. This manages MongoDB and any dependencies.

When we first install MongoDB, all the binaries and configuration files are in place, but it isn’t running.

We can check this with the status function of systemctl:

[email protected]:~$ systemctl status mongod
mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
     Active: inactive (dead)
       Docs: https://docs.mongodb.org/manual

We can also ask systemctl to startstop, and restart our mongod service.

Let’s restart it now:

[email protected]:~$ sudo service mongod restart
[email protected]:~$ 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 2021-07-13 07:45:50 HST; 7s ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 154987 (mongod)
     Memory: 155.4M
     CGroup: /system.slice/mongod.service
             └─154987 /usr/bin/mongod --config /etc/mongod.conf

Jul 13 07:45:50 shoes systemd[1]: Started MongoDB Database Server.

Checking the status also gives us recent logs to see the date events happened and the server name.

The quick and easy way to restart our MongoDB server is:

systemctl mongod restart

3.1. Setting MongoDB to Launch on Boot With enable

But just because we’ve used systemctl to start mongod, that doesn’t mean it will automatically launch every time our system starts.

Let’s use systemctl‘s enable option to make sure MongoDB starts up with the rest of our system:

[email protected]:~$ sudo systemctl enable mongod
Created symlink /etc/systemd/system/multi-user.target.wants/mongod.service → /lib/systemd/system/mongod.service.

As we see, the mongod.service file in /lib/systemd is now included in the “multi-user” or default run level. We can look at the links under /etc/systemd for a list of those services.

This target now “wants” MongoDB to start up on a normal boot, so mongod will be launched after all of its dependencies are met (for example, after the network has started).

4. Conclusion

In this article, we use the systemd suite to restart our MongoDB server. We use systemctl to check the status of and restart our mongod server.

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

And finally, we remember to set our MongoDB server to ‘enabled’ if we want it to come up again on a reboot.

Comments are closed on this article!