简体   繁体   English

在MongoDB中进行分片

[英]Sharding in MongoDB

I try to test sharding in MongoDB. 我尝试在MongoDB中测试分片。 For example, I use host1.com and host2.com instead real server names. 例如,我使用host1.com和host2.com而不是真实的服务器名称。

So I created config server at host1.com: 所以我在host1.com创建了配置服务器:

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

Started mongos at the same machine: 在同一台机器上启动mongos

mongos --configdb host1.com --port 27020

And started mongod at two machines (host1.com and host2.com): 并在两台机器(host1.com和host2.com)上启动了mongod

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

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 . 我添加了分片,使用分片键{'name':1}启用分片进行数据库test和集合test (集合只有此字段, _id用于测试),如教程中所述。 But after all this operations all my data writes only to one shard, which is primary for database. 但是在完成所有这些操作之后,我的所有数据都只写入一个分片,这是数据库的主要分片。

Here is config: 这是配置:

Sharding status: 分片状态:

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)

Collection stats: 收集统计:

mongos> db.printCollectionStats()
test
{
    "sharded" : false,
    "primary" : "shard0000",
    "size" : 203535788,
    ...
}

Balancer status: 平衡器状态:

mongos> sh.isBalancerRunning()
true

So why all data in collection reside only at one shard though I added more than 1 megabyte of data? 那么为什么集合中的所有数据只驻留在一个碎片中,虽然我添加了超过1兆字节的数据? And why db.printCollectionStats() show me that test database "sharded" : false . 为什么db.printCollectionStats()向我显示test数据库"sharded" : false What I did wrong? 我做错了什么?

The default chunk size is 64MB so you have room to grow before a split will occur. 默认的块大小为64MB,因此在分割发生之前您有增长的空间。 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. 有关详细信息,请参阅MongoDB Split Chunks文档

On the difference between chunk size and maxSize: 关于块大小和maxSize之间的区别:

maxSize will limit the volume of data on a given shard. maxSize将限制给定分片上的数据量。 When reached the balancer will look to move chunks to a shard where maxSize has not been reached. 到达平衡器时,将查看将块移动到尚未达到maxSize的分片。 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. MongoDB平衡器将在块级别的分片之间移动数据以平衡。 When a chunk approaches the maxSize value, it will be split into 2 which may result in a move. 当块接近maxSize值时,它将被拆分为2,这可能导致移动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM