2014-04-22 23 views
0

我成立了一個MongoDB的碎片羣在Windows首次作爲解釋在這裏:http://docs.mongodb.org/manual/tutorial/deploy-shard-cluster/的MongoDB會將所有數據發送到一個服務器在分片集羣

我用下面的腳本起牀配置服務器,mongos實例和mongo數據服務器。

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; 
} 

$mongodPath = "..\mongod.exe" 
$mongosPath = "..\mongos.exe" 
$cnfgServers = @(
    (get-cnfgServerObj -port 20001 -path "..\data\configdb20001"), 
    (get-cnfgServerObj -port 20002 -path "..\data\configdb20002"), 
    (get-cnfgServerObj -port 20003 -path "..\data\configdb20003") 
) 

$dataServers = @(
    (get-cnfgServerObj -port 27001 -path "..\data\shdb1"), 
    (get-cnfgServerObj -port 27002 -path "..\data\shdb2") 
) 

# Create the mongo config servers first 
$cnfgServers | foreach { 

    if((Test-Path $_.Path) -eq $false) 
    { 
     New-Item -Path $_.Path -ItemType Directory 
    } 

    $args = "--configsvr --dbpath $($_.Path) --port $($_.Port)" 
    start-process $mongodPath $args -windowstyle Normal 
} 

# Create the mongo servers 
$dataServers | foreach { 

    if((Test-Path $_.Path) -eq $false) 
    { 
     New-Item -Path $_.Path -ItemType Directory 
    } 

    $args = "--dbpath $($_.Path) --port $($_.Port)" 
    start-process $mongodPath $args -windowstyle Normal 
} 

# Create the mongos instances 
$mongosArgs = "--configdb localhost:20001,localhost:20002,localhost:20003" 
start-process $mongosPath $mongosArgs -windowstyle Normal 

我運行上面的腳本後,我連接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 }) 

最後,我在mongo shell(它連接到mongos實例)中運行以下注釋:

use foo 

// init data 
for (var i = 1; i <= 2500; i++) db.testData.insert({ x : i }) 

後來,我連接到我的數據服務器中的一個:

mongo --host localhost --port 27001 

當我嘗試看看TESTDATA集合內的文件的數量,我看到這個號碼是2500,其代表所有的計數文檔:

use foo 
db.testData.count() 

簡單地說,數據在所有分片中都沒有負載平衡。我在這裏做錯了什麼?

編輯:

這裏是sh.status()命令的結果:

--- Sharding Status --- 
    sharding version: { 
     "_id" : 1, 
     "version" : 3, 
     "minCompatibleVersion" : 3, 
     "currentVersion" : 4, 
     "clusterId" : ObjectId("535673272850501ad810ff51") 
} 
    shards: 
     { "_id" : "shard0000", "host" : "localhost:27001" } 
     { "_id" : "shard0001", "host" : "localhost:27002" } 
    databases: 
     { "_id" : "admin", "partitioned" : false, "primary" : "config" } 
     { "_id" : "test", "partitioned" : false, "primary" : "shard0000" } 
     { "_id" : "foo", "partitioned" : true, "primary" : "shard0000" } 
       foo.testData 
         shard key: { "x" : 1, "_id" : 1 } 
         chunks: 
           shard0001  1 
           shard0000  1 
         { "x" : { "$minKey" : 1 }, "_id" : { "$minKey" : 1 } } - 
->> { "x" : 1, "_id" : ObjectId("53567411cb144434eb53f08d") } on : shard0001 Tim 
estamp(2, 0) 
         { "x" : 1, "_id" : ObjectId("53567411cb144434eb53f08d") 
} -->> { "x" : { "$maxKey" : 1 }, "_id" : { "$maxKey" : 1 } } on : shard0000 Tim 
estamp(2, 1) 

回答

4

我會假設你的分片羣集設置正確,你是正確連接到它(雖然我不知道你爲什麼要連接到一個mongod,而不是mongos,也許你插入到mongod,而不是mongos ?)

缺乏平衡的一個原因是您的密鑰是單調的,從最低到最高的線性範圍,因爲寫入將從範圍的開始處開始等等。

由於MongoDB sharding是基於範圍的,它實際上會將整個第一個範圍放在第一個分區上,直到它看起來適合實際平衡它:http://docs.mongodb.org/manual/tutorial/manage-sharded-cluster-balancer/這很可能不是這種情況。

從根本上解決這個問題的辦法是選擇你的片鍵:http://docs.mongodb.org/manual/tutorial/choose-a-shard-key/明智

+0

謝謝。所以,當它達到某個點(某些定義的大小等)時,mongo將開始在分片之間平衡數據,對嗎?我連接到一個mongod實例以查看文檔的數量。我通過mongos進行了寫作。 – tugberk

+0

@tugberk確實應該,嘗試插入200萬記錄 – Sammaye

+0

會做。那裏的「大塊」是極限嗎? – tugberk

1

我猜你是錯在這裏什麼都不做。 MongoDB只有64MB的默認塊大小,這意味着所有的數據都在一個塊中,因此將位於一個碎片中。

您可以通過issueing

db.settings.save({ _id:"chunksize", value: <sizeInMB> }) 

這也解釋in the docs修改塊大小。

+0

修改CHUNKSIZE是不是一個好主意,而不是正確的碎片關鍵應選擇 – Sammaye

+1

@Sammaye我不會說** *從來沒有*,這肯定有用例。我並不是想要改變生產塊的大小或任何嚴重的東西,但我認爲這樣做可以開始探索MongoDB並遊玩。 – dirkk

+0

的確,我想它沒有碰到這樣的:) – Sammaye

相關問題