How to stop mongodb server on Windows?

2019-02-08 14:36发布

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?

5条回答
Animai°情兽
2楼-- · 2019-02-08 15:02

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
查看更多
forever°为你锁心
3楼-- · 2019-02-08 15:07

I guess, using TASKKILL /IM mongod.exe is fine to terminate the mongodb server gracefully.

查看更多
干净又极端
4楼-- · 2019-02-08 15:11

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();
查看更多
唯我独甜
5楼-- · 2019-02-08 15:13

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

查看更多
\"骚年 ilove
6楼-- · 2019-02-08 15:21

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> 
查看更多
登录 后发表回答