Nov 022016
 November 2, 2016  Posted by at 2:29 pm Databases, PowerShell  Add comments


Yesterday ‘the’ day finally came. The day I got the A-Okay to set up a document database for our system. While I certainly appreciate MS SQL and alike there are different types of databases to cover different types of scenarios. I had not so discreetly made a plea for using a document database for our logging, and when I was tasked with the backend implementation of our user feedback service we all quickly agreed that a schema-less database could benefit us as the data we would send with each feedback item would vary.

It feels like aaaaages since I last used MongoDB but I do remember that you could get an instance up and running in no time what so ever. And that remains true. For a quick and dirty fully automated install the script below would do it, of course with your own preferences for install location and so on.

A few notes!

I first gave Chocolatey a try, but it wouldn’t install MongoDB even though the dowload would run to completion with no failures. Huh. Powershell to the rescue! Script for Chocolatey is at the end of this post if you want to give it a go.

Some quick notes:
MongoDB expects both DB and log dir’s to exist, and do chuck in an empty log file as well. The service might refuse to run giving you the following not-helpful error:

PS C:\WINDOWS\system32> Start-Service -DisplayName “MongoDB”

Start-Service : Service ‘MongoDB (MongoDB)’ cannot be started due to the following error: Cannot start service MongoDB
on computer ‘.’.
At line:1 char:1
+ Start-Service -DisplayName “MongoDB”
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Start-Service],
+ FullyQualifiedErrorId : CouldNotStartService,Microsoft.PowerShell.Commands.StartServiceCommand

You probably messed up a path, the YAML in the config file or put in a bad argument list for the start service registration when you see the above. Go to services.msc and try starting the service from there to get an error number or a better description. And close services.msc before attempting to delete the ‘bad service’ as it will only be marked for deletion but not actually deleted until you do so and therefore subsequent attempts at registering a service by the same name will fail.

mkdir C:\Mongo\data\db
mkdir C:\Mongo\data\log
"" > C:\Mongo\data\log\mongod.log
# The minimum needed for a MongoDB config file
    destination: file
    path: C:\Mongo\data\log\mongod.log
    dbPath: C:\Mongo\data\db
"@	> "C:\Mongo\mongod.cfg"
# This is version 3.2, thus the 3.2 in the path for the binary in the new-service call
Invoke-WebRequest -OutFile mongodb.msi 
# /qn runs the install silently (with all defaults unless you specify otherwise)
start-process mongodb.msi /qn -Wait
new-service -Name "MongoDB" -binaryPathName '"C:\Program Files\MongoDB\Server\3.2\bin\mongod.exe" --service --config "C:\Mongo\mongod.cfg"' -DisplayName "MongoDB" -StartupType Automatic
Start-Service -DisplayName "MongoDB"

Installing Chocolatey and then MongoDB. Didn’t work for me, but here is how 😛

iwr -UseBasicParsing | iex
choco install mongodb -y

  2 Responses to “Installation of mongodb on windows as a service with powershell”

  1. , create this folder manually. MongoDB won t create it for you. You can also specify an alternate data directory with

Leave a Reply to Pjeroo Cancel reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>



What is 10 + 5 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)