2014-04-22 18 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

相关问题