【发布时间】:2017-07-31 15:48:19
【问题描述】:
我遇到了一个关于 MongoDB 分片的问题。 我的测试设置如下-
1.Application Server(1 server) - 我的应用程序在哪里运行。
2.MongoS & Router(1台服务器)
3.Two Shards-Primary shard 包含完整的 DB & Secondary shard 只是空白。
有一个名为“DEMO”的集合,其中包含以下数据-
"_id" : ObjectId("541c2df0e4b06af824c2c046"),
"country" : "INDIA",
"deviceType" : "manu-laptop",
"osVersion" : "patanahi",
"logtime" : {
"logtime" : ISODate("2014-09-19T13:21:52.596Z"),
"logtimeStr" : "19-09-2014 06:51:52",
"day" : 19,
"month" : 9,
"year" : 2014,
"hour" : 18,
"min" : 51,
"second" : 52
},
"countryId" : "511d0f28c3c4e5cc447c8dac"
有两个国家——印度和中国。 我已经分片了国家密钥。 我用来分片密钥的命令是
db.runCommand({shardcollection:"demo.db",key:{"country" : 1}});
但是当我在 Mongos 上运行负载时,它只会将数据保留在主分片上,而不会将其路由到第二个分片。
用例如下- 我想将印度数据保留在一个分片上,将中国数据保留在另一个分片上。 请帮忙。
设置完成,运行良好。
--- Sharding Status ---
sharding version: {
"_id" : 1,
"version" : 3,
"minCompatibleVersion" : 3,
"currentVersion" : 4,
"clusterId" : ObjectId("541bf31a8c554f2e2d4e1ad4")
}
shards:
{ "_id" : "shard0000", "host" : "xx.xx.xx.xx:27017", "tags" : [ "INDIA" ] }
{ "_id" : "shard0001", "host" : "xx.xx.xx.xx:27017", "tags" : [ "USA" ] }
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "demo", "partitioned" : true, "primary" : "shard0000" }
demo.device
shard key: { "country" : 1 }
chunks:
shard0001 1
shard0000 1
{ "country" : { "$minKey" : 1 } } -->> { "country" : "INDIA" } on : shard0001 Timestamp(2, 0)
{ "country" : "INDIA" } -->> { "country" : { "$maxKey" : 1 } } on : shard0000 Timestamp(2, 1)
demo.incoming_request_log
shard key: { "regionId" : 1 }
chunks:
shard0001 2
shard0000 3
{ "regionId" : { "$minKey" : 1 } } -->> { "regionId" : 0 } on : shard0001 Timestamp(2, 0)
{ "regionId" : 0 } -->> { "regionId" : 2 } on : shard0000 Timestamp(3, 1)
{ "regionId" : 2 } -->> { "regionId" : "0" } on : shard0000 Timestamp(2, 2)
{ "regionId" : "0" } -->> { "regionId" : "2" } on : shard0000 Timestamp(2, 4)
{ "regionId" : "2" } -->> { "regionId" : { "$maxKey" : 1 } } on : shard0001 Timestamp(3, 0)
tag: INDIA { "regionId" : "0" } -->> { "regionId" : "1" }
tag: USA { "regionId" : "2" } -->> { "regionId" : "3" }
{ "_id" : "demo;", "partitioned" : false, "primary" : "shard0001" }
【问题讨论】:
-
你应该看看基于标签的分片:docs.mongodb.org/manual/core/tag-aware-sharding
-
请张贴 sh.status() - 我怀疑你只有一个块,它还没有被分割。还应该注意的是,使用只有 2 个值的字段对于分片键来说是一个糟糕的选择,你只能有 2 个块(如果你需要 4 个分片来处理流量怎么办?),并且最终可能会超过最大值块大小也是如此,这有其自身的含义。如果您只想拆分查询/写入到不同服务器上的 2 组不同的数据,为什么不只使用 2 个数据库呢?
-
Adam Comerford - 仅用于测试目的。生产实现会有所不同。为了测试,我只需根据国家/地区将流量路由到分配的分片。
-
感谢 lalit-agarwal!tag-aware-sharding 为我工作。
标签: mongodb ubuntu amazon-ec2 sharding database