Deploy a Replica Set
This tutorial describes how to create a three-member replicaset from three existing mongod
instances running withaccess control disabled.
To deploy a replica set with enabled access control, seeDeploy Replica Set With Keyfile Authentication. If you wish to deploy areplica set from a single MongoDB instance, seeConvert a Standalone to a Replica Set. For moreinformation on replica set deployments, see the Replication andReplica Set Deployment Architectures documentation.
Overview
Three member replica sets provide enoughredundancy to survive most network partitions and other systemfailures. These sets also have sufficient capacity for many distributedread operations. Replica sets should always have an odd number ofmembers. This ensures that elections will proceed smoothly. For more aboutdesigning replica sets, see the Replication overview.
Requirements
For production deployments, you should maintain as much separation betweenmembers as possible by hosting the mongod
instances on separate machines. When using virtual machines forproduction deployments, you should place each mongod
instance on a separate host server serviced by redundant power circuitsand redundant network paths.
Before you can deploy a replica set, you must install MongoDB oneach system that will be part of your replica set.If you have not already installed MongoDB, see the installation tutorials.
Considerations When Deploying a Replica Set
Architecture
In production, deploy each member of the replica set to its own machineand if possible bind to the standard MongoDB port of 27017
.
See Replica Set Deployment Architectures for more information.
Hostnames
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.
IP Binding
Use the bind_ip
option to ensure that MongoDB listens forconnections from applications on configured addresses.
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
Connectivity
Ensure that network traffic can pass securely between all members of the set andall clients in the network .
Consider the following:
- Establish a virtual private network. Ensure that your network topologyroutes all traffic between members within a single site over the localarea network.
- Configure access control to prevent connections from unknown clientsto the replica set.
- Configure networking and firewall rules so that incoming and outgoingpackets are permitted only on the default MongoDB port and only fromwithin your deployment. See the IP Binding considerations.
Ensure that each member of a replica set is accessible byway of resolvable DNS or hostnames. You should either configure yourDNS names appropriately or set up your systems’ /etc/hosts
file toreflect this configuration.
Each member must be able to connect to every other member. Forinstructions on how to check your connection, seeTest Connections Between all Members.
Configuration
Create the directory where MongoDB stores data files before deployingMongoDB.
Specify the mongod
configuration in a configurationfile stored in /etc/mongod.conf
or a related location.
For more information about configuration options, seeConfiguration File Options.
Procedure
The following procedure outlines the steps to deploy a replica set whenaccess control is disabled.
Start each member of the replica set with the appropriate options.
For each member, start a mongod
instance with thefollowing settings:
- Set
replication.replSetName
option to the replica set name,
If your application connects to more than one replica set, each setshould have a distinct name. Some drivers group replica setconnections by replica set name.
Set
net.bindIp
option to the hostname/ip or a comma-delimitedlist of hostnames/ips, andSet any other settings as appropriate for your deployment.
In this tutorial, the three mongod
instances areassociated with the following hosts:
Replica Set Member | Hostname |
---|---|
Member 0 | mongodb0.example.net |
Member 1 | mongodb1.example.net |
Member 2 | mongodb2.example.net |
The following example specifies the replica set name and the ipbinding through the —replSet
and —bind_ip
command-line options:
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.
- mongod --replSet "rs0" --bind_ip localhost,<hostname(s)|ip address(es)>
For <hostname(s)|ip address(es)>
, specify the hostname(s) and/orip address(es) for your mongod
instance that remoteclients (including the other members of the replica set) can use toconnect to the instance.
Alternatively, you can also specify the replica set name
and the ip addresses
in a configuration file:
- replication:
- replSetName: "rs0"
- net:
- bindIp: localhost,<hostname(s)|ip address(es)>
To start mongod
with a configuration file, specify theconfiguration file’s path with the —config
option:
- mongod --config <path-to-config>
In production deployments, you can configure a init scriptto manage this process. Init scripts are beyond the scope of thisdocument.
Connect a mongo shell to one of the mongod instances.
From the same machine where one of the mongod
is running(in this tutorial, mongodb0.example.net
), start the mongo
shell. To connect to the mongod
listening to localhost onthe default port of 27017
, simply issue:
- mongo
Depending on your path, you may need to specify the path to themongo
binary.
Initiate the replica set.
From the mongo
shell, run rs.initiate()
onreplica set member 0.
Important
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 : "rs0",
- members: [
- { _id: 0, host: "mongodb0.example.net:27017" },
- { _id: 1, host: "mongodb1.example.net:27017" },
- { _id: 2, host: "mongodb2.example.net:27017" }
- ]
- })
MongoDB initiates a replica set, usingthe default replica set configuration.
View the replica set configuration.
Use rs.conf()
to display the replica set configurationobject:
- rs.conf()
The replica set configuration object resembles the following:
- {
- "_id" : "rs0",
- "version" : 1,
- "protocolVersion" : NumberLong(1),
- "members" : [
- {
- "_id" : 0,
- "host" : "mongodb0.example.net:27017",
- "arbiterOnly" : false,
- "buildIndexes" : true,
- "hidden" : false,
- "priority" : 1,
- "tags" : {
- },
- "slaveDelay" : NumberLong(0),
- "votes" : 1
- },
- {
- "_id" : 1,
- "host" : "mongodb1.example.net:27017",
- "arbiterOnly" : false,
- "buildIndexes" : true,
- "hidden" : false,
- "priority" : 1,
- "tags" : {
- },
- "slaveDelay" : NumberLong(0),
- "votes" : 1
- },
- {
- "_id" : 2,
- "host" : "mongodb2.example.net:27017",
- "arbiterOnly" : false,
- "buildIndexes" : true,
- "hidden" : false,
- "priority" : 1,
- "tags" : {
- },
- "slaveDelay" : NumberLong(0),
- "votes" : 1
- }
- ],
- "settings" : {
- "chainingAllowed" : true,
- "heartbeatIntervalMillis" : 2000,
- "heartbeatTimeoutSecs" : 10,
- "electionTimeoutMillis" : 10000,
- "catchUpTimeoutMillis" : -1,
- "getLastErrorModes" : {
- },
- "getLastErrorDefaults" : {
- "w" : 1,
- "wtimeout" : 0
- },
- "replicaSetId" : ObjectId("585ab9df685f726db2c6a840")
- }
- }
Ensure that the replica set has a primary.
Use rs.status()
to identify the primary in the replica set.
See also