ITKeyword - 技术文章推荐分享

首页 > mongodb - How to stop mongo DB in one command

mongodb - How to stop mongo DB in one command

标签: mongodb,

相关推荐:mongodb - How to create mapping in elasticsearch for index generated by mongo-connector

the MongoDB database and collection names as animals.kitten.I tried to create mapping as:$ curl -XPUT 'http://localhost:9200/animals.kitten/_mapping' -d '{ "animals.kitten" : { "properties" : { "name" : {"type"

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

3,872921

asked Aug 2 '12 at 9:43

XiaoYao

53221118



 | 

7 Answers

7

-------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

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

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 at 18:12

Alex P

4,0072722

answered Aug 2 '12 at 11:45

Mark Hillick

3,872921

3

 

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 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 at 10:20



 | 

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.


 |
  this answer

answered Aug 2 '12 at 12:10

Stennie

30.3k46994

  

 

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 at 8:11



 | 

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

622812

  

 

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

5

 

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 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 s

相关推荐:mongodb - How to execute mongo shell command from javascript

indow, I'd like to periodically run a script that will display various stats on the database activity, before displaying the stats, I'd like to clear the screen. There is a "cls" command in the mongo shell, but I am not able to execute it f

hutdown 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

4,64512229

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



 | 

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

To start the service use: NET START MONGODB

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,313102945

answered Apr 16 '13 at 13:55

Pirira

111

  

 

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.

– Dc01_xx

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 simply did:

quit();

Please note I'm using mongo 3.0.

Mongo


 |
  this answer

answered Dec 25 '15 at 18:42

Felipe Alarcon

500413



 | 

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,1811826

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

  

 

Good ksh.

Thank you!

– XiaoYao

Aug 3 '12 at 8:20



 | 

相关推荐:mongodb - How to list all collections in the mongo shell

mongodb share|improve this question edited Apr 19 '15 at 10:21 Salvador Dali

var ados = ados || {}; ados.run = ados.run || []; ados.run.push(function () { ados_add_placement(22,8277,"adzerk1983168263",4).setZone(43); }); ...

------分隔线----------------------------