- Replication >
- Convert a Standalone to a Replica Set
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:
- Replica Set Fundamental Concepts,
- Replica Set Architectures and Deployment Patterns,
- Replica Set Operation and Management, and
- Replica Set Considerations and Behaviors for Applications and Development.
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:
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.
Shut down the your MongoDB instance and then restart using the
--replSetoption and the name of the replica set, which isrs0in the example below.Use a command similar to the following:
Replace
/srv/mongodb/db0with the path of yourdbpath.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.Open a
mongoshell and connect to themongodinstance. In a new system shell session, use the following command to start amongoshell:Use
rs.initiate()to initiate the replica set:The set is now operational. To return the replica set configuration, call the
rs.conf()method. To check the status of the replica set, users.status().Now add additional replica set members. On two distinct systems, start two new standalone
mongodinstances. Then, in themongoshell instance connected to the firstmongodinstance, issue a command in the following form:Replace
<hostname>and<port>with the resolvable hostname and port of themongodinstance you want to add to the set. Repeat this operation for eachmongodthat 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.