MongoDB Replica Sets not only provides a high-availability solution, it also provides a load-balanced solution, increasing or decreasing Replica sets nodes in practical applications is very common, such as when the application of read pressure surge, 3 node environment can not meet the demand, Then you need to add some nodes to distribute the pressure evenly.
two ways: one is to increase the node through Oplog, the other is to increase the node by database snapshot and Oplog
adding nodes through Oplog
1, configure and start the new node, enable 28013 this port to the new node
[Email protected] ~]# mkdir-p/data/data/~]# echo "This is rs1 super secret key" >/data/key/~]# CHM OD 600/data/key/~]#/apps/mongo/bin/mongod--replset rs1--keyfile/data/key/r3--fork--Port28013-- DBPATH/DATA/DATA/R3--logpath=/data/log/r3.log--/data/log/10553~]#
View Code
2. Add this new node to the existing replica sets
Rs1:primary> Rs.add ("localhost:28013""OK": 1}
3, see replica sets, we can clearly see how the internal is added 28013 this new node
(1) to initialize
Rs1:primary >Rs.status () {"Set": "Rs1","Date": Isodate ("2012-05-31t12:17:44z"),"MyState": 1,"Members": [... {"_ID": 3,"Name": "localhost:28013","Health": 0,"State": 6,"Statestr": "(not Reachable/healthy)","Uptime": 0,"Optime" : {"T": 0,"I": 0},"Optimedate": Isodate ("1970-01-01t00:00:00z"),"Lastheartbeat": Isodate ("2012-05-31t12:17:43z"),"ErrMsg": "Still Initializing"}],"OK": 1}
View Code
(2) synchronization of data
Rs1:primary>Rs.status () {"Set": "Rs1","Date": Isodate ("2012-05-31t12:18:07z"),"MyState": 1,"Members": [... {"_ID": 3,"Name": "localhost:28013","Health": 1,"State": 3,"Statestr": "RECOVERING","Uptime": 16,"Optime" : {"T": 0,"I": 0},"Optimedate": Isodate ("1970-01-01t00:00:00z"),"Lastheartbeat": Isodate ("2012-05-31t12:18:05z"),"ErrMsg": "Initial sync need a member to be primary or Secondaryto DoOur initial sync"}],"OK": 1}
View Code
(3) Initialization synchronization complete
Rs1:primary>Rs.status () {"Set": "Rs1","Date": Isodate ("2012-05-31t12:18:08z"),"MyState": 1,"Members": [... {"_ID": 3,"Name": "localhost:28013","Health": 1,"State": 3,"Statestr": "RECOVERING","Uptime": 17,"Optime" : {"T": 1338466661000,"I": 1},"Optimedate": Isodate ("2012-05-31t12:17:41z"),"Lastheartbeat": Isodate ("2012-05-31t12:18:07z"),"ErrMsg": "Initial sync Done"}],"OK": 1}
View Code
(4) node added complete, status OK
Rs1:primary>Rs.status () {"Set": "Rs1","Date": Isodate ("2012-05-31t12:18:10z"),"MyState": 1,"Members": [... {"_ID": 3,"Name": "localhost:28013","Health": 1,"State": 2,"Statestr": "Secondary","Uptime": 19,"Optime" : {"T": 1338466661000,"I": 1},"Optimedate": Isodate ("2012-05-31t12:17:41z"),"Lastheartbeat": Isodate ("2012-05-31t12:18:09z")}],"OK": 1}
View Code
4. The verification data has been synchronized.
[Email protected] data]#/apps/mongo/bin/mongo-port 280131.8.1127.0.0.1:28013/testrs1: Secondary> rs.slaveok () rs1:secondary>"_id": ObjectId ("4fc760d2383ede1dce14ef86"), " Age ": Ten }rs1:secondary>
Adding nodes through database snapshots and Oplog
Through the oplog directly to increase the node operation is simple and no manual intervention too much, but the Oplog is capped collection, the use of a circular way to log processing, so the use of Oplog way to increase the node, it may lead to inconsistent data, Because the information stored in the log may have been refreshed. But it doesn't matter, we can use database Snapshot (--fastsync) and Oplog in a way to increase the node, the process is to take a copy of a member of the physical file to initialize the data, and then the rest of the Oplog log to chase, and finally achieve data consistency.
(1) Taking a physical file of a replica set member as the initialization data
[Email protected] ~]# scp-r/data/data/r3/data/data/~]# echo "This is rs1 super secret key" >/data/key/
~]# chmod 600/DATA/KEY/R4
(2) After the physical file has been taken, a new document is inserted in the C1 set to verify that the update was also synchronized
rs1:primary>"_id": ObjectId ("4fc760d2383ede1dce14ef86"), "age": }rs1:primary> Db.c1.insert ({age:20}) rs1:primary>"_id": ObjectId ("4fc760d2383ede1dce14ef86"), "age": "_id": ObjectId ("4fc7748f479e007bde6644ef"), "age": }rs1:primary>
(3) Enable 28014 this port to the new node
/apps/mongo/bin/mongod--replset rs1--keyfile/data/key/r4--fork--port 28014--dbpath/DATA/DATA/R4-- Logpath=/data/log/r4.log--logappend--fastsync
(4) Add 28014 nodes
Rs1:primary> Rs.add ("localhost:28014""OK": 1}
(5) Verify that the data has been synchronized.
[Email protected] data]#/apps/mongo/bin/mongo-port 280141.8.1127.0.0.1:28014/testrs1: Secondary> rs.slaveok () rs1:secondary>"_id": ObjectId ("4fc760d2383ede1dce14ef86"), " Age ": Ten" _id ": ObjectId (" 4fc7748f479e007bde6644ef ")," age ": }rs1:secondary>
View Code
MongoDB Finishing Notes Add node