mongodb 如何在一个命令中停止 mongo DB

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11774887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 12:46:35  来源:igfitidea点击:

How to stop mongo DB in one command

mongodb

提问by XiaoYao

I need to be able to start/stop MongoDB on the cli. It is quite simple to start:

我需要能够在 cli 上启动/停止 MongoDB。开始很简单:

./mongod

./mongod

But to stop mongo DB, I need to run open mongo shell first and then type two commands:

但是要停止 mongo DB,我需要先运行 open mongo shell,然后输入两个命令:

$ ./mongo

use admin

db.shutdownServer()

$ ./mongo

使用管理员

db.shutdownServer()

So I don't know how to stop mongo DB in one line. Any help?

所以我不知道如何在一行中停止 mongo DB。有什么帮助吗?

回答by Mark Hillick

Starting and Stopping MongoDBis 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.

MongoDB 手册中介绍了启动和停止 MongoDB。它解释了通过 shell、cli、驱动程序等停止 MongoDB 的各种选项。它还详细介绍了错误停止 MongoDB(例如数据损坏)的风险,并讨论了不同的终止信号。

Additionally, if you have installed MongoDB using a package manager for Ubuntuor Debianthen you can stop mongodb (currently mongod in ubuntu) as follows:

此外,如果您已经使用UbuntuDebian的包管理器安装了 MongoDB,那么您可以按如下方式停止 mongodb(目前在 ubuntu 中为 mongod):

  • Upstart: sudo service mongod stop

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

  • 暴发户: sudo service mongod stop

  • 系统维尼特: sudo /etc/init.d/mongod stop

Or on Mac OS X

或者在Mac OS X 上

Or on Red Hatbased systems:

或者在基于Red Hat的系统上:

  • service mongod stop
  • service mongod stop

Or on Windowsif you have installed as a service named MongoDB:

或者在Windows 上,如果您已安装为名为 的服务MongoDB

  • net stop MongoDB
  • net stop MongoDB

And if not installed as a service (as of Windows 7+) you can run:

如果未安装为服务(从 Windows 7+ 开始),您可以运行:

  • taskkill /f /im mongod.exe
  • 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.

要了解有关不正常关机的问题、如何最好地避免这种情况以及在发生不正常关机时该怎么做的更多信息,请参阅:意外关机后恢复数据

回答by Stennie

If you literally want a one line equivalent to the commands in your original question, you could alias:

如果您确实想要一行与原始问题中的命令等效的命令,则可以使用别名:

mongo --eval "db.getSiblingDB('admin').shutdownServer()"

mongo --eval "db.getSiblingDB('admin').shutdownServer()"

Mark's answer on starting and stopping MongoDB via services is the more typical (and recommended) administrative approach.

Mark 对通过服务启动和停止 MongoDB 的回答是更典型(和推荐)的管理方法。

回答by francoisrv

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

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

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

更多官方信息:http: //docs.mongodb.org/manual/tutorial/manage-mongodb-processes/

回答by Nanhe Kumar

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,

干净地关闭正在运行的服务器的另一种方法是使用 shutdown 命令,

> 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 这样的命令来发送信号。如果 mongod 的 PID 为 10014,则命令为

kill -2 10014

回答by t0r0X

I followed the official MongoDB documentationfor stopping with signals. One of the following commands can be used (PID represents the Process IDof the mongodprocess):

我按照官方的MongoDB 文档来停止信号。一个下面的命令,可以使用的(PID表示Process ID的的mongod处理):

kill PID

which sends signal 15 (SIGTERM), or

发送信号 15(SIGTERM),或

kill -2 PID

which sends signal 2 (SIGINT).

它发送信号 2 (SIGINT)。

Warning from MongoDB documentation:
Never use kill -9(i.e. SIGKILL) to terminate a mongodinstance.

来自MongoDB 文档的警告:
切勿使用kill -9(即 SIGKILL)终止mongod实例。

If you have more than one instance running or you don't care about the PID, you could use pkillto send the signal to all running mongodprocesses:

