如何在 Windows 上停止 mongodb 服务器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7973113/
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
How to stop mongodb server on Windows?
提问by Vitaly
I'm using the nginx/PHP/MongoDB stack and trying to set up my development environment on Windows by creating two batch files.
我正在使用 nginx/PHP/MongoDB 堆栈并尝试通过创建两个批处理文件在 Windows 上设置我的开发环境。
start.bat
启动文件
cd C:\www\nginx
start nginx
cd C:\www\php
start php-cgi.exe -b 127.0.0.1:9000
cd C:\www\mongodb
start mongod
stop.bat
停止.bat
cd C:\www\nginx
nginx -s quit
cd C:\www\php
taskkill /F /IM php-cgi.exe
cd C:\www\mongodb
mongo --eval "use admin; db.shutdownServer(); quit" # this doesn't work
mongo --eval stop_mongod.js # this doesn't work either
Using taskkill to stop mongod isn't an option, since that may lead to data corruption. Any suggestions?
使用 taskkill 来停止 mongod 不是一种选择,因为这可能会导致数据损坏。有什么建议?
采纳答案by Todor Bukov
From the mongo shell documentation:
从 mongo shell文档:
use dbname
使用数据库名
This command does not work in scripted mode. Instead you will need to explicitly define the database in the connection (/dbname in the example above).
此命令在脚本模式下不起作用。相反,您需要在连接中显式定义数据库(上例中的 /dbname)。
Alternately, you can also create a connection within the script:
或者,您也可以在脚本中创建连接:
db2 = connect("server:27017/otherdbname")
I've come up with the following code: Save the following snippet in stop_mongod.jsfile:
我想出了以下代码: 将以下代码段保存在stop_mongod.js文件中:
db = connect("localhost:27017/admin");
db.shutdownServer();
quit();
Adjust the connection string if necessary. Then from the command line or within your batch script:
如有必要,调整连接字符串。然后从命令行或批处理脚本中:
mongo stop_mongod.js
回答by JulienD
I'm not sure that's a proper way to do, sending a kill could probably cause damage to your mongo server, and you'll need to repair your database after in case of crash.
我不确定这是一个正确的方法,发送杀死可能会对您的 mongo 服务器造成损坏,并且您需要在崩溃后修复您的数据库。
Maybe you already solved this question but here is what I do :
也许你已经解决了这个问题,但这是我所做的:
mongo admin --eval "db.shutdownServer()"
I'm automatically connected on the admin's collection and next, I just have to run the extinction.
我自动连接到管理员的集合,接下来,我只需要运行灭绝。
Here is the official link about how to stop Mongodb properly : http://api.mongodb.org/wiki/current/Starting%20and%20Stopping%20Mongo.html
这是关于如何正确停止 Mongodb 的官方链接:http://api.mongodb.org/wiki/current/Starting%20and%20Stopping%20Mongo.html
Best
最好的事物
回答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();
回答by Rut Shah
- Open Command Prompt/Powershell as Administrator.
- Execute "net stop MongoDB"
- 以管理员身份打开命令提示符/Powershell。
- 执行“net stop MongoDB”
回答by ChromaBurst
From the Windows command line. I'm using MongoDB 3.4 Server on 64-bit Windows 7 Pro SP1.
从 Windows 命令行。我在 64 位 Windows 7 Pro SP1 上使用 MongoDB 3.4 服务器。
The following links explain the steps:
以下链接解释了这些步骤:
configure-a-windows-service-for-mongodb-community-edition
配置-a-windows-service-for-mongodb-community-edition
manually-create-a-windows-service-for-mongodb-community-edition
手动创建windows-service-for-mongodb-community-edition
The following console command line terminates MongoDB at start-up:
以下控制台命令行在启动时终止 MongoDB:
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Windows\System32>"C:\Program Files\MongoDB\Server.4\bin\mongod.exe" --remove
2017-12-06T08:15:47.883-0600 I CONTROL [main] Trying to remove Windows service 'MongoDB'
2017-12-06T08:15:47.884-0600 I CONTROL [main] Service 'MongoDB' removed
The MongoDB service create command line (shown below) is located in the Windows registry here:
MongoDB 服务创建命令行(如下所示)位于 Windows 注册表中:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\MongoDB
To check, do the following:
要检查,请执行以下操作:
C:\Windows\System32>sc.exe create MongoDB binPath= "\"C:\Program Files\MongoDB\Server.4\bin\mongod.exe\" --service --c
onfig=\"C:\Program Files\MongoDB\Server.4\mongod.cfg\"" DisplayName= "MongoDB" start= "auto"
[SC] CreateService SUCCESS
C:\Windows\System32>net start MongoDB
The MongoDB service is starting.
The MongoDB service was started successfully.
C:\Windows\System32>net stop MongoDB
The MongoDB service is stopping.
The MongoDB service was stopped successfully.
C:\Windows\System32>"C:\Program Files\MongoDB\Server.4\bin\mongod.exe" --remove
2017-12-06T08:16:36.504-0600 I CONTROL [main] Trying to remove Windows service 'MongoDB'
2017-12-06T08:16:36.505-0600 I CONTROL [main] Service 'MongoDB' removed
C:\Windows\System32>
回答by Vitaly
I guess, using TASKKILL /IM mongod.exe is fine to terminate the mongodb server gracefully.
我想,使用 TASKKILL /IM mongod.exe 可以优雅地终止 mongodb 服务器。