Iris Classon
Iris Classon - In Love with Code

Installation of mongodb on windows as a service with powershell

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],
ServiceCommandException
+ 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
 
@"
systemLog:
destination: file
path: C:\Mongo\data\log\mongod.log
storage:
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 http://downloads.mongodb.org/win32/mongodb-win32-x86_64-2008plus-ssl-v3.2-latest-signed.msi?_ga=1.56414827.19723263.1478030430 -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
:Piwr https://chocolatey.org/install.ps1 -UseBasicParsing | iexchoco install mongodb -y 

Comments

Leave a comment below, or by email.
Pjeroo
11/13/2016 3:38:54 PM
, create this folder manually. MongoDB won t create it for you. You can also specify an alternate data directory with 
Iris Classon
11/14/2016 6:20:06 AM
Reply to: Pjeroo
Ah! Thank you for sharing that! 


Last modified on 2016-11-02

comments powered by Disqus