Navigation

rs.initiate()

Description

rs.initiate(configuration)

Initiates a replica set. Optionally, the method can take an argument in the form of a document that holds the configuration of a replica set.

The rs.initiate() method has the following parameter:

Parameter Type Description
configuration document Optional. A document that specifies configuration for the new replica set. If a configuration is not specified, MongoDB uses a default replica set configuration.

The rs.initiate() method provides a wrapper around the replSetInitiate command.

Replica Set Configuration

See Replica Set Configuration Document Example for details of replica set configuration document.

Example

The following operation initiates a new replica set with three members. The three mongod instances must have started with the --replSet command line option, or replication.replSetName if using a configuration file, set to myReplSet:

Connect to one of the mongod instances and run rs.initiate().

Note

Run the method on only one of the mongod instances.

rs.initiate(
   {
      _id: "myReplSet",
      version: 1,
      members: [
         { _id: 0, host : "mongodb0.example.net:27017" },
         { _id: 1, host : "mongodb1.example.net:27017" },
         { _id: 2, host : "mongodb2.example.net:27017" }
      ]
   }
)

For details on replica set configuration, see Replica Set Configuration Fields.

For details on deploying a replica set, see Deploy a Replica Set.