HOWTO · MongoDB
How to Shutdown/Stop MongoDB
This article explains the various methods to shut down MongoDB.
On this page
The right way to stop MongoDB depends on how the server is managed. Use the database shell when you can connect to the server, the operating system’s service controls for a service-managed deployment, and the controlling terminal for a foreground mongod. Process signals are a last resort because they can stop the wrong process or interrupt an orderly shutdown. Choose the method that matches the way this instance was started.
Choose the shutdown method for your deployment
First identify how the target server runs. If an administrator or deployment tool manages it as a service, use that same service manager. If you started mongod in a terminal, stop it from that terminal. If you have an authorized database-shell connection, use the database-shell shutdown method described below. Do not mix methods casually: for example, stopping a service-managed process directly can leave its manager’s view of the service out of sync.
Before acting, make sure you have identified the intended MongoDB instance and have the access required by its deployment. This article does not establish particular shell launch commands, service names, operating-system commands, or access requirements; those vary with the environment and must be checked against its configuration.
Shut down MongoDB from a database shell
Use db.shutdownServer() from a database shell to request shutdown. Before using it, confirm the shell invocation and connection procedure for your deployment, verify that the connection targets the intended server and has the required permissions, and check that the method is supported with the syntax for your MongoDB version.
A shell request is useful when you need to stop a server through its database interface rather than its operating-system service manager. If the request cannot reach the intended server or is not authorized, do not switch immediately to a broad process-termination command. Check the deployment’s connection and access configuration, or use the control path responsible for managing that instance.
Stop a MongoDB service with the operating system
For a service-managed deployment, use the operating system’s service controls and the service identity configured on that host. The exact command and service name depend on the system and installation; no single command is established here as current or universal. Consult the host’s service configuration or its administrator, then issue the stop request through that manager.
This approach keeps the shutdown request aligned with the service that starts and supervises MongoDB. Avoid substituting a process-name termination command just because a service command is unfamiliar. If the manager reports an error, confirm that you selected the correct service and have the necessary permissions before trying another control path.
Stop a foreground mongod process
When mongod is attached to a terminal you control, stop it from that same terminal. You may use Ctrl+C only when the controlling terminal maps that key combination to an interrupt for this foreground process. Whether it does depends on the terminal and how you launched mongod. Use the control that the foreground environment provides, and wait for the process to finish rather than closing the terminal as a substitute for a shutdown request.
This method applies to a foreground process, not automatically to a service or a process running in another session. If the terminal is unavailable, identify the deployment’s actual manager before choosing another method.
Use process signals only when graceful controls are unavailable
Use a process signal only when the normal database-shell, service-manager, or controlling-terminal route is unavailable and you can identify the exact mongod process. A signal sent to the wrong PID can terminate an unrelated program; broad name-based commands can affect more than one MongoDB instance. Do not target every process matching a name, and do not assume a signal produces the same shutdown behavior in every deployment.
If you cannot confidently identify the intended process or determine the effect of the signal, stop and ask the system administrator to use the configured control path. Avoid escalating to forced termination merely because a graceful request is taking time; first check the status through the deployment’s own controls.
Verify shutdown and handle common problems
Confirm the result using the same interface that manages the instance: check the service manager for a service, the controlling terminal for a foreground process, or the deployment’s established status or health view. Make sure the status refers to the intended server. A request being issued is not by itself confirmation that the correct instance has stopped.
If the server still appears active, check that you selected the right instance and that the chosen control path has the required access. If the normal control is unavailable, use the fallback guidance above only when its conditions are met. An abrupt or incorrect shutdown can cause problems, so prefer the deployment’s graceful controls and avoid terminating processes you have not positively identified.