I have a big MongoDB database on my production server, which I want to set up as a replica set. I understand that if I enable the replica set option on both servers, it will automatically start to sync the data from scratch. But I don't want to sync from the beginning; instead I want to configure it like MySQL replication.
I would like to follow the following process:
- Take dump from primary
- Restore it on secondary
- Enable replica set in config file, on both servers
- Restart MongoDB.
- Go to primary server and add the secondary node.
Is this possible; or is there any other alternate solution for this?
Best Answer
Why do two sets of heavy lifting ??? Let MongoDB do it.
For this example, suppose IP of
SECONDARY
is10.30.50.70
with hostnamemyslave
STEP 01 : Enable Replica Set on the PRIMARY only
Make the following changes in
/etc/mongodb.conf
on the PRIMARY/etc/mongodb.conf
authorization: enabled
under the security YAML tag (MongoDB 2.6+)auth=true
(if you are using MongoDB 2.4)myreplica
)replSetName: myreplica
under replication YAML tag (MongoDB 2.6+)replSet = myreplica
(MongoDB 2.4)STEP 02 : Restart MongoDB on the
PRIMARY
STEP 03 : Initiate Replica Set on the
PRIMARY
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 replicatingSTEP 05 : NOW, add the
SECONDARY
to thePRIMARY
Goto the PRIMARY and run
or
STEP 06 : Check status of Replica Set
Run this repeatedly
If this command ever freezes, don't worry. It rebuilding all the indexes.
You can open another ssh session on the
SECONDARY
, runtail -f
on themongodb.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.