MongoDB 中的分片 [英] Sharding in MongoDB

查看:60
本文介绍了MongoDB 中的分片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在 MongoDB 中测试分片.例如,我使用 host1.com 和 host2.com 而不是真实的服务器名称.

I try to test sharding in MongoDB. For example, I use host1.com and host2.com instead real server names.

所以我在 host1.com 创建了配置服务器:

So I created config server at host1.com:

mongod --dbpath /path/to/configdb/ --configsvr

在同一台机器上启动mongos:

mongos --configdb host1.com --port 27020

并在两台机器(host1.com和host2.com)上启动mongod:

And started mongod at two machines (host1.com and host2.com):

mongod --dbpath /path/to/test_shard_db/ --shardsvr

我添加了分片,使用分片键 {'name': 1} 为数据库 test 和集合 test 启用分片(集合只有这个字段和 _id 用于测试),如教程中所述.但在所有这些操作之后,我的所有数据只写入一个分片,这是数据库的主要分片.

I added shards, enabled sharding for database test and collection test with shard key {'name': 1} (collection has only this field and _id for test) as explained in tutorial . But after all this operations all my data writes only to one shard, which is primary for database.

这里是配置:

分片状态:

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.我做错了什么?

So why all data in collection reside only at one shard though I added more than 1 megabyte of data? And why db.printCollectionStats() show me that test database "sharded" : false. What I did wrong?

推荐答案

默认块大小为 64MB,因此在发生拆分之前您有增长空间.您可以事先自己拆分分片键范围,这样可以从一开始就允许写入到多个分片.请参阅 MongoDB 拆分块文档了解更多信息.

The default chunk size is 64MB so you have room to grow before a split will occur. You can split the shard key range yourself beforehand which can allow writes to go to multiple shards from the start. See the MongoDB Split Chunks documentation for more info.

关于chunk size和maxSize的区别:

On the difference between chunk size and maxSize:

maxSize 将限制给定分片上的数据量.当达到时,平衡器将寻找将块移动到未达到 maxSize 的分片.块是所有属于分片键范围的一部分的文档的集合.MongoDB 平衡器将在块级别的分片之间移动数据以进行平衡.当一个块接近 maxSize 值时,它会被分成 2 块,这可能会导致移动.

maxSize will limit the volume of data on a given shard. When reached the balancer will look to move chunks to a shard where maxSize has not been reached. A chunk is a collection of documents that all fall within a section of the shard key range. The MongoDB balancer will move data between shards at the chunk level to balance. When a chunk approaches the maxSize value, it will be split into 2 which may result in a move.

这篇关于MongoDB 中的分片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