我成立了一个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)
谢谢。所以,当它达到某个点(某些定义的大小等)时,mongo将开始在分片之间平衡数据,对吗?我连接到一个mongod实例以查看文档的数量。我通过mongos进行了写作。 – tugberk
@tugberk确实应该,尝试插入200万记录 – Sammaye
会做。那里的“大块”是极限吗? – tugberk