Vitaly
Vitaly

Reputation: 4488

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?

Upvotes: 27

Views: 70726

Answers (8)

Rut Shah
Rut Shah

Reputation: 1278

  1. Open Command Prompt/Powershell as Administrator.

  2. Execute

    net stop MongoDB

Upvotes: 32

Shubham Khunt
Shubham Khunt

Reputation: 361

Open Command prompt as an administrator

net stop mongodb

Upvotes: 17

decoder
decoder

Reputation: 926

If you install mongodb as a service on windows. you can stop the service using following command in elevated command prompt

 net stop mongodb

Upvotes: 5

ChromaBurst
ChromaBurst

Reputation: 59

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> 

Upvotes: 5

JulienD
JulienD

Reputation: 3582

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

Upvotes: 36

Nanhe Kumar
Nanhe Kumar

Reputation: 16297

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

Upvotes: 12

Todor Bukov
Todor Bukov

Reputation: 156

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

Upvotes: 14

Vitaly
Vitaly

Reputation: 4488

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

Upvotes: 1

Related Questions