【发布时间】:2013-03-26 15:28:17
【问题描述】:
我尝试在 MongoDB 中测试分片。例如,我使用 host1.com 和 host2.com 代替真实的服务器名称。
所以我在 host1.com 创建了配置服务器:
mongod --dbpath /path/to/configdb/ --configsvr
在同一台机器上启动mongos:
mongos --configdb host1.com --port 27020
并在两台机器(host1.com 和 host2.com)上启动 mongod:
mongod --dbpath /path/to/test_shard_db/ --shardsvr
我添加了分片,启用了数据库test 和集合test 的分片,分片键为{'name': 1}(集合只有这个字段和_id 用于测试),如教程中所述。但在所有这些操作之后,我的所有数据都只写入一个分片,这是数据库的主要分片。
这里是配置:
分片状态:
mongos> db.printShardingStatus()
--- Sharding Status ---
sharding version: { "_id" : 1, "version" : 3 }
shards:
{ "_id" : "shard0000", "host" : "host1.com:27018", "maxSize" : NumberLong(1) }
{ "_id" : "shard0001", "host" : "host2.com:27018", "maxSize" : NumberLong(10) }
databases:
...
{ "_id" : "test", "partitioned" : true, "primary" : "shard0000" }
test.test chunks:
shard0001 1
{ "name" : { $minKey : 1 } } -->> { "name" : { $maxKey : 1 } } on : shard0001 Timestamp(1000, 0)
收藏统计:
mongos> db.printCollectionStats()
test
{
"sharded" : false,
"primary" : "shard0000",
"size" : 203535788,
...
}
平衡器状态:
mongos> sh.isBalancerRunning()
true
那么,为什么我添加了超过 1 兆字节的数据,但集合中的所有数据都只驻留在一个分片中?为什么db.printCollectionStats() 告诉我test 数据库"sharded" : false。我做错了什么?
【问题讨论】:
-
默认块大小为 64MB,因此在拆分发生之前您有增长空间。您可以事先自己拆分分片键范围,这样可以从一开始就允许写入到多个分片。有关详细信息,请参阅以下内容:docs.mongodb.org/manual/tutorial/…
-
@James Wahlin,所以分片 maxSize 和 chunkSize 不相关?
-
maxSize 将限制给定分片上的数据量。当达到时,平衡器将寻找将块移动到未达到 maxSize 的分片。块是文档的集合,它们都属于分片键范围的一部分。 MongoDB 平衡器将在块级别在分片之间移动数据以进行平衡。当一个块接近 maxSize 值时,它将被分成 2 块,这可能会导致移动。
-
您有
"primary" : "shard0000"和test.test chunks: shard0001 1用于状态输出这一事实验证主分片在0000 上,而单个分片在0001 上。另一种仔细检查的方法是验证是否存在索引超过name字段:如果您的收藏为空,则会自动创建。但是,如果它不为空,则不会发生分片 -
@James Wahlin,谢谢!!您可以发布答案,我会接受。