简体   繁体   English

如何在 Windows 上停止 mongodb 服务器?

[英]How to stop mongodb server on Windows?

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.使用 taskkill 来停止 mongod 不是一种选择,因为这可能会导致数据损坏。 Any suggestions?有什么建议?

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最好的事物

  1. Open Command Prompt/Powershell as Administrator.以管理员身份打开命令提示符/Powershell。
  2. Execute "net stop MongoDB"执行“net stop MongoDB”

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.js file:我想出了以下代码: 将以下代码段保存在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

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();

From the Windows command line.从 Windows 命令行。 I'm using MongoDB 3.4 Server on 64-bit Windows 7 Pro SP1.我在 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\3.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\3.4\bin\mongod.exe\" --service --c
onfig=\"C:\Program Files\MongoDB\Server\3.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\3.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> 

以管理员身份打开命令提示符

net stop mongodb

If you install mongodb as a service on windows.如果您在 Windows 上将 mongodb 作为服务安装。 you can stop the service using following command in elevated command prompt您可以在提升的命令提示符下使用以下命令停止服务

 net stop mongodb

我想,使用 TASKKILL /IM mongod.exe 可以优雅地终止 mongodb 服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM