Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save kamsar/98681b73979adc89610c to your computer and use it in GitHub Desktop.
Save kamsar/98681b73979adc89610c to your computer and use it in GitHub Desktop.
#
# MongoDB (as a windows service)
#
$mongoDbPath = "$env:SystemDrive\MongoDB"
$mongoDbConfigPath = "$mongoDbPath\mongod.cfg"
$url = "https://fastdl.mongodb.org/win32/mongodb-win32-x86_64-2008plus-3.2.7.zip"
$zipFile = "$mongoDbPath\mongo.zip"
$unzippedFolderContent ="$mongoDbPath\mongodb-win32-x86_64-2008plus-3.2.7"
if ((Test-Path -path $mongoDbPath) -eq $false)
{
Write-Host "Setting up directories..."
$temp = md $mongoDbPath
$temp = md "$mongoDbPath\log"
$temp = md "$mongoDbPath\data"
$temp = md "$mongoDbPath\data\db"
Write-Host "Setting up mongod.cfg..."
[System.IO.File]::AppendAllText("$mongoDbConfigPath", "dbpath=$mongoDbPath\data\db`r`n")
[System.IO.File]::AppendAllText("$mongoDbConfigPath", "logpath=$mongoDbPath\log\mongo.log`r`n")
[System.IO.File]::AppendAllText("$mongoDbConfigPath", "smallfiles=true`r`n")
Write-Host "Downloading MongoDB..."
$webClient = New-Object System.Net.WebClient
$webClient.DownloadFile($url,$zipFile)
Write-Host "Unblock zip file..."
Get-ChildItem -Path $mongoDbPath -Recurse | Unblock-File
Write-Host "Unzipping Mongo files..."
$shellApp = New-Object -com shell.application
$destination = $shellApp.namespace($mongoDbPath)
$destination.Copyhere($shellApp.namespace($zipFile).items())
Copy-Item "$unzippedFolderContent\*" $mongoDbPath -recurse
Write-Host "Cleaning up..."
Remove-Item $unzippedFolderContent -recurse -force
Remove-Item $zipFile -recurse -force
Write-Host "Installing Mongod as a service..."
& $mongoDBPath\bin\mongod.exe --config $mongoDbConfigPath --install
Write-Host "Starting Mongod..."
& net start mongodb
}
else {
Write-Host "MongoDB already installed."
}
@bienangelo
Copy link

This saved me from a lot of hassle. Thanks!

@GobindaMishra
Copy link

What If I want different version of MongoDB and want to set different port?

@crusaderky
Copy link

Works great! However it fails if I change to 4.2.6 :(

@marouane-miftah
Copy link

marouane-miftah commented Nov 8, 2021

Works great! However it fails if I change to 4.2.6 :(

I have update the code to work with version 4.4.
You can find it here: https://gist.github.com/marouane-miftah/acc497fc9681e8d907a421b73a7abdf3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment