Overview
The above two blog is about the configuration of MongoDB cluster, not related to distributed, the blog is to write MongoDB distributed, a whole, divided into multiple slices, each piece deployed to a different server, this is MongoDB distributed, that is: MongoDB shard operation.
deployment Diagram
MONGOs server is a routing server, config server is to save the corresponding configuration of the server, Shard1 and Shard2 is a shard server, the two servers together is a whole, and MONGOs server is their and point, so, Our client only needs to interact with the MONGOs server, and later, the server itself will store the corresponding information on different slices of the server.
Shard Configuration
Start Shard server mongod--dbpath C:\Mongodb\data\shard1 mongod--dbpath C:\Mongodb\data\shard2 // Start config server mongod--dbpath C:\Mongodb\data\config--port 27017 /start MONGOs server (chunkSize: When the Shard Insert is greater than 1M, Start data transfer, default 200MB) mongos--port 27017--configdb 192.168.24.125:27017--chunksize 1 //Login MONGOs server, add Shard server (allowlocal allows multiple shards to be deployed on-premises, not allowed by default) Use admin db.runcommand ({addshard: "192.168.24.42:27017", allowlocal:true}) Db.runcommand ({addshard: " 192.168.24.252:27017 ", allowlocal:true}) //Log on to the MONGOs server, turn on the database sharding feature, and specify the Shard key of the collection Db.runcommand ({ Enablesharding: "DBName"}) Db.runcommand ({shardcollection: "Dbname.cname", Key:{fieldname:1}}) //number represents sort
It is important to note that you must specify the slice key for the Shard of the collection, which is the key to dividing the whole part. At this point we can do some testing operations, log on to the MONGOs server, enter the appropriate test data, and then, respectively, boarded a different server, look inside the content, you understand. Other aspects of test data such as commands are as follows
for (Var i=0;i<100000;i++) {Db.perons.insert ({id:i, Name: "Qingshan" +i})}//test data Slice node operation //removal unless primary shard, first transfer data, second delete Db.runcommand ({"Removeshard": "192.168.24.42:27017"}); Db.runcommand ({"Removeshard": "192.168.24.42:27017"}); Add Shard (must delete database first) Db.runcommand ({addshard: "192.168.24.42:27017", allowlocal:true}) //Remove primary shard, first set Primary shard, second delete Db.runcommand ({"Moveprimary": "192.168.24.42:27017"}); Db.runcommand ({"Removeshard": "192.168.24.252:27017"}); Db.runcommand ({listshards:1}) //Shard information db.printshardingstatus () //Shard State Use config Db.shards.find () //Shard Server information db.chunks.find () //Shard Information
advantages and disadvantages of shardingAdvantagesthe problem of distributed solution is that the data is too long, a server can not store so many data problems, of course, to load balance and other aspects of the function, there is. Disadvantagesfrom the deployment diagram above we can see that once the routing server problems, it is dead, again, once a piece of service problems, then there will be the data of the incompleteness, that is, the problem of missing data.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
MongoDB Distributed Cluster (3, shard)