Skip to content

Instantly share code, notes, and snippets.

@nohwnd
Created March 27, 2016 09:00
Show Gist options
  • Save nohwnd/7de0ed814bdc0de16edb to your computer and use it in GitHub Desktop.
Save nohwnd/7de0ed814bdc0de16edb to your computer and use it in GitHub Desktop.
Rough script to quickly start cluster of local mongoDb instances, for experimenting with elections and sharding.
function Add-MongoToPath () {
$mongoPath = "C:\Program Files\MongoDB\Server\3.2\bin"
if ($env:Path -notlike "*$mongoPath*")
{
$env:path += ";$mongoPath"
}
}
function Start-ServerInstace ([string]$ComputerName = "localhost", [int]$Port, [string]$ReplicationSet = "rs0", [string]$DatabasePath) {
$process = Start-Process Powershell.exe -PassThru -ArgumentList "-NoProfile", "-Command ""
mongod --bind_ip $ComputerName --port $Port --dbpath $DatabasePath --replSet $ReplicationSet |
foreach { `$host.ui.rawui.WindowTitle = ""'${ComputerName}:$Port -- ' + `$_""; ""`$_"" | Out-Default }
"" "
[PsCustomObject]@{
ComputerName = $ComputerName
Port=$Port
Process = $process
}
}
Add-MongoToPath
$script:instances = @()
1..10 | foreach {
$port = 27000 + $_
$dbPath = "C:\temp\data\db$_"
New-Item -ItemType Directory -Force -Path $dbPath | Out-Null
$script:instances += Start-ServerInstace -ComputerName localhost -Port $Port -DatabasePath $dbPath -ReplicationSet rs0
Start-Sleep -Seconds 2
if ($_ -eq 1)
{
mongo localhost:27001 -eval 'rs.initiate({_id:"rs0", members: [{"_id":1, "host":"localhost:27001"}]})'
}
else
{
mongo localhost:27001 -eval "rs.add('localhost:$port')"
}
}
Start-Process mongo -ArgumentList 'localhost:27001' -Wait
#do not use Stop-Process here it does not allow the db to perform it's cleanup
$script:instances | foreach {
if (-not $_.process.ExitTime) {
taskkill /pid $_.process.id
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment