228

How can I set up MongoDB so it can run as a Windows service?

Boann
  • 48,794
  • 16
  • 117
  • 146
heisthedon
  • 3,657
  • 3
  • 21
  • 24

29 Answers29

386

After trying for several hours, I finally did it.

Make sure:

  1. you added the <MONGODB_PATH>\bin directory to the system variable PATH
  2. run command prompt as administrator

Steps:

step 1: execute this command:

D:\mongodb\bin>mongod --remove

Step 2: execute this command after opening command prompt as administrator:

D:\mongodb\bin>mongod --dbpath=D:\mongodb --logpath=D:\mongodb\log.txt --install

NOTE: you can also append --serviceName MongoDB after the command above.

That's All!


After that right there in the command prompt execute:
services.msc

// OR

net start MongoDB

And look for MongoDB service and click start.


NOTE: Make sure to run command prompt as administrator.

If you don't do this, your log file (D:\mongodb\log.txt in the above example) will contain lines like these:

2016-11-11T15:24:54.618-0800 I CONTROL  [main] Trying to install Windows service 'MongoDB'
2016-11-11T15:24:54.618-0800 I CONTROL  [main] Error connecting to the Service Control Manager: Access is denied. (5)

and if you try to start the service from a non-admin console, (i.e. net start MongoDB or Start-Service MongoDB in PowerShell), you'll get a response like this:

System error 5 has occurred.
Access is denied.

or this:

Start-Service : Service 'MongoDB (MongoDB)' cannot be started due to the following error: Cannot open MongoDB service
on computer '.'.
At line:1 char:1
+ Start-Service MongoDB
+ ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Start-Service],
   ServiceCommandException
    + FullyQualifiedErrorId : CouldNotStartService,Microsoft.PowerShell.Commands.StartServiceComman
Eddy
  • 3,623
  • 37
  • 44
hyeomans
  • 4,522
  • 5
  • 24
  • 29
  • 12
    You can also start it with `net start MongoDB` instead of using `services.msc`. (`mongod` recommended that to me.) – Joe May 01 '14 at 18:02
  • 1
    This worked for me. The path to the log file has to be created manually if it doesn't already exist (I used a path other than the mongo directory in the example). – dgundersen Feb 10 '15 at 15:18
  • At first, Mongo started as a service, but my database was empty. I could only see my previously existing database by adding \data to the path : mongod --dbpath=D:\mongodb\data – Jeremy Thille Aug 12 '15 at 10:50
  • "Make sure to run command prompt as administrator" - it did the job! Running with normal permission does not write any warning, but service is just not created. Permissions, permissions, permissions! – Dzoukr Oct 22 '15 at 11:25
  • neither of the top 2 answers will keep the service running on reboot - see instead my answer below – danday74 Dec 10 '16 at 16:19
  • `mongod --remove` was key for me, since I had an old install location, and `--install` didn't override with the new location on it's own. – Nyaarium May 07 '18 at 17:39
  • 1
    Should be the validated answer – Logan Wlv Apr 16 '19 at 08:15
123

I think if you run it with the --install command line switch, it installs it as a Windows Service.

mongod --install

It might be worth reading this thread first though. There seems to be some problems with relative/absolute paths when the relevant registry key gets written.

Ry-
  • 218,210
  • 55
  • 464
  • 476
Stephen Curran
  • 7,433
  • 2
  • 31
  • 22
  • 7
    i finally managed to install by execute this: mongod.exe --service – heisthedon Mar 15 '10 at 19:40
  • 3
    It seems like what you really need to do is enter the full path at the command line: c:\> c:\mongodb\bin\mongod.exe --service. However, make sure to run the command window as administrator (especially on windows server) otherwise you'll be thwarted by UAC. – JC Grubbs May 19 '10 at 19:47
  • 2
    this gist can be time saving for devleopment machine setups https://gist.github.com/serdarb/5102848 – Serdar Apr 24 '13 at 12:20
  • 3
    And after you run with --install, you'll have to `net start MongoDB` – John Mar 11 '15 at 00:21
  • this answer may be out of date. – helt Mar 19 '16 at 12:19
  • Like @JCGrubbs said if you run as a regular user the command completes quietly without any errors but does not add a service. If you remember that, and follow these https://docs.mongodb.com/manual/tutorial/install-mongodb-on-windows/#install-the-mongodb-service you'll be fine. – yǝsʞǝla Jun 29 '16 at 01:44
  • 17
    It requires a logfile path, you can add the dbpath and logfile path like this mongod --dbpath="c:\data\db" --logpath="c:\data\db\log.txt" --install , After this you need to run net start MongoDB. – sivaram636 Oct 18 '16 at 09:46
  • do i need to do it per path of data? i start mongo, with hello world, and the tutorial were not just about mongo so the detail were a little. the way it did it, said go to the same path as you did, then generate a fol;ter and used --path command to send data of a collection to there... now should we lunch it per collection specifying the path? for example you lunch two application on server? – Hassan Faghihi Apr 13 '17 at 16:16
  • 1
    The answer should specify that you need to run `mongod --install` as an Administrator, otherwise it fails silently. As others have stated, you also need to `run net start MongoDB`. – Kesarion May 27 '19 at 10:53
65

not only --install,

also need --dbpath and --logpath

and after reboot OS you need to delete "mongod.lock" manually

SoftSan
  • 2,482
  • 3
  • 23
  • 54
ruby
  • 671
  • 5
  • 7
64

Unlike other answers this will ..

START THE SERVICE AUTOMATICALLY ON SYSTEM REBOOT / RESTART

MongoDB Install

Windows

(1) Install MongoDB

(2) Add bin to path

(3) Create c:\data\db

(4) Create c:\data\log

(5) Create c:\data\mongod.cfg with contents ..

systemLog:
    destination: file
    path: c:\data\log\mongod.log
storage:
    dbPath: c:\data\db

(6) To create service that will auto start on reboot .. RUN AS ADMIN ..

sc.exe create MongoDB binPath= "\"C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe\" --service --config=\"C:\data\mongod.cfg\"" DisplayName= "MongoDB" start= "auto"

(7) Start the service .. RUN AS ADMIN ..

net start MongoDB

IMPORTANT: Even if this says 'The MongoDB service was started successfully' it can fail

To double check open Control Panel > Services, ensure the status of the MongoDB service is 'Running'

If not, check your log file at C:\data\log\mongod.log for the reason for failure and fix it

(Do not start MongoDB via Control Panel > Services, use .. net start MongoDB)

(8) Finally, restart your machine with MongoDB running and it will still be running on restart

If you ever want to kill it ..

net stop MongoDB

sc.exe delete MongoDB
danday74
  • 52,471
  • 49
  • 232
  • 283
  • thanks it worked for Me , I just had to change `3.4` to `3.6` version in this command `sc.exe create MongoDB binPath= "\"C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe\" --service --config=\"C:\data\mongod.cfg\"" DisplayName= "MongoDB" start= "auto"` – guru_007 Dec 21 '17 at 17:48
33

The below steps apply to Windows.

Run below in an administrative cmd

mongod --remove

This will remove the existing MongoDB service (if any).

mongod --dbpath "C:\data\db" --logpath "C:\Program Files\MongoDB\Server\3.4\bin\mongod.log" --install --serviceName "MongoDB"

Make sure that C:\data\db folder exists

Open services with:

services.msc

Find MongoDB -> Right click -> Start

Zameer Ansari
  • 28,977
  • 24
  • 140
  • 219
21

You can use the command below for running mongodb as a windows service

"C:\mongodb\bin\mongod" --bind_ip  yourIPadress  --logpath  "C:\data\dbConf\mongodb.log"  --logappend  --dbpath  "C:\data\db"  --port yourPortNumber --serviceName "YourServiceName" --serviceDisplayName "YourServiceName" --install 

If you use mongodb with default parameters, you can use these values :

  • yourIPadress : 127.0.0.1 or localhost
  • yourPortNumber : 27017 (default port) or dont put --port
  • serviceDisplayName : only if you run more than one service (since mongodb 1.8)

There's more information on this command here

http://www.mongodb.org/display/DOCS/Windows+Service

chafank
  • 389
  • 2
  • 8
17

I tried all answers and then did it the way https://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/#configure-a-windows-service-for-mongodb-community-edition describes it.

Use a config file...

"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install

helt
  • 4,925
  • 3
  • 35
  • 54
14

This PowerShell Script Downloads and Installs MongoDB as Windows Service:

Set-ExecutionPolicy RemoteSigned

$mongoDbPath = "C:\MongoDB" 
$mongoDbConfigPath = "$mongoDbPath\mongod.cfg"
$url = "http://downloads.mongodb.org/win32/mongodb-win32-x86_64-2008plus-2.4.9.zip" 
$zipFile = "$mongoDbPath\mongo.zip" 
$unzippedFolderContent ="$mongoDbPath\mongodb-win32-x86_64-2008plus-2.4.9"

if ((Test-Path -path $mongoDbPath) -eq $True) 
{ 
  write-host "Seems you already installed MongoDB"
    exit 
}

md $mongoDbPath 
md "$mongoDbPath\log" 
md "$mongoDbPath\data" 
md "$mongoDbPath\data\db"

[System.IO.File]::AppendAllText("$mongoDbConfigPath", "dbpath=C:\MongoDB\data\db`r`n")
[System.IO.File]::AppendAllText("$mongoDbConfigPath", "logpath=C:\MongoDB\log\mongo.log`r`n")
[System.IO.File]::AppendAllText("$mongoDbConfigPath", "smallfiles=true`r`n")
[System.IO.File]::AppendAllText("$mongoDbConfigPath", "noprealloc=true`r`n")

$webClient = New-Object System.Net.WebClient 
$webClient.DownloadFile($url,$zipFile)

$shellApp = New-Object -com shell.application 
$destination = $shellApp.namespace($mongoDbPath) 
$destination.Copyhere($shellApp.namespace($zipFile).items())

Copy-Item "$unzippedFolderContent\*" $mongoDbPath -recurse

Remove-Item $unzippedFolderContent -recurse -force 
Remove-Item $zipFile -recurse -force

& $mongoDBPath\bin\mongod.exe --config $mongoDbConfigPath --install

& net start mongodb
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Serdar
  • 1,416
  • 2
  • 17
  • 43
11

This was the only thing that worked for me. As everything had to be an absolute path:

C:\Program Files\MongoDB\Server\3.2\bin>mongod --install --dbpath=c:/data/db --logpath=c:/data/logs/log.txt

I also had to run it from admin cmd

Jamie Hutber
  • 26,790
  • 46
  • 179
  • 291
7

The recommended way mongod --install results in error:

2015-12-03T18:18:28.896+0100 I CONTROL  --install has to be used with a log file for server output

After having installed mongodb in C:\mongodb you need to simply add the logpath:

mongod --install --logpath C:\mongodb\logs\mongo.log

The path to the logfile must exist and it must be an absolute Windows path. Then you start the MongoDB service by typing:

net start MongoDB
Peter G.
  • 7,816
  • 20
  • 80
  • 154
6

Working on Mongo DB: 3.6

(1) Install MongoDB

(2) Add bin to environment path variable

(3) Create c:\data\db

(4) Create c:\data\mongod.log

(5) Run below Command on bin folder

.\mongod.exe --install --logpath c:\data\mongod.log --logappend --bind_ip 12 7.0.0.1 --dbpath c:\data\db

(6) To start mongo db as service

net start MongoDB

