Convert a Master-Slave Deployment to a Replica Set

Removed

MongoDB 4.0 removes support for master-slave replication. Before youcan upgrade to MongoDB 4.0, if your deployment uses master-slavereplication, you must upgrade to a replica set.

To convert your master-slave replication, seeConvert a Master-Slave Deployment to a Replica Set.

Procedure

To convert a master-slave deployment to a replica set, restart thecurrent master as a one-member replica set.

  • To confirm that the current instance is master, run:
  1. db.isMaster()

This should return a document that resembles the following:

  1. {
  2. "ismaster" : true,
  3. "msg" : "isdbgrid",
  4. "maxBsonObjectSize" : 16777216,
  5. "maxMessageSizeBytes" : 48000000,
  6. "maxWriteBatchSize" : 100000,
  7. "localTime" : ISODate("2017-11-15T17:50:05.775Z"),
  8. "logicalSessionTimeoutMinutes" : 30,
  9. "maxWireVersion" : 6,
  10. "minWireVersion" : 0,
  11. "ok" : 1,
  12. "$clusterTime" : {
  13. "clusterTime" : Timestamp(1510768200, 1),
  14. "signature" : {
  15. "hash" : BinData(0,"VKMQp+F/p+nDZldmWTUzBz7A8Kc="),
  16. "keyId" : NumberLong("6488693018630029321")
  17. }
  18. },
  19. "operationTime" : Timestamp(1510768200, 1)
  20. }
  • Shut down the mongod processes on the master and allslave(s), using the following command while connected to eachinstance:
  1. db.adminCommand({shutdown : 1, force : true})
  • Back up your /data/db directories, in case you need to revertto the master-slave deployment.

  • Start the former master with the —replSetoption, as in the following:

  1. mongod --replSet <setname>
  • Connect to the mongod with the mongo shell,and initiate the replica set with the following command:
  1. rs.initiate( { _id: "<setname>", members: [ { _id: 0, host: "<host:port>" } ] } )

When the command returns, you will have successfully deployed aone-member replica set. You can check the status of your replica setat any time by running the following command:

  1. rs.status()

To add the former slaves as secondaries to the replica set, remove thetheir data directories and add to the replica set. SeeAdd Members to a Replica Set to add new members to a replica set.