简体   繁体   中英

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.

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

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?

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.

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

Best

  1. Open Command Prompt/Powershell as Administrator.
  2. Execute "net stop MongoDB"

From the mongo shell documentation :


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

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:

 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,

> use admin
> db.shutdownServer();

From the Windows command line. I'm using MongoDB 3.4 Server on 64-bit Windows 7 Pro SP1.

The following links explain the steps:

configure-a-windows-service-for-mongodb-community-edition

manually-create-a-windows-service-for-mongodb-community-edition

The following console command line terminates MongoDB at start-up:

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:

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. you can stop the service using following command in elevated command prompt

 net stop mongodb

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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