2

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?

2 Answers 2

1

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.

1
  • Hi Rolando, thanks. Can you please explain this, 1. before start step1 can I take dump and restore it on secondary. 2.after added secondary server, will it start sync all data or just rebuild the induces and sync the data from after the dump process?
    – TheDataGuy
    Feb 15, 2017 at 6:28
0

Thanks Rolando.

Luckily I post the same on Google groups, someone from MongoDB helped me to fix my scenario. We can't directly dump, restore and configure replication. Please follow the steps to enable replication for large databases.

1.Copy the MongoDB directory to the secondary server.

2.Enable replication in the Primary config file and restart primary monogDB.

3.Run rs.initiate() in primary.

4.Stop MongoDB in secondary, replace the data directory with the primary data directory.

5.Enable replication in config file and start mongod service.

6.Go to primary and rs.add(secondary:port).

  1. You can see the secondary server is rebuilding indices and updated data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.