|
| 1 | +# Deploy Replica Set |
| 2 | + |
| 3 | +A replica set in **MongoDB** is a group of mongod processes that maintain the same data set. Replica sets provide redundancy and high availability, and are the basis for all production deployments. This section provides tutorials for common tasks related to replica sets. |
| 4 | + |
| 5 | +## Requirements |
| 6 | + |
| 7 | +Before you can deploy a replica set, you must install MongoDB on each system that will be part of your replica set (we meed at least 3 instances). If you have not already installed MongoDB, see the [installation tutorials](https://docs.mongodb.org/manual/installation/#tutorial-installation "Installation"). |
| 8 | + |
| 9 | +All members of a replica set must be able to connect to every other member of the set to support replication. Always verify connections in both “directions.” Networking topologies and firewall configurations can prevent normal and required connectivity, which can block replication. |
| 10 | + |
| 11 | +To configure the network bind edit the mongo config file (/etc/mongod.conf under Unix systems): |
| 12 | + |
| 13 | + # network interfaces |
| 14 | + net: |
| 15 | + port: 27017 |
| 16 | + bindIp: PRIVATE-IP |
| 17 | + |
| 18 | +Be sure to setup internal DNS name to reach all the different nodes from one virtual machine to an other. You can simply modify the **/etc/hosts** file and add your DNS resolution, for example: |
| 19 | + |
| 20 | + 192.168.0.1 mongo1.alexcomu |
| 21 | + 192.168.0.2 mongo2.alexcomu |
| 22 | + 192.168.0.3 mongo3.alexcomu |
| 23 | + |
| 24 | +So, to check the bidirectional test of networking try from **mongo1**: |
| 25 | + |
| 26 | + `mongo --host mongo2.alexcomu --port 27017` |
| 27 | + `mongo --host mongo3.alexcomu --port 27017` |
| 28 | + |
| 29 | +From **mongo2**: |
| 30 | + |
| 31 | + `mongo --host mongo1.alexcomu --port 27017` |
| 32 | + `mongo --host mongo3.alexcomu --port 27017` |
| 33 | + |
| 34 | +From **mongo3**: |
| 35 | + |
| 36 | + `mongo --host mongo1.alexcomu --port 27017` |
| 37 | + `mongo --host mongo2.alexcomu --port 27017` |
| 38 | + |
| 39 | +If any connection, in any direction fails, check your networking and firewall configuration and reconfigure your environment to allow these connections. |
| 40 | + |
| 41 | +## Configuration Procedure |
| 42 | + |
| 43 | +The following procedure outlines the steps to deploy a replica set when access control is disabled. |
| 44 | + |
| 45 | +### 1 - Start each member of the replica set with appropriate options |
| 46 | + |
| 47 | +For each member, start a **mongod** and specify the replica set option (**replSet**). If the application connects to more than one replica set, each set should have a dinstict name. In our case we'll use the same replica set name, for instance: |
| 48 | + |
| 49 | + mongod --replSet "rs0" |
| 50 | + |
| 51 | +We can also specify the replica set name in the configuration file (be careful, YAML syntax does not support indentation, use space!): |
| 52 | + |
| 53 | + replication: |
| 54 | + oplogSizeMB: <int> |
| 55 | + replSetName: <string> |
| 56 | + secondaryIndexPrefetch: <string> |
| 57 | + enableMajorityReadConcern: <boolean> |
| 58 | + |
| 59 | +### 2 - Connect a mongo shell to a replica set member |
| 60 | + |
| 61 | +Connect your shell to Mongo: |
| 62 | + |
| 63 | + mongo --host 192.168.0.1 --port 27017 |
| 64 | + |
| 65 | +### 3 - Initiate the replica set |
| 66 | + |
| 67 | +On one and one only member of the replica set, use **rs.initiate()** to initiates a set that consists of the current member and that uses the default replica set configuration. |
| 68 | + |
| 69 | + rs.initiate() |
| 70 | + |
| 71 | +### 4 - Verify the initial replica set configuration |
| 72 | + |
| 73 | +Use **rs.status** to display the replica set configuration object: |
| 74 | + |
| 75 | + rs.status() |
| 76 | + |
| 77 | +The replica set configuration object will looks like: |
| 78 | + |
| 79 | + rs1:PRIMARY> rs.conf() |
| 80 | + { |
| 81 | + _id" : "rs1", |
| 82 | + "version" : 1, |
| 83 | + "protocolVersion" : NumberLong(1), |
| 84 | + "members" : [ |
| 85 | + { |
| 86 | + "_id" : 0, |
| 87 | + "host" : "192.168.0.1:27017", |
| 88 | + "arbiterOnly" : false, |
| 89 | + "buildIndexes" : true, |
| 90 | + "hidden" : false, |
| 91 | + "priority" : 1, |
| 92 | + "tags" : { |
| 93 | + }, |
| 94 | + "slaveDelay" : NumberLong(0), |
| 95 | + "votes" : 1 |
| 96 | + } |
| 97 | + ] |
| 98 | + } |
| 99 | + |
| 100 | +### 5 - Add the remaining members to the replica set. |
| 101 | + |
| 102 | +We have to add the remaining members to our replica set using **rs.add()**. You must be connected to the **primary** member to add the others, in our case is **mongo1.alexcomu** instance. |
| 103 | + |
| 104 | + rs.add("mongo2.alexcomu") |
| 105 | + rs.add("mongo3.alexcomu") |
| 106 | + |
| 107 | +Check the status to identify the primary in replica set. And we're done! Congrats! |
| 108 | + |
| 109 | +### 5 Enable read-mode. |
| 110 | + |
| 111 | +From a secondary node is possible to enable the read mode using this simple command: |
| 112 | + |
| 113 | + rs.slaveOk() |
| 114 | + |
| 115 | + |
0 commit comments