问题
I try to test sharding in MongoDB. For example, I use host1.com and host2.com instead real server names.
So I created config server at host1.com:
mongod --dbpath /path/to/configdb/ --configsvr
Started mongos
at the same machine:
mongos --configdb host1.com --port 27020
And started mongod
at two machines (host1.com and host2.com):
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 . 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? And why db.printCollectionStats()
show me that test
database "sharded" : false
. What I did wrong?
回答1:
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.
On the difference between chunk size and maxSize:
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.
来源:https://stackoverflow.com/questions/15813194/sharding-in-mongodb