replSetInitiate
On this page
replSetInitiate
The
replSetInitiate
command initializes a new replica set.Tip
In the
mongo
Shell, this command can also be run through thers.initiate()
helper method.Helper methods are convenient for
mongo
users, but they may not return the same level of information as database commands. In cases where the convenience is not needed or the additional return fields are required, use the database command.To run
replSetInitiate
, use thedb.runCommand( { <command> } )
method.Note
Run the command on only one of the
mongod
instances for the replica set.Use the following syntax:
{ replSetInitiate : <config_document> } The
<config_document>
is a document that specifies the replica set's configuration. For instance, here's a config document for creating a simple 3-member replica set:{ _id : <setname>, members : [ {_id : 0, host : <host0>}, {_id : 1, host : <host1>}, {_id : 2, host : <host2>}, ] } Tip
When possible, use a logical DNS hostname instead of an ip address, particularly when configuring replica set members or sharded cluster members. The use of logical DNS hostnames avoids configuration changes due to ip address changes.
IP Binding
Starting in MongoDB 3.6, MongoDB binaries, mongod
and
mongos
, bind to localhost 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 localhost IPv6 address.
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 hostnames or 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 hostname My-Example-Associated-Hostname
, which is
associated 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 specify
the hostname or its associated ip address 198.51.100.1
:
mongo --host My-Example-Associated-Hostname mongo --host 198.51.100.1
Example
Assign a config document to a variable and then to pass the document to
the rs.initiate()
helper:
config = { _id : "my_replica_set", members : [ {_id : 0, host : "rs1.example.net:27017"}, {_id : 1, host : "rs2.example.net:27017"}, {_id : 2, host : "rs3.example.net", arbiterOnly: true}, ] } rs.initiate(config)
Tip
When possible, use a logical DNS hostname instead of an ip address, particularly when configuring replica set members or sharded cluster members. The use of logical DNS hostnames avoids configuration changes due to ip address changes.
Notice that omitting the port cause the host to use the default port
of 27017. Notice also that you can specify other options in the config
documents such as the arbiterOnly
setting in this example.