shutdown or db. shutdownServer() command was run, mongod only continues the shut down steps if the force field is true, or a. SIGTERM signal was sent to mongod , mongod always continues the shut down steps.
The best way to do this in Python is to write your database connection as a singleton and then use the 'atexit' module to to decorate a function that disconnects.
you can stop manually using "services. msc" from cmd and select the mongodb service in that list and stop it.
Starting and Stopping MongoDB is covered in the MongoDB manual. It explains the various options of stopping MongoDB through the shell, cli, drivers etc. It also details the risks of incorrectly stopping MongoDB (such as data corruption) and talks about the different kill signals.
Additionally, if you have installed MongoDB using a package manager for Ubuntu or Debian then you can stop mongodb (currently mongod in ubuntu) as follows:
Upstart: sudo service mongod stop
Sysvinit: sudo /etc/init.d/mongod stop
Or on Mac OS X
Find PID of mongod process using $ top
Kill the process by $ kill <PID>
(the Mongo docs have more info on this)
Or on Red Hat based systems:
service mongod stop
Or on Windows if you have installed as a service named MongoDB
:
net stop MongoDB
And if not installed as a service (as of Windows 7+) you can run:
taskkill /f /im mongod.exe
To learn more about the problems of an unclean shutdown, how to best avoid such a scenario and what to do in the event of an unclean shutdown, please see: Recover Data after an Unexpected Shutdown.
If you literally want a one line equivalent to the commands in your original question, you could alias:
mongo --eval "db.getSiblingDB('admin').shutdownServer()"
Mark's answer on starting and stopping MongoDB via services is the more typical (and recommended) administrative approach.
mongod --dbpath /path/to/your/db --shutdown
More info at official: http://docs.mongodb.org/manual/tutorial/manage-mongodb-processes/
If the server is running as the foreground process in a terminal, this can be done by pressing
Ctrl-C
Another way to cleanly shut down a running server is to use the shutdown command,
> use admin
> db.shutdownServer();
Otherwise, a command like kill can be used to send the signal. If mongod has 10014 as its PID, the command would be
kill -2 10014
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 usekill -9
(i.e. SIGKILL) to terminate amongod
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 this option, because mongod --shutdown
, although mentioned in the current MongoDB documentation, curiously doesn't work on my machine (macOS, mongodb v3.4.10, installed with homebrew
):Error parsing command line: unrecognised option '--shutdown'
PPS
(macOS specific) Before anyone wonders: no, I could not stop it with commandbrew services stop mongodb
because I did not start it withbrew services start mongodb
.
I had started mongod
with a custom command line :-)
Use mongod --shutdown
According to the official doc : manage-mongodb-processes/
:D
create a file called mongostop.bat
save the following code in it
mongo admin --eval "db.shutdownServer()"
run the file mongostop.bat and you successfully have mongo stopped
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With