Mongodb Study Notes 09 -- shard sharding
Overview
Shard sharding divides different data into different servers.
Model
Where: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Partition + vH87XEPGJyIC8 + DQpjb25maWdzdnIg08PT2rGjtOajrMSzzPXK/partition = "how to use a partition"> how to use a partition
Start two nodes as shard
mongd --dbpath ~/db/mongo1/databse --logpath ~/db/mongo1/log/mongo.log --port 27017 --fork --smallfilesmongd --dbpath ~/db/mongo2/databse --logpath ~/db/mongo2/log/mongo.log --port 27018 --fork --smallfiles
Start configsvr-Configsvr
mongd --dbpath ~/db/mongo3/databse --logpath ~/db/mongo3/log/mongo.log --port 27019 --fork --smallfiles --configsvr
Start mongosNote:-configdb specifies configsvr
mongos --logpath ~/db/mongo4/log/mongs.log --port 27020 --configdb 127.0.0.1:27020 --fork
Connect to mongos
mongo --port 27020
Add the preceding two nodes
sh.addShard("127.0.0.1:27017")sh.addShard("127.0.0.1:27018")
View status
sh.status()
Define sharding rulesFor example, declare that the test database can be sharded first, and then define the partitions Based on the my_id of my table.
sh.enableSharding('test')sh.shardCollection('test.my',{'my_id':1})
Sharding Principle
N documents form a "chunk" and put it on a shard first. When the chunk number on this shard is different from the chunk number on other shard, move the chunk to another shard.
Modify the chunk size: For example, to 4 m
The default chunk size is 64 MB.
use configdb.setting.save({_id:'chunksize',value:4m})
Manual pre-partitioningIn the previous sharding method, shards are based on the number of chunks, which may affect IO.
You can manually pre-create parts, for example, define my_id 0 ~ 1000 put in shard1, 1001 ~ 2000 put in shard2, 2001 ~ Put in shard3 in 3000
sh.enableSharding('test')sh.shardCollection('test.my',{'my_id':1})sh.splitAt('my',{my_id:1000})sh.splitAt('my',{my_id:2000})sh.splitAt('my',{my_id:3000})