1. Shards of Monogdb (sharding)
Sharding is a data storage method used by MongoDB for terabytes of data above the TB level.
MongoDB uses a solution that splits the collection and then splits the data into several MongoDB instances.
In shard mode, MongoDB instances are divided into three types:
Shards: MongoDB that stores data
Config: Save the set Monogdb
Routing (MONGOs): MongoDB for Shard processing
2. Configuring the Shard 2.1 pilot environment
1 MONGOs, 1 config, 2 shard
Config: test166:27019mongos: test166:27020shards: test166:27017,test167:27017
MongoDB is a single component and does not use a replica set
2.2 Start Config
Start config instance on test166, Port 27019
# Mongod--configsvr--dbpath/var/lib/mongo-c--port 27019
2.3 Start MONGOs
Launching the routing instance on test166, Port 27020
# MONGOs--configdb test166:27019--port 27020
2.4 Start shards
Start the shards on test166 and test167 respectively
#/etc/init.d/mongod Start
2.5 Adding shards
Connection Monogs
# MONGO--port 27020
Add shards
mongos> use adminmongos> sh.addshard ("test166:27017") mongos> Sh.addshard ("test167:27017")
Confirm
Mongos> Db.runcommand ({listshards:1})
2.6 Opening Shards
Turn on sharding for the specified library
Mongos> sh.enablesharding ("new")
Specify the slice key for the Shard
There are two modes of the tablet key: hash mode, Range mode
2.6.1 using hash mode sharding
Mongos> sh.shardcollection ("New.person", {"_id": "Hashed"})
Insert Data Acknowledgement
Mongos> use newmongos> for (Var i=0;i<10;i++) {Db.person.insert ({name: "Bluejoe" +i});
Use hash mode to record the average distribution on each slice
2.6.2 using range mode sharding
Mongos> sh.shardcollection ("New.person2", {"name": 1})
Insert Data Acknowledgement
Mongos> use newmongos> for (Var i=0;i<100;i++) {Db.person2.insert ({name: "Jack" +i});
2.7 Confirm Shard Condition
Mongos> sh.status () or mongos> db.printshardingstatus ()
Confirm data distribution on each shard (hash mode)
> Use new> db.person.find ()
Distribution of data on test166:27017
Distribution of data on test167:27017
2.8 Other
If the library you want to Shard has data, first build index and then specify the slice key
Mongos> sh.enablesharding ("New2") mongos> use new2mongos> db.user2.createIndex ({"username": 1}) mongos> Sh.shardcollection ("New2.user2", {"username": 1})
3. PostScript
This test environment is made up of a MongoDB single, no replica set is used, and the Shard configuration when using the replica set is similar to the one above, not detailed here.
MongoDB Series (iii): MongoDB Shard