Navigation
This version of the manual is no longer supported.

Convert a Standalone to a Replica Set

On this page

This tutorial describes the process for converting a standalone mongod instance into a three-member replica set. Use standalone instances for testing and development, but always use replica sets in production. To install a standalone instance, see the installation tutorials.

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

For more information on replica sets, their use, and administration, see:

Note

If you’re converting a standalone instance into a replica set that is a shard in a sharded cluster you must change the shard host information in the config database. While connected to a mongos instance with a mongo shell, issue a command in the following form:

db.getSiblingDB("config").shards.save( {_id: "<name>", host: "<replica-set>/<member,><member,><...>" } )

Replace <name> with the name of the shard, replace <replica-set> with the name of the replica set, and replace <member,><member,><> with the list of the members of the replica set.

After completing this operation you must restart all mongos instances. When possible you should restart all components of the replica sets (i.e. all mongos and all shard mongod instances.)

Procedure

This procedure assumes you have a standalone instance of MongoDB installed. If you have not already installed MongoDB, see the installation tutorials.

  1. Shut down the your MongoDB instance and then restart using the --replSet option and the name of the replica set, which is rs0 in the example below.

    Use a command similar to the following:

    mongod --port 27017 --dbpath /srv/mongodb/db0 --replSet rs0
    

    Replace /srv/mongodb/db0 with the path of your dbpath.

    This starts the instance as a member of a replica set named rs0. For more information on configuration options, see Configuration File Options and the mongod.

  2. Open a mongo shell and connect to the mongod instance. In a new system shell session, use the following command to start a mongo shell:

    mongo
    
  3. Use rs.initiate() to initiate the replica set:

    rs.initiate()
    

    The set is now operational. To return the replica set configuration, call the rs.conf() method. To check the status of the replica set, use rs.status().

  4. Now add additional replica set members. On two distinct systems, start two new standalone mongod instances. Then, in the mongo shell instance connected to the first mongod instance, issue a command in the following form:

    rs.add("<hostname><:port>")
    

    Replace <hostname> and <port> with the resolvable hostname and port of the mongod instance you want to add to the set. Repeat this operation for each mongod that you want to add to the set.

    For more information on adding hosts to a replica set, see the Add Members to a Replica Set document.