Shardingcluster introduces that this is a horizontally scalable mode, which is especially powerful when the data volume is large. In practice, large-scale applications generally use this architecture to build a monodb system. To build a MongoDBShardingCluster, three roles are required: ShardServer: mongod instance, which is used to store the actual data blocks. Sharding cluster Introduction
This is a horizontally scalable mode, which is especially powerful when the data volume is large. It is generally used in large-scale applications.ArchitectureTo build a monodb system.
To build a MongoDB Sharding Cluster, three roles are required:
Shard Server: mongod instance, used to store actual data blocks. In the actual production environment, a shard server role can be set by several machines to a relica.SetTo prevent single point of failure (spof)
Config Server: mongod instance, which stores the entire Cluster Metadata, including chunk information.
Route Server: mongos instance, front-end routing, access from the client, and make the entireClusterIt looks like a single database, and front-end applications can be used transparently.
Sharding architecture diagram:
In this example, the actual environment architecture
Architecture example:
- Run a mongod instance (called mongod shard11, mongod shard12, and mongod shard13) on three machines respectively to organize replica set1 as shard1 of the cluster.
- Run a mongod instance (called mongod shard21, mongod shard22, mongod shard23) on three machines respectively to organize replica set2 as shard2 of the cluster.
- Each machine runs a mongod instance as three config servers.
- Each machine runs a mongs process for client connection.
Host |
IP |
Port Information |
Server1 |
10.1.1.1 |
Mongod shard11: 27017 Mongod shard12: 27018 Mongod config1: 20000 Mongs1: 30000 |
Server2 |
10.1.1.2 |
Mongod shard12: 27017 Mongod shard22: 27018 Mongod config2: 20000 Mongs2: 30000 |
Server3 |
10.1.1.3 |
Mongod shard13: 27017 Mongod shard23: 27018 Mongod config3: 20000 Mongs3: 30000 |
Software preparation
Software preparation
1. Create a user
GroupaDd-G 20001 mongodb
Useradd-U 20001-g mongodb
PasswdMongodb
2. Install monodb Software
Su? Mongodb
Tar zxvf mongodb-linux-x86_64-1.6.2.tar
After installation, the directory structure is as follows:
$TrEe mongodb-linux-x86_64-1.6.2
Mongodb-linux-x86_64-1.6.2
|? GNU-AGPL-3.0
|? README
|? THIRD-PARTY-NOTICES
'? Bin
|? BsonDuMp
|? Mongo
|? Mongod
|? MongoDump
|? MongoExPort
|? MongoFileS
|? Export Import
|? MongoRestore
|? Mongos
|? Mongosniff
'? MongoStat
1 directory, 14 files
3. Create a data directory
Create a shard data file directory on each server according to the sharding architecture diagram in this example.
Server1:
Su? Monodb
Cd/Home/monodb
Mkdir-P data/shard11
Mkdir-p data/shard21
Server2:
Su? Monodb
Cd/home/monodb
Mkdir-p data/shard11
Mkdir-p data/shard22
Server3:
Su? Monodb
Cd/home/monodb
Mkdir-p data/shard13
Mkdir-p data/shard23
Configure relica sets
1. Configure the replica sets used by shard1:
Server1:
Cd/home/mongodb/mongodb-linux-x86_64-1.6.2/bin
./Mongod? Shardsvr? ReplSet shard1? Port 27017? Dbpath/home/mongodb/data/shard11? OplogSize 100? Logpath/home/mongodb/data/shard11.log? Logappend? Fork
Server2:
Cd/home/mongodb/mongodb-linux-x86_64-1.6.2/bin
./Mongod? Shardsvr? ReplSet shard1? Port 27017? Dbpath/home/mongodb/data/shard12? OplogSize 100? Logpath/home/mongodb/data/shard12.log? Logappend? Fork
Server3:
Cd/home/mongodb/mongodb-linux-x86_64-1.6.2/bin
./Mongod? Shardsvr? ReplSet shard1? Port 27017? Dbpath/home/mongodb/data/shard13? OplogSize 100? Logpath/home/mongodb/data/shard13.log? Logappend? Fork
Initialize replica set
Connect one mongod with mongo and run the following command:
> Config = {_Id: 'Shard1 ', members :[
{_ Id: 0, host: '10. 1.1.1: 27017 '},
{_ Id: 1, host: '10. 1.1.2: 27017 '},
{_ Id: 2, host: '10. 1.1.3: 27017 '}]
}
> Rs. initiate (config );
In the same way, configure the replica sets used by shard2:
Server1:
Cd/home/mongodb/mongodb-linux-x86_64-1.6.2/bin
./Mongod? Shardsvr? ReplSet shard2? Port 27018? Dbpath/home/mongodb/data/shard21? OplogSize 100? Logpath/home/mongodb/data/shard21.log? Logappend? Fork
Server2:
Cd/home/mongodb/mongodb-linux-x86_64-1.6.2/bin
./Mongod? Shardsvr? ReplSet shard2? Port 27018? Dbpath/home/mongodb/data/shard22? OplogSize 100? Logpath/home/mongodb/data/shard22.log? Logappend? Fork
Server3:
Cd/home/mongodb/mongodb-linux-x86_64-1.6.2/bin
./Mongod? Shardsvr? ReplSet shard2? Port 27018? Dbpath/home/mongodb/data/shard23? OplogSize 100? Logpath/home/mongodb/data/shard23.log? Logappend? Fork
Initialize replica set
Connect one mongod with mongo and run the following command:
> Config = {_ id: 'shard2 ', members :[
{_ Id: 0, host: '10. 1.1.1: 27018 '},
{_ Id: 1, host: '10. 1.1.2: 27018 '},
{_ Id: 2, host: '10. 1.1.3: 27018 '}]
}
> Rs. initiate (config );
Now we have configured two replica sets, that is, we have prepared two shards.