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:
- db.isMaster()
This should return a document that resembles the following:
- {
- "ismaster" : true,
- "msg" : "isdbgrid",
- "maxBsonObjectSize" : 16777216,
- "maxMessageSizeBytes" : 48000000,
- "maxWriteBatchSize" : 100000,
- "localTime" : ISODate("2017-11-15T17:50:05.775Z"),
- "logicalSessionTimeoutMinutes" : 30,
- "maxWireVersion" : 6,
- "minWireVersion" : 0,
- "ok" : 1,
- "$clusterTime" : {
- "clusterTime" : Timestamp(1510768200, 1),
- "signature" : {
- "hash" : BinData(0,"VKMQp+F/p+nDZldmWTUzBz7A8Kc="),
- "keyId" : NumberLong("6488693018630029321")
- }
- },
- "operationTime" : Timestamp(1510768200, 1)
- }
- Shut down the
mongod
processes on the master and allslave(s), using the following command while connected to eachinstance:
- 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
—replSet
option, as in the following:
- mongod --replSet <setname>
- 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:
- 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.