I followed the official MongoDB documentation for stopping with signals. One of the following commands can be used (PID represents the Process ID
of the mongod
process):
kill PID
which sends signal 15 (SIGTERM), or
kill -2 PID
which sends signal 2 (SIGINT).
Warning from MongoDB documentation:
Never use kill -9
(i.e. SIGKILL) to terminate a mongod
instance.
If you have more than one instance running or you don't care about the PID, you could use pkill
to send the signal to all running mongod
processes:
pkill mongod
or
pkill -2 mongod
or, much more safer, only to the processes belonging to you:
pkill -U $USER mongod
or
pkill -2 -U $USER mongod
NOTE:
If the DB is running as another user, but you have administrative rights, you have invoke the above commands with sudo
, in order to run them. E.g.:
sudo pkill mongod
sudo pkill -2 mongod
PS
Note: I resorted to using signals because mongod --shutdown
, although mentioned in the current MongoDB documentation, did not work on my machine (macOS, mongodb v3.4.10, installed with homebrew
):
Error parsing command line: unrecognised option '--shutdown'
Update 2022-05-10
meanwhile option --shutdown
is marked in the documentation as "Supported on Linux only
".
PPS
(macOS specific) Before anyone wonders: no, I could not stop it with command
brew services stop mongodb
because I did not start it with
brew services start mongodb
I had started mongod
with a custom command line :-)