MongoDB Auto-sharding solves the problem of mass storage and dynamic capacity expansion, but there are some distances from the high reliability and high availability required for the actual production environment, so there is a "Replica sets + sharding" solution.
Shard
Use replica sets to ensure that each data node has backup, automatic fault-tolerant transfer, and automatic resilience.
Config
Use 3 configuration servers to ensure metadata integrity.
Route
Use 3 routing processes to achieve load balancing and improve client access performance.
Configuration replica sets + sharding frame composition:
Configuring replica sets + Sharding
(1) Configure the replica sets used by the Shard1
On Server A
[Email protected] bin]#/apps/mongo/bin/mongod--shardsvr--replset shard1--port 27017--dbpath/data/shard1_1-- Logpath/data/shard1_1/shard1_1.log--logappend--/data/shard1_1/18923
On Server B
[Email protected] bin]#/apps/mongo/bin/mongod--shardsvr--replset shard1--port 27017--dbpath/data/shard1_2-- Logpath/data/shard1_2/shard1_2.log--logappend--18859/data/shard1_2/shard1_2.log[[email Protected] bin]#
On server C
[Email protected] bin]#/apps/mongo/bin/mongod--shardsvr--replset shard1--port 27017--dbpath/data/shard1_3-- Logpath/data/shard1_3/shard1_3.log--logappend--/data/shard1_3/18768[[email protected] bin] #
Using MONGO to connect the 27017 port of the Mongod to one of the machines, initialize replica sets "Shard1" and execute:
[[email protected] bin]#./mongo--port 270171.8.1127.0.0.1:27017/Test> config = {_id: ' Shard1 '0, Host: ' 192.168.3.231:27017 '1, Host: ' 192.168.3.232:27017 '2, Host: ' 192.168.3.233:27017 '}] ...} ... > rs.initiate (config) {"info": "Config now saved locally. Should come online in about a minute. " ,"OK": 1}
View Code
(2) Configure the replica sets used by the Shard2
On Server A
[Email protected] bin]#/apps/mongo/bin/mongod--shardsvr--replset shard2--port 27018--dbpath/data/shard2_1-- Logpath/data/shard2_1/shard2_1.log--logappend--/data/shard2_1/18993[[email protected] bin] #
View Code
On Server B
[Email protected] bin]#/apps/mongo/bin/mongod--shardsvr--replset shard2--port 27018--dbpath/data/shard2_2-- Logpath/data/shard2_2/shard2_2.log--logappend--/data/shard2_2/18923[[email protected] bin] #
View Code
On Server C
[Email protected] bin]#/apps/mongo/bin/mongod--shardsvr--replset shard2--port 27018--dbpath/data/shard2_3-- Logpath/data/shard2_3/shard2_3.log--logappend--/data/shard2_3/18824[[email protected] bin] #
View Code
Using MONGO to connect the 27018 port of the Mongod to one of the machines, initialize replica sets "Shard2" and execute:
[[email protected] bin]#./mongo--port 27018mongodb Shell version: 1.8.1connecting to: 127.0.0.1:27018/test > config = {_id: ' shard2 ' 0, Host: ' 192.168.3.231:27018 ' > Rs.initiate (config) {" info ":" Config now saved locally. Should come online in about a minute. " "OK": 1db.runcommand ({enablesharding: "Test" "Test.users", key: {_id:1< Span style= "color: #000000;" > }})}
View Code
(3) Configuring 3 config Servers
Execute on server A, B, C:
/apps/mongo/bin/mongod--configsvr--dbpath/data/config--port 20000--logpath/data/config/config.log-- Logappend--fork
(4) Configure 3 route Process
Execute on server A, B, C:
/apps/mongo/bin/mongos--configdb192.168.3.231:20000,192.168.3.232:20000,192.168.3.233:20000--port 30000--chunksize 1--logpath/data/mongos.log--logappend--fork
(5) Configuring Shard Cluster
Connect to the MONGOs process on port 30000 of one of the machines, and switch to the admin database to do the following configuration
[Email protected] bin]#./mongo--port 300001.8.1127.0.0.1:30000/Test> Use adminswitched to DB admin>db.runcommand ({addshard: "shard1/ 192.168.3.231:27017,192.168.3.232:27017,192.168.3.233:27017 "}); {"shardadded": "Shard1", "OK": 1 }>db.runcommand ({addshard: "shard2/ 192.168.3.231:27018,192.168.3.232:27018,192.168.3.233:27018 "}); {"shardadded": "Shard2", "OK": 1 }>
View Code
Activating shards for databases and collections
Db.runcommand ({enablesharding: "Test""Test.users", key: {_id:1}})
(6) Verify that sharding works
Connect to the MONGOs process on port 30000 of one of the machines and switch to the test database, To add test data
Use test for (Var i=1;i<=200000;i++) Db.users.insert ({id:i,addr_1: "Beijing", Addr_2: "Shanghai"});d B.users.stats () { true ,"ns": "Test.users","Count": 200000,"size": 25600384,"avgobjsize": "storagesize": 44509696,"nindexes": 2,"nchunks": "shards": {" shard0000 " : {...}," shard0001 " : {...}," OK ": 1}
View Code
Can see sharding build success, and we expect the results of the same, so we will replica sets and sharding combination of architecture also learned!
MongoDB Finishing Note のreplica sets + sharding