I need to be able to start/stop MongoDB on the cli. It is quite simple to start:
./mongod
But to stop mongo DB, I need to run open mongo shell first and then type two commands:
$ ./mongo
use admin
db.shutdownServer()
So I don’t know how to stop mongo DB in one line. Any help?
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 stopSysvinit:
sudo /etc/init.d/mongod stopOr on Mac OS X
Find PID of mongod process using
$ topKill the process by
$ kill <PID>(the Mongo docs have more info on this)Or on Red Hat based systems:
service mongod stopOr on Windows if you have installed as a service named
MongoDB:net stop MongoDBAnd if not installed as a service (as of Windows 7+) you can run:
taskkill /f /im mongod.exeTo 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.