MongoDB has officially supported Sharding since version 1.6.
MongoDB also released Replica Sets to replace earlier versions of Replica Pairs.
By combining Sharding and Replica Sets, we can build a distributed, highly available, and automatically horizontally Scalable Cluster.
A typical cluster structure is as follows:
A cluster consists of the following three services:
- Shards Server: each shard is composed of one or more mongod processes used to store data.
- Config Server: used to store the Metadata information of a cluster, including the information of each Shard and chunks.
- Route Server: used to provide the routing service, connected by the Client, so that the entire Cluster looks like a single DB Server
In addition, Chunks are continuous data blocks in MongoDB. The default size is 200 MB, and a Chunk is located on one of the Shard servers.
Next, build a Cluster, which consists of four servers, including two Shard, three Config, and one Route
Each Shard is composed of a Replica Set, and each Replica Set is composed of two Mongod nodes and one vote node.
The configuration process is as follows:
1. The four servers start the corresponding Mongod process respectively:
192.168.95.216
/Usr/local/mongodb/bin/mongod-fork-shardsvr-port 10000-replSet set1-dbpath/pvdata/mongodb_data-logpath/pvdata/mongodb_log/mongod. log
/Usr/local/mongodb/bin/mongod-fork-shardsvr-port 10001-replSet set2-dbpath/pvdata/mongodb_data1-logpath/pvdata/mongodb_log/mongod1.log
192.168.95.217
/Usr/local/mongodb/bin/mongod-fork-shardsvr-port 10000-replSet set1-dbpath/pvdata/mongodb_data-logpath/pvdata/mongodb_log/mongod. log
192.168.95.218
/Usr/local/mongodb/bin/mongod-fork-shardsvr-port 10000-replSet set2-dbpath/pvdata/mongodb_data-logpath/pvdata/mongodb_log/mongod. log
/Usr/local/mongodb/bin/mongod-fork-shardsvr-port 10001-replSet set1-dbpath/pvdata/mongodb_data1-logpath/pvdata/mongodb_log/mongod1.log
192.168.95.20.
/Usr/local/mongodb/bin/mongod-fork-shardsvr-port 10000-replSet set2-dbpath/opt/mongodb_data-logpath/opt/mongodb_log/mongod. log
2. Configure two groups of Replica Sets:
192.168.95.216
Mongo-port 10000
Config = {_ id: 'set1', members :[
{_ Id: 0, host: '192. 168.95.216: 100 '},
{_ Id: 1, host: '192. 168.95.217: 100 '},
{_ Id: 1, host: '192. 168.95.218: 100', arbiterOnly: true}
]}
Rs. initiate (config)
Rs. status ()
192.168.95.218
Mongo-port 10000
Config = {_ id: 'set2', members :[
{_ Id: 0, host: '192. 168.95.218: 100 '},
{_ Id: 1, host: '192. 168.95.20.: 100 '},
{_ Id: 1, host: '192. 168.95.216: 100', arbiterOnly: true}
]}
Rs. initiate (config)
Rs. status ()
Note: The Mongod corresponding to 10001 on the two servers is only responsible for vote election of a new master after a node is down. They do not store data backup.
3. Configure three Config Servers:
Mongod-configsvr-fork-logpath/pvdata/mongodb_log/config. log-dbpath/pvdata/mongodb_config_data-port 20000
4. Configure one Route Server:
192.168.95.216
/Usr/local/mongodb/bin/mongos-fork-chunkSize 1-configdb "192.168.95.216: 20000,192.168 .95.217: 20000,192.168 .95.218: 20000"-logpath/pvdata/mongodb_log/mongos. log
The chunkSize parameter is used to set the chunk block size. For testing, set it to 1 MB.