Last active
August 29, 2015 14:00
-
-
Save tugberkugurlu/11176205 to your computer and use it in GitHub Desktop.
MongoDB Development Sharding Cluster Setup with PowerShell. Info about the cluster: http://docs.mongodb.org/manual/tutorial/deploy-shard-cluster/ and http://stackoverflow.com/questions/23220843/mongodb-routes-all-data-to-one-server-in-a-shard-cluster
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## TODO: Execute the necessary javascript code on mongo servers for setting up the sharding and replications | |
## TODO: Take a param for type (e.g. All, Config, Shard, Mongos). | |
## TODO: Take a param fof installing as a service or firing up the exes. | |
param( | |
[string]$startDir | |
) | |
function get-cnfgServerObj($port, $path) { | |
$firstCnfgSrv = New-Object PSObject | |
$firstCnfgSrv | Add-Member Noteproperty -Name Port -value $port | |
$firstCnfgSrv | Add-Member Noteproperty -Name Path -value $path | |
return $firstCnfgSrv | |
} | |
function Get-BaseName { | |
$fileLabel = "mongodb" | |
return "$($fileLabel)-$((get-date).ToString("yyyyMMddHHmmss"))" | |
} | |
function Download-File($url, $targetFile) { | |
"Downloading $url" | |
$uri = New-Object "System.Uri" "$url" | |
$request = [System.Net.HttpWebRequest]::Create($uri) | |
$request.set_Timeout(15000) #15 second timeout | |
$response = $request.GetResponse() | |
$totalLength = [System.Math]::Floor($response.get_ContentLength()/1024) | |
$responseStream = $response.GetResponseStream() | |
$targetStream = New-Object -TypeName System.IO.FileStream -ArgumentList $targetFile, Create | |
$buffer = new-object byte[] 10KB | |
$count = $responseStream.Read($buffer,0,$buffer.length) | |
$downloadedBytes = $count | |
while ($count -gt 0) | |
{ | |
[System.Console]::CursorLeft = 0 | |
[System.Console]::Write("Downloaded {0}K of {1}K", [System.Math]::Floor($downloadedBytes/1024), $totalLength) | |
$targetStream.Write($buffer, 0, $count) | |
$count = $responseStream.Read($buffer,0,$buffer.length) | |
$downloadedBytes = $downloadedBytes + $count | |
} | |
"`nFinished Download" | |
$targetStream.Flush() | |
$targetStream.Close() | |
$targetStream.Dispose() | |
$responseStream.Dispose() | |
} | |
$shouldAddFirewallRules = $false | |
$mongoDBDir = $null | |
$mongoDBBinDir = $null | |
$zipToDownloadUrl = "https://fastdl.mongodb.org/win32/mongodb-win32-x86_64-2008plus-2.6.0.zip" | |
if($startDir -ne '') { | |
$mongoDBDir = $startDir | |
$startBinDir = (Join-Path $startDir "bin") | |
if(((Test-Path $startBinDir) -eq $false)) { | |
$zipFileName = "$(Get-BaseName)-mongodb-2_6_0.zip" | |
$zipFileFullPath = "$env:TEMP\$zipFileName" | |
$pathToUnzip = "$(Get-BaseName)-mongodb-2_6_0" | |
$fullPathToUnzip = "$env:TEMP\$pathToUnzip" | |
New-Item -Path $fullPathToUnzip -ItemType Directory | Out-Null | |
# Download the zip | |
Download-File $zipToDownloadUrl $zipFileFullPath | |
# Unzip the downloaded zip file | |
$shellApp = new-object -com shell.application | |
$zipFile = $shellApp.namespace($zipFileFullPath) | |
$destinationToUnzip = $shellApp.namespace($fullPathToUnzip) | |
$destinationToUnzip.Copyhere($zipFile.items()) | |
$tempMongoDbDir = (Join-Path $fullPathToUnzip "mongodb-win32-x86_64-2008plus-2.6.0") | |
$tempMongoDbBinDir = (Join-Path $tempMongoDbDir "bin") | |
New-Item -Path $startBinDir -ItemType Directory | Out-Null | |
Move-Item (Join-Path $tempMongoDbBinDir "*") $startBinDir | |
Remove-Item -Path $zipFileFullPath | |
Remove-Item -Path $fullPathToUnzip -Recurse | |
$shouldAddFirewallRules = $true | |
} | |
$mongoDBBinDir = $startBinDir | |
} | |
else { | |
$zipFileName = "$(Get-BaseName)-mongodb-2_6_0.zip" | |
$zipFileFullPath = "$env:TEMP\$zipFileName" | |
$pathToUnzip = "$(Get-BaseName)-mongodb-2_6_0" | |
$fullPathToUnzip = "$env:TEMP\$pathToUnzip" | |
New-Item -Path $fullPathToUnzip -ItemType Directory | Out-Null | |
# Download the zip | |
Download-File $zipToDownloadUrl $zipFileFullPath | |
# Unzip the downloaded zip file | |
$shellApp = new-object -com shell.application | |
$zipFile = $shellApp.namespace($zipFileFullPath) | |
$destinationToUnzip = $shellApp.namespace($fullPathToUnzip) | |
$destinationToUnzip.Copyhere($zipFile.items()) | |
Write-Host "MongoDB Path: $fullPathToUnzip" | |
Remove-Item -Path $zipFileFullPath | |
$shouldAddFirewallRules = $true | |
$mongoDBDir = (Join-Path $fullPathToUnzip "mongodb-win32-x86_64-2008plus-2.6.0") | |
$mongoDBBinDir = (Join-Path $mongoDBDir "bin") | |
} | |
$mongoRoot = $mongoDBBinDir | |
$dataPath = Join-Path $mongoRoot "data" | |
$mongodPath = Join-Path $mongoRoot "mongod.exe" | |
$mongosPath = Join-Path $mongoRoot "mongos.exe" | |
$cnfgServers = @( | |
(get-cnfgServerObj -port 20001 -path (Join-Path $dataPath "configdb20001")), | |
(get-cnfgServerObj -port 20002 -path (Join-Path $dataPath "configdb20002")), | |
(get-cnfgServerObj -port 20003 -path (Join-Path $dataPath "configdb20003")) | |
) | |
$dataServers = @( | |
(get-cnfgServerObj -port 27001 -path (Join-Path $dataPath "shdb1")), | |
(get-cnfgServerObj -port 27002 -path (Join-Path $dataPath "shdb2")) | |
) | |
$mongosServerPorts = @( | |
) | |
if($shouldAddFirewallRules -eq $true) { | |
New-NetFirewallRule -Name "mongod.exe" -DisplayName "mongod.exe on $mongodPath" ` | |
-Protocol UDP ` | |
-Direction Inbound ` | |
-Profile Private ` | |
-Program $mongodPath ` | |
-Action Allow | Out-Null | |
New-NetFirewallRule -Name "mongod.exe" -DisplayName "mongod.exe on $mongodPath" ` | |
-Protocol TCP ` | |
-Direction Inbound ` | |
-Profile Private ` | |
-Program $mongodPath ` | |
-Action Allow | Out-Null | |
New-NetFirewallRule -Name "mongos.exe" -DisplayName "mongos.exe on $mongosPath" ` | |
-Protocol UDP ` | |
-Direction Inbound ` | |
-Profile Private ` | |
-Program $mongosPath ` | |
-Action Allow | Out-Null | |
New-NetFirewallRule -Name "mongos.exe" -DisplayName "mongos.exe on $mongosPath" ` | |
-Protocol TCP ` | |
-Direction Inbound ` | |
-Profile Private ` | |
-Program $mongosPath ` | |
-Action Allow | Out-Null | |
} | |
# Create the mongo config servers first | |
$cnfgServers | foreach { | |
if((Test-Path $_.Path) -eq $false) | |
{ | |
New-Item -Path $_.Path -ItemType Directory | Out-Null | |
} | |
$args = "--configsvr --dbpath $($_.Path) --port $($_.Port)" | |
Write-Host "Starting mongod on $mongodPath with the following args: $args" | |
start-process $mongodPath $args -windowstyle Normal | |
} | |
# Create the mongo servers | |
# TODO: Set up each data server as a replication set | |
$dataServers | foreach { | |
if((Test-Path $_.Path) -eq $false) | |
{ | |
New-Item -Path $_.Path -ItemType Directory | Out-Null | |
} | |
$args = "--dbpath $($_.Path) --port $($_.Port)" | |
Write-Host "Starting mongod on $mongodPath with the following args: $args" | |
start-process $mongodPath $args -windowstyle Normal | |
} | |
# Create the mongos instances | |
# TODO: Create at least two mongos instances | |
$mongosArgs = "--configdb localhost:20001,localhost:20002,localhost:20003" | |
Write-Host "Starting mongos on $mongosPath with the following args: $mongosArgs" | |
start-process $mongosPath $mongosArgs -windowstyle Normal |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// connect to mongos | |
mongo --host localhost --port 27017 | |
// add servers as shards | |
sh.addShard("localhost:27001") | |
sh.addShard("localhost:27002") | |
// Enable sharding on 'foo' db | |
sh.enableSharding("foo") | |
// Enable sharding on 'testData' collection of 'foo' database | |
sh.shardCollection("foo.testData", { "x": 1, "_id": 1 }) | |
// init data | |
for (var i = 1; i <= 2500; i++) db.testData.insert( { x : i } ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment