Deploy a Replica Set for Testing and Development

This procedure describes deploying a replica set in a development ortest environment. For a production deployment, refer to theDeploy a Replica Set tutorial.

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 test and development systems, you can run your mongodinstances on a local system, or within a virtual instance.

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.

Each member must be able to connect to every other member. Forinstructions on how to check your connection, seeTest Connections Between all Members.

Considerations

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

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

In this test deployment, the three members run on the same machine.

Replica Set Naming

Important

These instructions should only be used for test ordevelopment deployments.

The examples in this procedure create a new replica set named rs0.

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.

Procedure

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.

  • Create the necessary data directories for each member by issuing acommand similar to the following:
  1. mkdir -p /srv/mongodb/rs0-0 /srv/mongodb/rs0-1 /srv/mongodb/rs0-2

This will create directories called “rs0-0”, “rs0-1”, and “rs0-2”, whichwill contain the instances’ database files.

  • Start your mongod instances in their own shell windows by issuing the followingcommands:

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.

First member:

  1. mongod --replSet rs0 --port 27017 --bind_ip localhost,<hostname(s)|ip address(es)> --dbpath /srv/mongodb/rs0-0 --oplogSize 128

Second member:

  1. mongod --replSet rs0 --port 27018 --bind_ip localhost,<hostname(s)|ip address(es)> --dbpath /srv/mongodb/rs0-1 --oplogSize 128

Third member:

  1. mongod --replSet rs0 --port 27019 --bind_ip localhost,<hostname(s)|ip address(es)> --dbpath /srv/mongodb/rs0-2 --oplogSize 128

This starts each instance as a member of a replica set namedrs0, each running on a distinct port, and specifies the path toyour data directory with the —dbpath setting.If you are already using the suggested ports, select different ports.

The instances bind to both the localhost and the ip addressof the host.

The —oplogSize setting reduces thedisk space that each mongod instance uses. [1]This is ideal for testing and development deployments as it preventsoverloading your machine. For more information on this and otherconfiguration options, see Configuration File Options.

  • Connect to one of your mongod instances through themongo shell. You will need to indicate which instance byspecifying its port number. For the sake of simplicity and clarity,you may want to choose the first one, as in the following command;
  1. mongo --port 27017
  • In the mongo shell, use rs.initiate() toinitiate the replica set. You can create a replica setconfiguration object in the mongo shell environment, asin the following example:
  1. rsconf = {
  2. _id: "rs0",
  3. members: [
  4. {
  5. _id: 0,
  6. host: "<hostname>:27017"
  7. },
  8. {
  9. _id: 1,
  10. host: "<hostname>:27018"
  11. },
  12. {
  13. _id: 2,
  14. host: "<hostname>:27019"
  15. }
  16. ]
  17. }

replacing <hostname> with your system’s hostname,and then pass the rsconf file to rs.initiate() asfollows:

  1. rs.initiate( rsconf )
  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" : "<hostname>: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" : "<hostname>:27018",
  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" : "<hostname>:27019",
  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. "settings" : {
  47. "chainingAllowed" : true,
  48. "heartbeatIntervalMillis" : 2000,
  49. "heartbeatTimeoutSecs" : 10,
  50. "electionTimeoutMillis" : 10000,
  51. "catchUpTimeoutMillis" : -1,
  52. "getLastErrorModes" : {
  53.  
  54. },
  55. "getLastErrorDefaults" : {
  56. "w" : 1,
  57. "wtimeout" : 0
  58. },
  59. "replicaSetId" : ObjectId("598f630adc9053c6ee6d5f38")
  60. }
  61. }

Check the status of your replica set at any time with thers.status() operation.

See also

The documentation of the following shell functions formore information:

You may also consider the simple setup scriptas an example of a basic automatically-configured replica set.

Refer to Replica Set Read and Write Semanticsfor a detailed explanation of read and write semantics in MongoDB.

[1]Starting in MongoDB 4.0, the oplog can grow past its configured sizelimit to avoid deleting the majority commit point.