remove and add an instance from a mongodb replica set

1. Connect to one of the instances in the replica set, and do the following mongodb commands to save the current relica set configuration in a variable.

config = rs.conf();
printjson(config);

2. The printjson function above will print the config data of the current replica set configuration. Let’s modify the members fields to remove one of the instances. Assume you have 3 instances in the config.members and you want to remove the first one. Do the following

config.members = [config.members[1], config.members[2]];
rs.reconfig(config, {force: true});

The force true option forces the current instance to use the new configuration and it will be propagated to all other instances listed in the config.members array.

3. If there is an instance that is not working due to hardware failure or network connections or whatever reasons, and you want to remove that from the replica set. You can just do this:

config = rs.config();
rs.reconfig(config, {force: true});

4. Remove an instance by specfing the instance endpoint using the rs.remove(), this command will remove the instance located in example.com:27017

rs.remove("example.com:27017");

5. Add an instance

rs.add("example.com:27017");

Search within Codexpedia

Custom Search

Search the entire web

Custom Search