Re-add member in replica

Hi @Mauricio_Tavares_Lei, I tried the same actions: From a 3 member replica-set removed a member and added it again. See the steps I followed on a Windows and MongoDB v4.2.8.

1. Create a replica-set with three members:

conf1.cfg:

storage:
  dbPath: G:\mongo\mongo-stuff\misc\db\node1
net:
  bindIp: localhost
  port: 27011
systemLog:
  destination: file
  path: G:\mongo\mongo-stuff\misc\db\node1\mongod.log
  logAppend: true
replication:
  replSetName: myrset

Similarly, I created conf2.cfg and conf3.cfg on ports 27012 and 27013 with data directories on node2 and node3 folders (instead of node1).

Started the three mongods:

mongod -f conf1.cfg
mongod -f conf2.cfg
mongod -f conf3.cfg

Connected to a member:

mongo --port 27011

Initiated the replica-set and added the other two members.

rs.initiate()
rs.add("localhost:27012")
rs.add("localhost:27013")

rs.status()

All okay, replica-set created and running.
Add some data, etc.

2. Remove a member:

I connected to the secondary member “localhost:27012”, and stopped the instance.

use admin
db.shutdownServer()

[ NOTE: Corrected the typo rs.shutdownServer() to db.shutdownServer() ]

From the primary, removed the member “localhost:27012” from the replica-set:

rs.remove("localhost:27012")

Deleted the data directory of member (“localhost:27012”): G:\mongo\mongo-stuff\misc\db\node2

3. Adding the member again (“localhost:27012” to the replica-set)

Prepared the Data Directory:

  • Created the directory G:\mongo\mongo-stuff\misc\db\node2
  • Copied the data (all the files except the log) from the node3 to node2. There were no writes happening when I made a copy of the data.

NOTE from documentation: If your storage system does not support snapshots, you can copy the files directly using cp, rsync, or a similar tool. Since copying multiple files is not an atomic operation, you must stop all writes to the mongod before copying the files. Otherwise, you will copy the files in an invalid state.

Started the instance of the tobe-added member.

mongod -f conf2.cfg

Connect to replica-set’s primary. Add the new member “localhost:27012”.
Check the status before and after adding.

rs.status()
rs.add("localhost:27012")
rs.status()

All okay.

1 Like