Convert a Secondary to an Arbiter

If you have a secondary in a replica set that nolonger needs to hold data but that needs to remain in the set toensure that the set can elect a primary, you may convert the secondary to anarbiter using either procedure in thistutorial. Both procedures are operationally equivalent:

  • You may operate the arbiter on the same port as the former secondary.In this procedure, you must shut down the secondary and remove itsdata before restarting and reconfiguring it as an arbiter.

For this procedure, see Convert Secondary to Arbiter and Reuse the Port Number.

  • Run the arbiter on a new port. In this procedure, you can reconfigurethe server as an arbiter before shutting down the instance running asa secondary.

For this procedure, see Convert Secondary to Arbiter Running on a New Port Number.

Note

For the following MongoDB versions, pv1 increases the likelihoodof w:1 rollbacks compared to pv0(no longer supported in MongoDB 4.0+) for replica sets with arbiters:

  • MongoDB 3.4.1
  • MongoDB 3.4.0
  • MongoDB 3.2.11 or earlier

See Replica Set Protocol Version.

Convert Secondary to Arbiter and Reuse the Port Number

  • If your application is connecting directly to the secondary,modify the application so that MongoDB queries don’t reachthe secondary.

  • Shut down the secondary.

  • Remove the secondary from the replica set by callingthe rs.remove() method. Perform this operation while connected to the currentprimary in the mongo shell:

  1. rs.remove("<hostname><:port>")
  • Verify that the replica set no longer includes the secondary bycalling the rs.conf() method in the mongo shell:
  1. rs.conf()
  • Move the secondary’s data directory to an archive folder. For example:
  1. mv /data/db /data/db-old

Optional

You may remove the data instead.

  • Create a new, empty data directory to point to when restarting themongod instance. You can reuse the previous name. Forexample:
  1. mkdir /data/db
  • Restart the mongod instance for the secondary, specifyingthe port number, the empty data directory, and the replica set. Youcan use the same port number you used before. Issue a command similarto the following:

Warning

Before binding to a non-localhost (e.g. publicly accessible)IP address, ensure you have secured your cluster from unauthorizedaccess. For a complete list of security recommendations, seeSecurity Checklist. At minimum, considerenabling authentication andhardening network infrastructure.

  1. mongod --port 27021 --dbpath /data/db --replSet rs --bind_ip localhost,<hostname(s)|ip address(es)>
  • In the mongo shell convert the secondary to an arbiterusing the rs.addArb() method:
  1. rs.addArb("<hostname><:port>")
  • Verify the arbiter belongs to the replica set by calling thers.conf() method in the mongo shell.
  1. rs.conf()

The arbiter member should include the following:

  1. "arbiterOnly" : true

Convert Secondary to Arbiter Running on a New Port Number

  • If your application is connecting directly to the secondaryor has a connection string referencing the secondary,modify the application so that MongoDB queries don’t reachthe secondary.

  • Create a new, empty data directory to be used with the new portnumber. For example:

  1. mkdir /data/db-temp
  • Start a new mongod instance on the new port number,specifying the new data directory and the existing replicaset. Issue a command similar to the following:

Warning

Before binding to a non-localhost (e.g. publicly accessible)IP address, ensure you have secured your cluster from unauthorizedaccess. For a complete list of security recommendations, seeSecurity Checklist. At minimum, considerenabling authentication andhardening network infrastructure.

  1. mongod --port 27021 --dbpath /data/db-temp --replSet rs --bind_ip localhost,<hostname(s)|ip address(es)>
  • In the mongo shell connected to the current primary,convert the new mongod instance to an arbiter using the rs.addArb()method:
  1. rs.addArb("<hostname><:port>")
  • Verify the arbiter has been added to the replica set by calling thers.conf() method in the mongo shell.
  1. rs.conf()

The arbiter member should include the following:

  1. "arbiterOnly" : true
  1. rs.remove("<hostname><:port>")
  • Verify that the replica set no longer includes the old secondary bycalling the rs.conf() method in the mongo shell:
  1. rs.conf()
  • Move the secondary’s data directory to an archive folder. For example:
  1. mv /data/db /data/db-old

Optional

You may remove the data instead.