ITKeyword,专注技术干货聚合推荐

注册 | 登录

解决mongodb - How to stop mongo DB in one command

相关推荐:mongodb - How to execute a mongo command in sh file

sh I receive bin/db.sh: line 2: mongod: command not found in console what is wrong there #!/bin/shmongod --dbpath db --rest --jsonp; mongodb termi

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?

mongodb
|
  this question
edited Aug 2 '12 at 14:12 Mark Hillick 4,332 10 22 asked Aug 2 '12 at 9:43 XiaoYao 682 2 13 19

 | 

8 Answers
8

-------Accepted-------Accepted-------Accepted-------

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 mongod as follows:

  • Upstart: sudo service mongod stop

  • Sysvinit: sudo /etc/init.d/mongod stop

Or on Mac OS X

Or on Red Hat based systems:

  • service mongod stop

Or on Windows if you have installed as a service named MongoDB:

  • net stop MongoDB

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.


|
  this answer
edited Feb 27 '16 at 18:12 Alex P 4,337 2 9 23 answered Aug 2 '12 at 11:45 Mark Hillick 4,332 10 22 4   I believe the order for service is sudo service mongodb [start|stop|restart|status]. sudo service stop mongodb results in: stop: unrecognized service –  jacob Jul 12 '13 at 16:21 2   @Jacob - We just tried this on ubuntu, you're right, I've edited the answer. –  Andrew M Oct 15 '13 at 12:35      For Mac, you can also just do 'killall mongod'. That way, you don't need to know the PID. –  Jon M Mar 20 '16 at 14:00      One additional warning might help: If you started mongod in the foreground, do NOT use CTRL-C to shut it down, since this may lead to lost data. –  DrMickeyLauer Mar 27 '16 at 10:20 1   Or on Mac: 'ps -ax | grep mongod', then kill the process number –  Bryan Grace Nov 4 '16 at 5:34  |  show more comment

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.

相关推荐:mongodb - Meteor how to retrieve an array of questions from the mongo db and displaying just one of them within the app

Meteor.js. I followed the discover Meteor Guide book and rebuilding their example microscope project into a quiz. I am currently struggling with retri


|
  this answer
answered Aug 2 '12 at 12:10 Stennie 34.6k 6 80 108      Great answer. When setting up multiple instances on development machine (trying the replication set before deployment) it helps to have a .bat start C:\mongodb\bin\mongod.exe --config C:\net2\primary1-pc\mongod.cfg start C:\mongodb\bin\mongod.exe --config C:\net2\secondary1-pc\mongod.cfg ... and then have shutdown.bat C:\mongodb\bin\mongo.exe --eval "db.getSiblingDB('admin').shutdownServer()" --port 27017 C:\mongodb\bin\mongo.exe --eval "db.getSiblingDB('admin').shutdownServer()" --port 27018 C:\mongodb\bin\mongo.exe --eval "db.getSiblingDB('admin').shutdownServer()" --port 27019 ... –  Willmore Feb 1 '16 at 8:11      On Windows where it's installed as a service, I found that net stop MongoDB wasn't reliable, so I used something similar: mongo admin --eval "db.shutdownServer()" . Cheers! –  NateJ Aug 9 '16 at 21:25      @Stennie When I use the 'net stop MongoDB' on windows it is showing "System Error 1067 has occured" –  charan tej Mar 1 at 5:52

 | 

mongod --dbpath /path/to/your/db --shutdown

More info at official: http://docs.mongodb.org/manual/tutorial/manage-mongodb-processes/


|
  this answer
answered Mar 16 '14 at 13:56 francoisrv 872 11 12      I love using this way, it works without dbpath too, makes sense if you only have one instance running. –  radtek May 27 '14 at 3:47 1   it seems it is not working for newer versions, from 2.4.xx to latest. –  alexserver Jun 7 '14 at 6:20 8   this did not work on mac. –  chovy Jul 18 '14 at 1:48      This only works on linux systems –  Phil Vollhardt Aug 9 '14 at 2:28      This is the only answer that I've seen that actually worked. –  David Betz Feb 1 '16 at 4:32

 | 

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

|
  this answer
