Shutdown/Stop MongoDB

Bilal Shahid Dec 12, 2022
  1. Starting MongoDB
  2. Stopping MongoDB
  3. Shutdown Command
  4. Methods Specified in the Manual
  5. Single Line Commands
Shutdown/Stop MongoDB

MongoDB is a popular platform where users can easily organize their databases and carry out multiple tasks. However, getting the hang of a new interface might take a bit.

It’s common to be on the hunt for new methods to start or stop a platform. In the same way, if you’re looking for a new way to stop MongoDB, you’re in the right place.

This article discusses multiple ways to stop MongoDB so you can pick whatever you find the easiest.

Starting MongoDB

Before we discuss multiple ways to close MongoDB, it is best to discuss a simple method to start it. One of the most common ways to do so is using the code below.


This command will allow you to start MongoDB and begin using it.

Stopping MongoDB

There are multiple ways to get it done when it comes to stopping MongoDB. There is no specific method that can be categorized as the best.

Instead, users often pick a method based on their preference and usage of MongoDB. Here are some of the many ways you can stop MongoDB.

Shutdown Command

The most common method is probably using the shutdown command. It uses a few short lines of code, as shown below.

use admin

This will instantly allow you to stop MongoDB with no problem at all.

Methods Specified in the Manual

If you view the MongoDB manual, you might come across suggested methods to stop it using shell, cli, drivers, and more. Moreover, it also discusses the risks of stopping it abruptly or incorrectly.

The methods to stop MongoDB specified in the manual are as follows:

Use It in a Package

If you have been using MongoDB in a package such as Ubuntu or Debian, you can easily stop it using this code.


sudo service mongod stop


sudo /etc/init.d/mongod stop

Mac OS X-Based Systems

If you’re using a system that runs on Mac OS X, you’ll need to opt for a different process. You essentially have to find the process ID first and use it to stop it.

So, the first step would be to find the PID of the MongoDB process with the following command:


This lists all of the PIDs that are currently in use. Identify the one on your system designated to MongoDB by skimming through the list.

Once you’ve found it, use this line of code to get it to stop.

$ kill <PID>

Here, replace PID within the tags with the PID you identified for your system that corresponds to MongoDB.

Red Hat-Based Systems

It is pretty straightforward on Red Hat systems. All you need is a single line of code.

service mongod stop

Windows-Based Systems

For Windows, it is pretty simple as well. If you have installed it as a service named MongoDB, using this will work fine.

net stop MongoDB

However, if you have not installed it as a service and are using Windows 7 or later, the following line can help you stop MongoDB.

taskkill /f /im mongod.exe

Single Line Commands

The majority of users want quick solutions to practically everything. In the same way, some users are always on the hunt for simple lines of code that can get their job done.

When shutting down MongoDB, there are a few ways to get it done in a single line of code. Some of those methods have already been mentioned above.

If you would like to delve into further possibilities within one line, here are a few more.

Use an Alias

The simplest way to simplify code is by using an alias. You can do the same thing to generate a code that stops MongoDB in one line.

mongo --eval "db.getSiblingDB('admin').shutdownServer()"

the Manual Recommendation

The MongoDB tutorial also offers a solution in one line. This isn’t used frequently, but it still works.

mongod --dbpath /path/to/your/db --shutdown

Foreground Process

If MongoDB is running as a foreground process, you may use Ctrl+C to stop it. It is important to note that this may not work in all instances.

Create a Separate File

Another method is to create a file and save the following code.

mongo admin --eval "db.shutdownServer()"

The name of the file can be mongostop.bat or something similar. Now, when you run the file, MongoDB will stop automatically.

the kill Command

As explained above, the top command will help you find the PID to stop MongoDB. However, there are variations to the kill command as well.

kill -2 PID

This sends signal 2 or SIGINT while removing -2 sends signal 15 or SIGTERM.

You can also use the pkill command if you have more than one instance running or the PID doesn’t matter. You could send the signal to all MongoDB processes with this.

pkill mongodb

However, it is much safer to target only those processes that belong to the user with the following:

pkill -U $USER mongod

Note: If you have administrative rights, but the database is running as another user, you will need to use the above commands by adding sudo before them. Without this, they might not work.

An unclean shutdown can lead to various problems in MongoDB or your system. This is why it is crucial to identify a correct and safe shutdown of MongoDB.

We have introduced you to various methods throughout this article so that you won’t have a problem with this in the future.

Author: Bilal Shahid
Bilal Shahid avatar Bilal Shahid avatar

Hello, I am Bilal, a research enthusiast who tends to break and make code from scratch. I dwell deep into the latest issues faced by the developer community and provide answers and different solutions. Apart from that, I am just another normal developer with a laptop, a mug of coffee, some biscuits and a thick spectacle!