Sharding
- In MongoDB there is another cluster, that is, sharding technology, can meet the needs of a large number of MONGODB data volume growth
- When MongoDB stores a huge amount of data, a machine may not be enough to store data, or it may not be enough to provide acceptable read-write throughput, so we can split the data on multiple machines, allowing the database system to store and process more data
Why use Shards
- Local disk is not large enough
- When the volume of requests is large, there is insufficient memory.
- Vertical expansion is expensive (memory, disk, CPU)
Implementing shards
- The Shard structure diagram is as follows:
- Implementing a Shard requires 3 parts:
- Routing server MONGOs: The client is connected and writes data to a different data server according to the Shard basis
- Configure Server Mongod: The basis for slicing data
- Data server Mongod: can have multiple physical machines for storing actual blocks of data
- The design ports are as follows:
- Routing server: 60001
- Configuration server: 60002
- Data server 1:60,003
- Data server 2:60,004
- Step1: Start the data server, currently in the desktop directory
sudo mkdir t1sudo mkdir t2sudo mongod --port 60003 --dbpath=~/Desktop/t1sudo mongod --port 60004 --dbpath=~/Desktop/t2
- Step2: Start the configuration server
sudo mkdir confsudo mongod --port 60002 --dbpath=~/Desktop/conf
- Step3: Starting the routing server
sudo mongos --port 60001 --configdb 192.168.196.128:60002
- STEP4: Adding a data server to the routing server
mongo --port 60001use admindb.runCommand({addshard:‘192.168.196.128:60003‘})db.runCommand({addshard:‘192.168.196.128:60004‘})
- STEP5: Enable sharding on database test1
db.runCommand({enablesharding:‘test1‘})
- STEP6: Specifies the slice key, which is the Shard basis of the document in the collection
db.runCommand({shardcollection:‘test1.t1‘,key:{name:1}})
- STEP7: Test data, inserting 1W data into the collection
for(i=0;i<10000;i++){ db.t1.insert({name:‘abc‘+i})}
- STEP8: Viewing data storage conditions
db.printShardingStatus()
- You can see that the data is stored evenly on the data server
- STEP9: Querying data
db.t1.find({name:‘abc1000‘})db.t1.find({name:‘abc9000‘})
- Use of shards, transparent to clients, no change in read and write data
MongoDB Cluster Shard