Convert a Standalone to a Replica Set

This tutorial describes the process for converting a standalonemongod instance into a replica set. Usestandalone instances for testing and development, but always usereplica sets in production.

The procedure is specific to instances that are not part of a shardedcluster. To convert a shard standalone instance to a shard replica set,see Convert a Shard Standalone to a Shard Replica Setinstead.

To deploy a replica set without using a pre-existingmongod instance, see Deploy a Replica Setinstead.

To install a standalone instance, see the installation tutorials.

Procedure

Tip

When possible, use a logical DNS hostname instead of an ip address,particularly when configuring replica set members or sharded clustermembers. The use of logical DNS hostnames avoids configurationchanges due to ip address changes.

  • Shut down the standalonemongod instance.

  • Restart the instance. Use the —replSetoption to specify the name of the new replica set.

For example, the following command starts a standalone instance as amember of a new replica set named rs0. The command uses thestandalone’s existing database path of /srv/mongodb/db0:

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 27017 --dbpath /srv/mongodb/db0 --replSet rs0 --bind_ip localhost,<hostname(s)|ip address(es)>

If your application connects to more than one replica set, each setshould have a distinct name. Some drivers group replica setconnections by replica set name.

For more information on configuration options, seeConfiguration File Options and the mongodmanual page.

  1. rs.initiate()

The replica set is now operational. To view the replica setconfiguration, use rs.conf(). To check the status of thereplica set, use rs.status().

To add members to this replica set, use the rs.add() method.For more information on adding members to a replica set, seeAdd Members to a Replica Set.

See also

Convert a Shard Standalone to a Shard Replica Set