How to stop mongo DB in one command

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.


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.


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

Tags:

Mongodb