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 mongodinstances on separate machines. When using virtual machines forproduction deployments, you should place each mongodinstance 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 —ipv6command 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:

  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:

  1. mongo --host My-Example-Associated-Hostname
  2.  
  3. 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.confor 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:

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, and

  • Set any other settings as appropriate for your deployment.

In this tutorial, the three mongod instances areassociated with the following hosts:

Replica Set MemberHostname
Member 0mongodb0.example.net
Member 1mongodb1.example.net
Member 2mongodb2.example.net

The following example specifies the replica set name and the ipbinding through the —replSet and —bind_ipcommand-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.

  1. 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:

  1. replication:
  2. replSetName: "rs0"
  3. net:
  4. bindIp: localhost,<hostname(s)|ip address(es)>

To start mongod with a configuration file, specify theconfiguration file’s path with the —config option:

  1. 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 mongoshell. To connect to the mongod listening to localhost onthe default port of 27017, simply issue:

  1. 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.

  1. rs.initiate( {
  2. _id : "rs0",
  3. members: [
  4. { _id: 0, host: "mongodb0.example.net:27017" },
  5. { _id: 1, host: "mongodb1.example.net:27017" },
  6. { _id: 2, host: "mongodb2.example.net:27017" }
  7. ]
  8. })

MongoDB initiates a replica set, usingthe default replica set configuration.

View the replica set configuration.

Use rs.conf() to display the replica set configurationobject:

  1. rs.conf()

The replica set configuration object resembles the following:

  1. {
  2. "_id" : "rs0",
  3. "version" : 1,
  4. "protocolVersion" : NumberLong(1),
  5. "members" : [
  6. {
  7. "_id" : 0,
  8. "host" : "mongodb0.example.net:27017",
  9. "arbiterOnly" : false,
  10. "buildIndexes" : true,
  11. "hidden" : false,
  12. "priority" : 1,
  13. "tags" : {
  14.  
  15. },
  16. "slaveDelay" : NumberLong(0),
  17. "votes" : 1
  18. },
  19. {
  20. "_id" : 1,
  21. "host" : "mongodb1.example.net:27017",
  22. "arbiterOnly" : false,
  23. "buildIndexes" : true,
  24. "hidden" : false,
  25. "priority" : 1,
  26. "tags" : {
  27.  
  28. },
  29. "slaveDelay" : NumberLong(0),
  30. "votes" : 1
  31. },
  32. {
  33. "_id" : 2,
  34. "host" : "mongodb2.example.net:27017",
  35. "arbiterOnly" : false,
  36. "buildIndexes" : true,
  37. "hidden" : false,
  38. "priority" : 1,
  39. "tags" : {
  40.  
  41. },
  42. "slaveDelay" : NumberLong(0),
  43. "votes" : 1
  44. }
  45.  
  46. ],
  47. "settings" : {
  48. "chainingAllowed" : true,
  49. "heartbeatIntervalMillis" : 2000,
  50. "heartbeatTimeoutSecs" : 10,
  51. "electionTimeoutMillis" : 10000,
  52. "catchUpTimeoutMillis" : -1,
  53. "getLastErrorModes" : {
  54.  
  55. },
  56. "getLastErrorDefaults" : {
  57. "w" : 1,
  58. "wtimeout" : 0
  59. },
  60. "replicaSetId" : ObjectId("585ab9df685f726db2c6a840")
  61. }
  62. }

Ensure that the replica set has a primary.

Use rs.status() to identify the primary in the replica set.

See also

Deploy Replica Set With Keyfile Authentication