(7) Finally run mongo in command line to check mongo shell is open or not.

Zahidur Rahman
  • 1,688
  • 2
  • 20
  • 30
  • Works also with mongod version 4.2.8. Thanks! – Metafaniel Sep 04 '20 at 04:51
  • In step 5, if this error is thrown: `F CONTROL [main] Failed global initialization: FileNotOpen: Failed to open "C:\ProgramData\MongoDB\log\mongod.log"` all that is needed is to delete `mongod.log` file – Metafaniel Sep 23 '20 at 05:34
5

Consider using

mongod --install --rest --master
javierlinked
  • 553
  • 6
  • 6
4

For version 2.4.3 (current version as of posting date), create a config file and then execute the following:

C:\MongoDB\bin\mongod.exe --config C:\MongoDB\mongod.cfg --service

iisystems
  • 1,089
  • 1
  • 11
  • 12
4
1) echo logpath=F:\mongodb\log\mongo.log > F:\mongodb\mongod.cfg

2) dbpath=F:\mongodb\data\db [add this to the next line in mongod.cfg]

C:\>F:\mongodb\bin\mongod.exe –config F:\mongodb\mongod.cfg –install

Reference

Hassan Amir Khan
  • 645
  • 8
  • 13
4

This is what worked for me:

sc.exe create MongoDB binPath= "d:\MongoDB\bin\mongod.exe --service --config=d:\MongoDB\bin\mongod.config" displayname= "MongoDB 2.6 Standard" start= "auto"

escaping the binPath was failing for me as described in the Mongo documentation

Failed:

sc.exe create MongoDB binPath= "\"C:\Program Files\MongoDB 2.6 Standard\bin\mongod.exe\" --service --config=\"C:\Program Files\MongoDB 2.6 Standard\mongod.cfg\"" DisplayName= "MongoDB 2.6 Standard" start= "auto"
Enkode
  • 4,515
  • 4
  • 35
  • 50
  • 1
    Run cmd as administrator and run this command: sc.exe create MongoDB binPath= "d:\MongoDB\bin\mongod.exe --service --config=d:\MongoDB\bin\mongod.config" displayname= "MongoDB 2.6 Standard" start= "auto" – Akash5288 Sep 03 '14 at 10:14
4

The simplest way is,

  1. Create folder C:\data\db
  2. Create file C:\data\db\log.txt
  3. Open command prompt as "Run as Administrator" and make sure the mogodb bin directory path is correct and write

    C:\Program Files\MongoDB\Server\3.4\bin> mongod.exe --install mongod --dbpath="c:\data\db" --logpath="c:\data\db\log.txt" 
    
  4. Start mongodb service:

    net run MongoDB
    
Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
  • 'C:\Program' is not recognized as an internal or external command, operable program or batch file. – 151291 Jun 22 '18 at 04:44
4

Working on MongoDB 3.4 [Windows]

  • Create dir C:/mongodb/data
  • Create a file in C:/mongodb/mongodb.config using this configuration:

    storage:
        engine: wiredTiger
        dbPath: "C:/mongodb/data"
        directoryPerDB: true
        journal:
            enabled: true
    systemLog:
        destination: file
        path: "C:/mongodb/data/mongod.log"
        logAppend: true
        timeStampFormat: iso8601-utc
    net:
        bindIp: 127.0.0.1
        port: 27017
        wireObjectCheck : false
    
  • To install MongoDb as a service, run this command in powershell with admin power

    mongod --config="C:\mongodb\mongodb.config" --install --service

  • Open Services.msc and look for MongoDb, then start it

Marco Fantasia
  • 720
  • 8
  • 12
3

Currently (up to version 2.4.4), if any path (dbpath/logpath/config) contains spaces, then the service won't start, and show the error: "The service is not responding to the control function".

RoeeK
  • 1,112
  • 12
  • 23
2

I'm on version 2.4.9 and using a config file. The service wouldn't start until I surrounded the equals sign in the config file with spaces:

