rs.initiate()
Description
rs.
initiate
(configuration)- Initiates a replica set. Optionally, the method can take anargument in the form of a document that holds theconfiguration of a replica set.
The rs.initiate()
method has the following parameter:
ParameterTypeDescriptionconfiguration
documentOptional. A document that specifies configuration for the new replica set. If aconfiguration is not specified, MongoDB uses a defaultreplica set configuration.
The rs.initiate()
method provides a wrapper around thereplSetInitiate
command.
IP Binding
Starting in MongoDB 3.6, MongoDB binaries, mongod
andmongos
, bind to localhost by default. If thenet.ipv6
configuration file setting or the —ipv6
command line option is set for the binary, the binary additionally bindsto the localhost IPv6 address.
Previously, starting from MongoDB 2.6, only the binaries from theofficial MongoDB RPM (Red Hat, CentOS, Fedora Linux, and derivatives)and DEB (Debian, Ubuntu, and derivatives) packages bind to localhost bydefault.
When bound only to the localhost, these MongoDB 3.6 binaries can onlyaccept 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 tothe binaries bound only to localhost.
To override and bind to other ip addresses, you can use thenet.bindIp
configuration file setting or the—bind_ip
command-line option to specify a list of hostnames or ipaddresses.
Warning
Before binding to a non-localhost (e.g. publicly accessible)IP address, ensure you have secured your cluster from unauthorizedaccess. For a complete list of security recommendations, seeSecurity Checklist. At minimum, considerenabling authentication andhardening network infrastructure.
For example, the following mongod
instance binds to boththe localhost and the hostname My-Example-Associated-Hostname
, which isassociated with the ip address 198.51.100.1
:
- mongod --bind_ip localhost,My-Example-Associated-Hostname
In order to connect to this instance, remote clients must specifythe hostname or its associated ip address 198.51.100.1
:
- mongo --host My-Example-Associated-Hostname
- mongo --host 198.51.100.1
Replica Set Configuration
See Replica Set Configuration Document Example for details of replicaset configuration document.
Tip
When possible, use a logical DNS hostname instead of an ip address,particularly when configuring replica set members or sharded clustermembers. The use of logical DNS hostnames avoids configurationchanges due to ip address changes.
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 (orreplication.replSetName
if using a configuration file) setto myReplSet
and the —bind_ip
(or net.bindIp
if using a configuration file) set appropriately such that othermembers 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 unauthorizedaccess. For a complete list of security recommendations, seeSecurity Checklist. At minimum, considerenabling authentication andhardening 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 onemongod
instance for the replica set.
Tip
When possible, use a logical DNS hostname instead of an ip address,particularly when configuring replica set members or sharded clustermembers. The use of logical DNS hostnames avoids configurationchanges due to ip address changes.
- 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, seeReplica Set Configuration Fields.
For details on deploying a replica set, seeDeploy a Replica Set.
See also