How to gracefully restart MongoDB installed through Ubuntu package manager

Hi there,

We have MongoDB 5.0.2 installed through Ubuntu 20.04 package manager (apt) and running as a systemd service (mongod.service). We would like to know which one of the below is the proper method to restart the mongo Instance in our scenario:

  • From Mongosh using “db.shutdownServer()” command


  • Directly by issuing “Systemctl stop mongod”

Thanks and Regards

Hi @Abdullah_Madani,
I think is the same thing.

Best Regards.

Hi @Fabio_Ramohitaj,

Thanks for the reply. So if I use the option 1 i.e. “db.shutdownServer()” command then does it stops the mongod service as well? Then can I start the DB by simply starting the mongod service? Please advise

None of the options you presented will

However both are valid ways to stop the server.

Both offers different security models.

Requires a valid database user with specific privileges on the admin database. See for more details.

You may run this command from any other machines, given the appropriate credentials, that can connect to the database. The database user issuing this command might not be able to restart mongod if he does not have access, as an OS user with sudo privileges, to the machine where mongod is running.

This can be done by any OS admin that has access with sudo privileges to the machine where mongod is running. This OS user will also be able to start mongod.

So depending of the security model you have. A DB user can terminate mongod using shutdownServer and only an OS user can start/restart it. The same person may have both DB user and OS user credentials.

Not entirely, especially for replica set members, see

1 Like

Hi @Abdullah_Madani ,

For any application, you can use

sudo systemctl restart <application_name>

e.g. sudo systemctl restart mongod

Hi there,
@steevej My ultimate goal is to reboot the MongoDB server. For that, I want to shutdown the MongoDB gracefully followed by the Server reboot and then start the MongoDB Instance.

So what I understand from the responses is, if I shutdown using “db.shutdownServer()” command the DB instance will stop gracefully. However the service “mongod.service” will only shutdown if the DB User has the privilege on the OS User. Please correct me if I am wrong!

While my understanding is that the DB users has no relation with the OS user. Their scope is limited to the Database only. The OS service “mongod.service” is owned by “mongodb” OS user which is also the owner of MogoDB file structure at OS Level.

So in short, if I stop MongoDB by “db.shutdownServer()” command using a user that has dbAdmin privilege on admin database, then it should in turn trigger “Mongodb” OS user to stop the service as well. After rebooting the machine, the MongoDB should directly start once the OS Service “mongod.service” is started.

Is my understanding is correct?

The solution is

I also saw that sometimes the service is mongodb rather than simply mongod.


No. If mongod is terminated with shutdownServer, systemd knows it is down systemctl status will indicate that mongod is not running.

Not exactly. mongod.service will restart at reboot if the service is enabled. A service has many states. Read systemd - Debian Wiki for more details.

If the mongod is started with systemctl, then you have nothing to do when you shutdown your computer in order to make sure that it terminates gracefully.

If you want mongod to start automatically when starting the computer, the service has to be enabled.

Thanks @steevej for your active participation and bringing your expertise and experience here. Your inputs were very helpful.

Thanks @Monika_Shah/Fabio_Ramohitaj for engaging and providing your proficient views.

So the simple takeaway is:

Both “sudo systemctl stop mongod.service” and "db.shutdownServer()” command shutdowns the MongoDB gracefully.

After rebooting the machine; issing “sudo systemctl start mongod.service” at OS level will bring the MongoDB up and running (in case the service was not “enabled” in systemctl for auto start)

Thanks and Regards,
Abdullah Madani


This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.