answered Feb 20 '14 at 13:26 Nanhe Kumar 5,697 2 27 33 1   Using kill isn't a good idea, this might lead to a broken DB, which needs a repair for restart. "Never use kill -9 (i.e. SIGKILL) to terminate a mongod instance." Docs: docs.mongodb.org/manual/tutorial/manage-mongodb-processes/… –  Jens A. Koch Dec 15 '14 at 20:18 2   @JensA.Koch Ya, Nanhe wasn't telling anyone to use a -9 signal, so.. saying "using kill isn't a good idea" simply isn't accurate. Only instant termination is unsafe - a normal kill is perfectly safe and the documentation you linked to advocates it. –  B T Jul 23 '16 at 8:10

 | 

One liners to start or stop mongodb service using command line;

  1. To start the service use: NET START MONGODB
  2. To stop the service use: NET STOP MONGODB

I use this myself, it does work.


|
  this answer
edited Apr 16 '13 at 14:12 Hugo Dozois 4,775 10 31 52 answered Apr 16 '13 at 13:55 Pirira 11 1      I get this on using those commands on windows: c:\mongodb\bin>NET START MONGODB The service name is invalid. More help is available by typing NET HELPMSG 2185. c:\mongodb\bin>NET STOP MONGODB The service name is invalid. More help is available by typing NET HELPMSG 2185. –  user471450 Jun 15 '13 at 4:24      On windows you first have to install mongodb as a service using the --install option. The commands you can use a command similar to the following to install it as a service. mongod --install --directoryperdb --dbpath C:\mongodb-win32-x86_64-2.4.6\data --logpath C:\mongodb-win32-x86_64-2.4.6\log\mongodb.log --logappend --rest The command above must be run with administrator privileges. After that is done you can then use the net start and net stop commands provided by Hugo. –  Ed Ost Aug 31 '13 at 19:41

 | 

I use this startup script on Ubuntu.

#!/bin/sh

### BEGIN INIT INFO
# Provides:     mongodb
# Required-Sart:
# Required-Stop:
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description: mongodb
# Description: mongo db server
### END INIT INFO

. /lib/lsb/init-functions

PROGRAM=/opt/mongo/bin/mongod
MONGOPID=`ps -ef | grep 'mongod' | grep -v grep | awk '{print $2}'`

test -x $PROGRAM || exit 0

case "$1" in
  start)
     log_begin_msg "Starting MongoDB server"
         ulimit -v unlimited.
         ulimit -n 100000
     /opt/mongo/bin/mongod --fork --quiet --dbpath /data/db --bind_ip 127.0.0.1 --rest   --config /etc/mongod.conf.
     log_end_msg 0
     ;;
  stop)
     log_begin_msg "Stopping MongoDB server"
     if [ ! -z "$MONGOPID" ]; then
kill -15 $MONGOPID
     fi
     log_end_msg 0
     ;;
  status)
     ;;
  *)
     log_success_msg "Usage: /etc/init.d/mongodb {start|stop|status}"
     exit 1
esac

exit 0

|
  this answer
answered Aug 2 '12 at 9:51 Boris Ivanov 2,601 18 29 2   So you just kill mongoDB directly,right? I'm a beginner of MongoDB, is there any hurts to kill it? I know for SQLServer or Oracle, they will meet problems if we just kill them. –  XiaoYao Aug 2 '12 at 9:54      to kill is -9 but -15 u send SIGNAL. –  Boris Ivanov Aug 2 '12 at 10:14

 | 

I simply did:

quit();

Please note I'm using mongo 3.0.

Mongo


|
  this answer
answered Dec 25 '15 at 18:42 Felipe Alarcon 518 4 14

 | 

From the given commands I think you're on Linux.

Start MongoDB:

$ sudo service mongod start
mongod start/running, process XXXXX 

Check the Status:

$ sudo service mongod status
mongod start/running, process XXXXX 

Stop MongoDB:

$ sudo service mongod stop
mongod stop/waiting 

|
  this answer
answered Sep 14 '16 at 19:45 ranafeb14 129 1 6

 | 

相关推荐:mongodb - how to use findAndModify() function in mongo

priority: 5 , started: false , done: false , startTime: null , endTime: null }, { _id : 2 , title: "Become a Billionair


相关阅读排行


用户评论

游客

相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。