Migrate from Standalone to Replica Set

For example, I have now a project that works in production and I want to migrate from standalone to Replica Set. The database is already quite large. What is the correct: should I first set up a Replica Set with empty databases - and then use mongodump to transfer data? Or should I first, when tuning the Replica Set on each node, create a database with the same data and then initialize the Replica Set?

Or I want to change the topology of the existing Replica Set and add one or several nodes? How can I transfer data to new nodes? Is it necessary to stop all nodes for this?


Great question. To convert from standalone to replica set, the preferred procedure is to:

  1. Shut down the standalone mongod instance.
  2. Change its config to include the option replication.replSetName, with the name of your new replica set.
  3. Connect to the instance, and run the command rs.initiate(). This will initiate the replica set, and your standalone will now be the primary node.
  4. Start some other mongod instances (at least two).
  5. Add the other instances to your replica set with rs.add(). (You must run this command from the primary node.)

To your second question, if your database is sufficiently large, you should use mongodump or mongoexport to export the data from your primary, and then send the data to your secondary nodes. Then you can import them to your secondary nodes with mongorestore or mongoimport, depending on whether you used mongodump or mongoexport.

You can find more detail on the docs. I hope this helps!


Thank you!!!

If I restore the database using mongorestore to a secondary node, will this data get into the oplog?
Or should I first prepare a node with data and only then connect it to the replicas using rs.add?