如果您有多个实例在运行,或者您不关心 PID,则可以使用pkill将信号发送到所有正在运行的mongod进程:

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

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'

PS
注意:我采用了这个选项,因为mongod --shutdown虽然在当前的MongoDB 文档中提到过,但奇怪的是在我的机器上不起作用(macOS,mongodb v3.4.10,安装了homebrew):
Error parsing command line: unrecognised option '--shutdown'

PPS
(macOS specific) Before anyone wonders: no, I could not stop it with command
brew services stop mongodb
because I did not start it with
brew services start mongodb.
I had started mongodwith a custom command line :-)

PPS
(特定于 macOS) 在任何人怀疑之前:不,我无法使用命令停止它,
brew services stop mongodb
因为我没有以
brew services start mongodb.
我从mongod自定义命令行开始:-)

回答by crifan

My special case is:

我的特殊情况是:

previously start mongod by:

以前通过以下方式启动 mongod:

sudo -u mongod mongod -f /etc/mongod.conf

now, want to stop mongod.

现在,想停下来mongod

and refer official doc Stop mongod Processes, has tried:

并参考官方文档Stop mongod Processes,尝试过:

(1) shutdownServerbut failed:

(1)shutdownServer但失败:

> use admin
switched to db admin
> db.shutdownServer()
2019-03-06T14:13:15.334+0800 E QUERY    [thread1] Error: shutdownServer failed: {
        "ok" : 0,
        "errmsg" : "shutdown must run from localhost when running db without auth",
        "code" : 13
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype.shutdownServer@src/mongo/shell/db.js:302:1
@(shell):1:1

(2) --shutdownstill failed:

(2)--shutdown仍然失败:

# mongod --shutdown
There doesn't seem to be a server running with dbpath: /data/db

(3) previous start command adding --shutdown:

(3) 之前的启动命令添加--shutdown:

sudo -u mongod mongod -f /etc/mongod.conf --shutdown
killing process with pid: 30213
failed to kill process: errno:1 Operation not permitted

(4) use serviceto stop:

(4) 使用service停止:

service mongod stop

and

service mongod status

show expected Active: inactive (dead)but mongodactually stillrunning, for can see process from ps:

显示预期Active: inactive (dead)mongod实际上仍在运行,因为可以从 ps 中看到进程:

# ps -edaf | grep mongo | grep -v grep
root     30213     1  0 Feb04 ?        03:33:22 mongod --port PORT --dbpath=/var/lib/mongo

and finally, really stop mongod by:

最后,真的被停止的mongod:

# sudo mongod -f /etc/mongod.conf --shutdown
killing process with pid: 30213

until now, root cause: still unknown ...

直到现在,根本原因:仍然未知......

hope above solution is useful for your.

希望以上解决方案对您有用。

回答by Shawn Wang

Use mongod --shutdown

mongod --shutdown

According to the official doc : manage-mongodb-processes/

根据官方文档:manage-mongodb-processes/

:D

:D

回答by Adeojo Emmanuel IMM

create a file called mongostop.bat

创建一个名为 mongostop.bat 的文件

save the following code in it

将以下代码保存在其中

 mongo admin --eval "db.shutdownServer()"

run the file mongostop.bat and you successfully have mongo stopped

运行文件 mongostop.bat 并成功停止 mongo

回答by Pirira

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

使用命令行启动或停止 mongodb 服务的一个衬垫;

  1. To start the service use: NET START MONGODB
  2. To stop the service use: NET STOP MONGODB
  1. 要启动服务使用: NET START MONGODB
  2. 停止服务使用: NET STOP MONGODB

I use this myself, it does work.

我自己用这个,确实有效。

回答by ranafeb14

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

根据给定的命令,我认为您使用的是 Linux。

Start MongoDB:

启动MongoDB:

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

Check the Status:

检查状态:

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

Stop MongoDB:

停止 MongoDB:

$ sudo service mongod stop
mongod stop/waiting