Why do two sets of heavy lifting ??? Let MongoDB do it.
For this example, suppose IP of SECONDARY
is 10.30.50.70
with hostname myslave
STEP 01 : Enable Replica Set on the PRIMARY only
Make the following changes in /etc/mongodb.conf
on the PRIMARY
- enable authorization in
/etc/mongodb.conf
authorization: enabled
under the security YAML tag (MongoDB 2.6+)
auth=true
(if you are using MongoDB 2.4)
- Enable replication (suppose your name the replica set
myreplica
)
- Put
replSetName: myreplica
under replication YAML tag (MongoDB 2.6+)
- Put
replSet = myreplica
(MongoDB 2.4)
STEP 02 : Restart MongoDB on the PRIMARY
service mongod restart
STEP 03 : Initiate Replica Set on the PRIMARY
rs.initiate();
NOTE: At this point your have a one-node Replica Set
STEP 04 : Perform Steps 01 and 02 on your SECONDARY
NOTE: At this point, the SECONDARY
is replica set enabled but not replicating
STEP 05 : NOW, add the SECONDARY
to the PRIMARY
Goto the PRIMARY and run
rs.add("10.30.50.70:27017")
or
rs.add("myslave:27017")
STEP 06 : Check status of Replica Set
Run this repeatedly
rs.status()
If this command ever freezes, don't worry. It rebuilding all the indexes.
You can open another ssh session on the SECONDARY
, run tail -f
on the mongodb.log
file and see the index rebuild progression.
WHY DO IT THIS WAY ???
MongoDB will do essentially start replication, copy the data, and rebuild indexes in a single operation. As shown, this process is in no way automatic when building an initial replica set. Doing it this way, is less error-prone.