Skip to content

Clean up shutdown procedures. #1433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 49 additions & 38 deletions source/tutorial/manage-mongodb-processes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -106,39 +106,65 @@ configurations for common use cases, see
Stop ``mongod``
---------------

To stop a :program:`mongod` instance not running as a daemon,
press ``Control+C``. MongoDB stops when all ongoing operations are
complete and does a clean exit, flushing and closing all data files.
A clean shutdown means that all ongoing MongoDB operations are
complete, and :program:`mongod` has flushed and closed all data files.

To stop a :program:`mongod` instance running in the background or
foreground, issue the :method:`db.shutdownServer()` helper in the
:program:`mongo` shell. Use the following sequence:
An unclean shutdown can compromise the validity of the data files. Subsequent
data operations could then cause corruption, or worsen existing corruption.

1. To open the :program:`mongo` shell for a :program:`mongod` instance
running on the default port of ``27017``, issue the following command:
To ensure a clean shutdown, use one of the following shutdown procedures:

.. code-block:: sh
Use ``shutdownServer()``
~~~~~~~~~~~~~~~~~~~~~~~~

Shut down the :program:`mongod` from the :program:`mongo` shell using
the :method:`db.shutdownServer()` method as follows:

.. code-block:: none

use admin
db.shutdownServer()

Calling the same method from a control script accomplishes the same result.

.. important::

You may only use :method:`db.shutdownServer()` when authenticated
to the ``admin`` database or via the localhost interface on systems
without authentication enabled.

Use ``--shutdown``
~~~~~~~~~~~~~~~~~~

mongo
From the Linux command line, shut down the :program:`mongod` using the
:option:`shutdown <mongod --shutdown>` option in the following command:

#. To switch to the ``admin`` database and shutdown the :program:`mongod`
instance, issue the following commands:
.. code-block:: none

.. code-block:: javascript
mongod --shutdown

use admin
db.shutdownServer()
Use ``CTRL-C``
~~~~~~~~~~~~~~

You may only use :method:`db.shutdownServer()` when connected to the
:program:`mongod` when authenticated to the ``admin`` database or on
systems without authentication connected via the localhost interface.
When running the :program:`mongod` instance in interactive mode, typing
``ctrl-c`` performs a clean shutdown.

Alternately, you can shut down the :program:`mongod` instance
from a driver using the :dbcommand:`shutdown` command. For details, see the
:doc:`drivers documentation </applications/drivers>` for your driver.
Use ``Kill``
~~~~~~~~~~~~

``mongod`` Shutdown and Replica Sets
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
From the Linux command line, shut down a specific :program:`mongod` instance
using the following command:

.. code-block:: none

kill <mongod process ID>

.. warning::

Never use ``kill -9`` (i.e. ``SIGKILL``) to terminate a mongod instance.

Shut down a Replica Set
~~~~~~~~~~~~~~~~~~~~~~~

If the :program:`mongod` is the :term:`primary` in a :term:`replica
set`, the shutdown process for these :program:`mongod` instances has
Expand Down Expand Up @@ -186,18 +212,3 @@ Alternately you can use the ``timeoutSecs`` argument with the

db.shutdownServer({timeoutSecs : 5})

Sending a UNIX INT or TERM Signal
---------------------------------

You can cleanly stop :program:`mongod` using a ``SIGINT`` or ``SIGTERM``
signal on UNIX-like systems. Either ``^C`` for a non-daemon
:program:`mongod` instance, ``kill -2 <pid>``, or ``kill -15 <pid>`` will
cleanly terminate the :program:`mongod` instance.

Terminating a :program:`mongod` instance that is **not** running with
:term:`journaling <journal>` with ``kill -9 <pid>`` (i.e. ``SIGKILL``)
will probably cause data corruption.

To recover data in situations where :program:`mongod` instances have
not terminated cleanly *without* :term:`journaling <journal>` see
:doc:`/tutorial/recover-data-following-unexpected-shutdown`.