Complete example of building a MongoDB cluster (replica set + Shard) ...
Prepare four machines, namely BLUEJOE1,BLUEJOE2,BLUEJOE3, and BLUEJOE0
The replica set and Shard policy are determined as follows:
- 3 replica sets will be created, named Shard1,shard2,shard3;
- The above 3 replica sets as 3 shards;
- Each replica set consists of 2 replicas (primary, secondary);
- Copies are stored separately, that is, Shard1 exists bluejoe1 and bluejoe2 on each copy ... And so on
- 3 Configuration library Instances will be created, one for each machine
- Configure a MONGOs on BLUEJOE0 (MONGOs can be configured on the application side)
Draw a diagram:
The following are the steps:
- Download MONGDB installation package on BLUEJOE1;
- Unzip to/usr/local/mongdb (pay attention to renaming);
- Create the Data/node1 and Data/node2 directories, launch 2 Mongod instances, and note that they are shard1-1 and shard3-2, respectively:
/usr/local/mongodb/bin/mongod--dbpath/usr/local/mongodb/data/node1--port 10001--replSet shard1/bluejoe2:10002-- Logpath=/usr/local/mongodb/log/node1.log/usr/local/mongodb/bin/mongod--dbpath/usr/local/mongodb/data/node2-- Port 10002--replset shard3/bluejoe3:10001--logpath=/usr/local/mongodb/log/node2.log
- Initializing replicas
Db.runcommand ({"replsetinitiate": {"_id": "Shard1", "members": [{"_id": 1, "host": "bluejoe1:10001"},{"_id": 2, "host": "Bluejoe2:10002"}]})
- To create a data/configdb, launch the configuration library instance:
/usr/local/mongodb/bin/mongod--dbpath/usr/local/mongodb/data/configdb--port 20000--logpath=/usr/local/mongodb/ Log/configdb.log
- Using the SCP command, copy the MongoDB directory to the Bluejoe2 and Bluejoe3 machines and start the 3 instances on each machine in steps 3rd, 4, and note that the replicas are initialized;
- So far there should be 9 Mongd instances, 3 of which are configuration library instances, and the remaining 3 replica sets;
- Next, manage the shards and start the mongs on BLUEJOE0:
/usr/local/mongodb/bin/mongos--port 30000--configdb bluejoe1:20000,bluejoe2:20000,bluejoe3:20000
- To connect the MONGOs, configure the Shard information:
Mongos> use adminswitched to db Admindb.runcommand ({"Addshard": "shard1/bluejoe1:10001"}) Db.runcommand ({"AddShard ":" shard2/bluejoe2:10001 "}) Db.runcommand ({" Addshard ":" shard3/bluejoe3:10001 "})
- To turn on the Shard feature on the library:
Mongos> use adminswitched to db adminmongos> Db.runcommand ({"enablesharding": "Test"}) {"OK": 1}mongos> Db.runC Ommand ({"Shardcollection": "Test.person", "key": {"name": 1}}) {"collectionsharded": "Test.person", "OK": 1}
Building a MongoDB cluster (replica set + Shard)