Mongodb distributed cluster (3. sharding), mongodb Cluster
Overview
The above two blogs are about the mongodb cluster configuration and do not involve the distributed architecture. This blog is about the distributed architecture of mongodb, which divides a whole into multiple parts, each slice is deployed on different servers. This is mongodb's distributed architecture, that is, mongodb's sharding operation.
Deployment Diagram
The mongos Server is a Routing Server, the config server is the server that saves the corresponding configuration, and the shard1 and shard2 are partition servers. The two servers are combined to form a whole, the mongos server is their sum point. Therefore, our client only needs to interact with the mongos server, the server itself stores the corresponding information on different slice servers.
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 the mongos server (chunkSize: starts data transfer when the partition is inserted more than 1 MB, the default value is 200 MB) mongos -- port 27017 -- configdb 192.168.24.125: 27017 -- chunkSize 1 // log on to the mongos server and add a shard server (allowLocal allows local deployment of Multiple shards, which is 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, enable the database shard function, and specify the shard key db of the set. runCommand ({enablesharding: "DBName"}) db. runCommand ({shardcollection: "DBName. CName ", key: {fieldName: 1}) // number indicates sorting
Note that you must specify the part key of the set. This key is the key to divide the entire part. In this case, we can perform some test operations, log on to the mongos server, enter the corresponding test data, and then log on to different slice servers to view the content. Test data and other commands are as follows:
For (var I = 0; I <100000; I ++) {db. perons. insert ({id: I, name: "qingshan" + I})} // test the Data Partition node operation // remove a non-primary shard. Data is transferred for the first time and db is deleted for the second time. runCommand ({"removeshard": "192.168.24.42: 27017"}); db. runCommand ({"removeshard": "192.168.24.42: 27017"}); // Add a shard (delete the database first) db. runCommand ({addshard: "192.168.24.42: 27017", allowLocal: true}) // removes the primary shard. The first time the primary Shard is set, the second time the db is deleted. runCommand ({"moveprimary": "192.168.24.42: 27017"}); db. runCommand ({"removeshard": "192.168.24.252: 27017"}); db. runCommand ({listshards: 1}) // shard information db. printShardingStatus () // shard status use config db. shards. find () // shard Server Information db. chunks. find () // shard Information
Advantages and disadvantages of sharding Distributed Solution: when there is too much data, a server cannot store that much data. Of course, there are also some functions such as load balancing. Disadvantages from the above deployment diagram, we can see that once a problem occurs on the routing server, it will be finished. Similarly, once a problem occurs on a certain slice service, data integrity will occur, that is, the problem of missing data.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.