Navigation
This version of the documentation is archived and no longer supported.

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.

IP Binding

Starting in MongoDB 3.6, MongoDB binaries, mongod and mongos, bind to localhost (127.0.0.1) by default. If the net.ipv6 configuration file setting or the --ipv6 command line option is set for the binary, the binary additionally binds to the IPv6 address ::1.

Previously, starting from MongoDB 2.6, only the binaries from the official MongoDB RPM (Red Hat, CentOS, Fedora Linux, and derivatives) and DEB (Debian, Ubuntu, and derivatives) packages bind to localhost by default.

When bound only to the localhost, these MongoDB 3.6 binaries can only accept connections from clients (including the mongo shell, other members in your deployment for replica sets and sharded clusters) that are running on the same machine. Remote clients cannot connect to the binaries bound only to localhost.

To override and bind to other ip addresses, you can use the net.bindIp configuration file setting or the --bind_ip command-line option to specify a list of ip addresses.

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.

For example, the following mongod instance binds to both the localhost and the sample ip address 198.51.100.1:

mongod --bind_ip localhost,198.51.100.1

In order to connect to this instance, remote clients must specify the ip address 198.51.100.1 or the hostname associated with the ip address:

mongo --host 198.51.100.1

mongo --host My-Example-Associated-Hostname

Replica Set Configuration

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

Example

The following example 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 and the --bind_ip (or net.bindIp if using a configuration file) set appropriately such that other members of the replica set and clients can connect.

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.

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

Note

Run rs.initiate() on just one and only one mongod instance for the replica set.

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.