Overview
Shard Sharding is the separation of different data on different servers.
Model
among them:
The user's operation to MongoDB is requested by the mongs.
CONFIGSVR is used for saving, which data is stored on which shard
How to use Shards
Start 2 nodes first 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, is more than the previous –configsvr
mongd --dbpath ~/db/mongo3/databse --logpath ~/db/mongo3/log/mongo.log --port 27019 --fork --smallfiles --configsvr
start MONGOs, note –configdb Specify CONFIGSVR
mongos --logpath ~/db/mongo4/log/mongs.log --port 27020 --configdb 127.0.0.1:27020 --fork
Even to MONGOs.
mongo --port 27020
Add the 2 nodes that are running ahead
sh.addShard("127.0.0.1:27017")sh.addShard("127.0.0.1:27018")
View status
sh.status()
define Shard Rules : For example, declare the test library to be fragmented, and then define the shards based on the my_id of my table
sh.enableSharding(‘test‘)sh.shardCollection(‘test.my‘,{‘my_id‘:1})
sharding principle
n Document, form a "chunk", first put on a shard, when this piece of Shard on the chunk number, than the Shard number on other chunk difference, move chunk to another shard.
Modify the size of the chunk : for example, modify to 4m
The default size of a chunk is 64m
use configdb.setting.save({_id:‘chunksize‘,value:4m})
Manual Pre-sharding
In the preceding Shard method, shards are based on the number of chunk, which can cause problems affecting IO.
You can manually pre-make shards, such as defining my_id 0~1000 in Shard1, 1001~2000 in Shard2, 2001~3000 on Shard3
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})
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
MongoDB Learning Note--Shard Shard