dbpath = D:\Mongo data
logpath = C:\mongodb\logs\mongo.log 
logappend = true

Originally I had:

logpath=C:\mongodb\logs\mongo.log 

I also discovered that when installing the service that you have to use an absolute path for the config file eg:

c:\mongodb\bin\>mongodb.exe C:\mongodb\bin\mongod.conf --install

Don't be tempted to put inverted commas around a dbpath with spaces. The service will appear to start when you execute net start MongoDB but it will terminate. Check the log files for confirmation that the service has really started.

CrispinH
  • 1,899
  • 4
  • 23
  • 38
2

Run "cmd.exe" as administrator and then run "sc.exe" to add a new Windows service.

for example:

sc.exe create MongoDB binPath= "c:\program files\mongodb\server\3.2\bin\mongod.exe"
Terry Lin
  • 2,529
  • 22
  • 21
2

I found that you should pass : dbpath , config and logfile to mongod with the install flag

example :

mongod --dbpath=c:\data\db --config=c:\data\db\config.cfg --logpath=c:\data\db\log.txt --install 

note : I have mongod path in my path variable .

You can control the service with :

net start mongodb
net stop  mongodb 
Ahmed Eid
  • 4,414
  • 3
  • 28
  • 34
2

These are the steps to install MongoDB as Windows Service :

  1. Create a log directory, e.g. C:\MongoDB\log

  2. Create a db directory, e.g. C:\MongoDB\db

  3. Prepare a configuration file with following lines

    dbpath=C:\MongoDB\db

    logpath=C:\MongoDB\log

    Place the configuration file with name mongod.cfg in folder "C:\MongoDB\"

  4. Following command will install the Windows Service on your sc.exe create MongoDB binPath= "\"C:\MongoDB\Server\3.4\bin\mongod.exe\" --service --config=\"C:\MongoDB\mongod.cfg\" DisplayName= "MongoDB 3.4" start= "auto"

  5. Once you run this command, you will get the [SC] CreateService SUCCESS

  6. Run following command on Command Prompt

    net start MongoDB

RITZ XAVI
  • 3,633
  • 1
  • 25
  • 35
C.P. Soni
  • 41
  • 2
1

If you install MongoDB 2.6.1 or newer using the MSI download from an Administrator Command Prompt, a service definition should automatically be created for you.

The MongoDB documentation also has a tutorial to help you Manually Create a Windows Service definition if needed.

Stennie
  • 63,885
  • 14
  • 149
  • 175
1

This answer is for those who have already installed mongo DB using MSI installer.

Let's say your default installed location is "C:\Program Files\MongoDB\Server\3.2\bin"

Steps to run mongo as a window service

  1. Open command prompt as administrator
  2. Type cd C:\Program Files\MongoDB\Server\3.2\bin (check path properly, as you may have a different version installed, and not 3.2).
  3. Press enter
  4. Type net start MongoDB
  5. Press enter
  6. Press Windows + R, type services.msc and check if Mongo is running as a service.
CDspace
  • 2,639
  • 18
  • 30
  • 36
Abhas Bhoi
  • 349
  • 3
  • 3
  • Have correct path but getting error : `System error 2 has occurred. The system cannot find the file specified.` tried mongod , mongodb as well. – 151291 Jun 22 '18 at 04:42
1

I just had to restart the MongoDB (v4.4) service after editing the config file on a Windows box. Here's what I did:

  1. Press Win+R to open the Run panel
  2. Type in "services.msc" and press Enter
  3. Search for "MongoDB" - you can press "m" to jump to it.
  4. Right click - select "Restart"

Thats it!

TJBlackman
  • 1,895
  • 3
  • 20
  • 46
0
mongod --config "C:\Program Files\MongoDB\Server\3.6\mongod_primary.cfg" --install --serviceName "MongoDB_Primary" --serviceDisplayName "MongoDB Primary"
Gehan Fernando
  • 1,221
  • 13
  • 24
0
  1. check windows services

    if you have service for mongo remove it by run bellow command
    mongod --remove

  2. create mongo.cfg file with bellow content

    systemLog:
    destination: file
    path: c:\data\log\mongod.log
    storage:
    dbPath: c:\data\db

    path: where you want to store log datas
    dbPath: your database directory

  3. then run bellow command

    sc.exe create MongoDB binPath= "\"C:\Program Files\MongoDB\Server\3.2\bin\mongod.exe\" --service --config=\"C:\Program Files\MongoDB\Server\3.2\mongod.cfg\"" DisplayName= "MongoDB" start= "auto"

    binPath : mongodb installation directory
    config: .cfg file address
    DisplayName:Your Service Name

  4. start service

    net start MongoDB

now every things are done . enjoy that

Ahmad Hamzavi
  • 626
  • 6
  • 18
0

In my case, I create the mongod.cfg beside the mongd.exe with the following contents.

# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# Where and how to store data.
storage:
  dbPath: D:\apps\MongoDB\Server\4.0\data
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path:  D:\apps\MongoDB\Server\4.0\log\mongod.log

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0


#processManagement:

#security:

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options:

#auditLog:

#snmp:

Then I run either the two command to create the service.

D:\apps\MongoDB\Server\4.0\bin>mongod --config D:\apps\MongoDB\Server\4.0\bin\mongod.cfg --install

D:\apps\MongoDB\Server\4.0\bin>net stop mongodb
The MongoDB service is stopping.
The MongoDB service was stopped successfully.


D:\apps\MongoDB\Server\4.0\bin>mongod --remove
2019-04-10T09:39:29.305+0800 I CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2019-04-10T09:39:29.309+0800 I CONTROL  [main] Trying to remove Windows service 'MongoDB'
2019-04-10T09:39:29.310+0800 I CONTROL  [main] Service 'MongoDB' removed

D:\apps\MongoDB\Server\4.0\bin>
D:\apps\MongoDB\Server\4.0\bin>sc.exe create MongoDB binPath= "\"D:\apps\MongoDB\Server\4.0\bin\mongod.exe\" --service --config=\"D:\apps\MongoDB\Server\4.0\bin\mongod.cfg\""
[SC] CreateService SUCCESS

D:\apps\MongoDB\Server\4.0\bin>net start mongodb
The MongoDB service is starting..
The MongoDB service was started successfully.

D:\apps\MongoDB\Server\4.0\bin>

The following are not correct, note the escaped quotes are required.

D:\apps\MongoDB\Server\4.0\bin>sc.exe create MongoDB binPath= "D:\apps\MongoDB\Server\4.0\bin\mongod --config D:\apps\MongoDB\Server\4.0\bin\mongod.cfg"
[SC] CreateService SUCCESS

D:\apps\MongoDB\Server\4.0\bin>net start mongodb
The service is not responding to the control function.

More help is available by typing NET HELPMSG 2186.


D:\apps\MongoDB\Server\4.0\bin>
Donghua Liu
  • 1,776
  • 2
  • 21
  • 30
-1

Plz try the steps in Mongo DB: 3.6 & Windows 10

  1. mongod --remove
  2. mongod --dbpath=C:/data/db --port 27017 --logpath C:/data/log/log.txt --service
  3. mongod --dbpath=C:/data/db --port 27017 --logpath C:/data/log/log.txt --install
  4. net start MongoDB
Tính Ngô Quang
  • 4,400
  • 1
  • 33
  • 33
  • 1
    MongoDB 4.0 runs fine on Windows 64-bit ... UNLESS you try securing it by adding the "authorization: enabled" line to the "Security" setting in mongod.cfg. Then it results in windows reporting an error message "the service did not start in a timely manner". Any idea what's happening here? – David Edwards Jul 16 '19 at 00:18