Docs Menu
Docs Home
/
MongoDB Manual
/ /

Convert a Standalone to a Replica Set

This tutorial describes the process for converting a standalone mongod instance into a replica set. Use standalone instances for testing and development, but always use replica sets in production.

To deploy a replica set without using a pre-existing mongod instance, see Deploy a Replica Set instead.

To install a standalone instance, see the installation tutorials.

Important

To avoid configuration updates due to IP address changes, use DNS hostnames instead of IP addresses. It is particularly important to use a DNS hostname instead of an IP address when configuring replica set members or sharded cluster members.

Use hostnames instead of IP addresses to configure clusters across a split network horizon. Starting in MongoDB 5.0, nodes that are only configured with an IP address will fail startup validation and will not start.

  1. Shut down the standalone mongod instance.

  2. Restart the instance. Use the --replSet option to specify the name of the new replica set.

    For example, the following command starts a standalone instance as a member of a new replica set named rs0. The command uses the standalone'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 unauthorized access. For a complete list of security recommendations, see Security Checklist. At minimum, consider enabling authentication and hardening network infrastructure.

    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 set must have a distinct name. Some drivers group replica set connections by replica set name.

    For more information on configuration options, see Configuration File Options and the mongod manual page.

  3. Connect mongosh to the mongod instance.

  4. Use rs.initiate() to initiate the new replica set:

    rs.initiate()

    The replica set is now operational. To view the replica set configuration, use rs.conf(). To check the status of the replica 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, see Add Members to a Replica Set.

Back

Add an Arbiter to Replica Set