After one day and a half, bouncing between various resources, I finally managed to make a MongoDB Replica Set work. As it is my habit, I log my steps in a bloc-note for easy back tracking and effort saving for the future (It happens to everyone, but I get so annoyed to search twice a solution for the same problem). Those include solutions to some recurrent problems appearing down the way (and this what is special about this tutorial). I’m going to share these steps with some commentary.
Note that these are the minimum required “how-to” steps, which drove me to the final success. That said, if you want more in-depth explanations about the “what-is” part of the equation, like security considerations and performance optimizations, then, I recommend you look farther; I refer to the documentations [1] and one another interesting tutorial (that didn’t work for me for the “how-to” part) [2].
The steps are tested on a cluster of three nodes, each having Ubuntu 16.04 and MongoDB 3.6.1 installed.
- Download MongoDB as you would normally do on a single machine in each of the nodes. We recommend the tutorial on the official website: Install MongoDB.
At the end of the tutorial, you would be asked to start MongoDB server using:
1 |
sudo service mongod start |
If the service is not recognized, returning some error like: Failed to start mongodb.service: Unknown unit: mongodb.service
, then you need to enable it explicitly using:
1 |
sudo systemctl enable mongod |
Check if the server’s correctly started using:
1 |
sudo service mongod status |
- We need now to add two pieces of information to MongoDB config file: (1) the IP address of of the the node, and (2) the replica set name. Stop Mongod first:
1 |
sudo service mongod stop |
Open the config file for editing, like using vi: sudo vi /etc/mongod.conf
. Go down to net:
and set the bind IP address as a value for bindIp:
(to enable other nodes to talk to this node) . As we are creating a cluster of nodes, I recommend to omit the default localhost address 127.0.0.1 (and give less chance to errors):
1 2 3 |
net: port: 27017 bindIp: 172.180.10.160 |
Then go further down and comment out the line #replication:
. Provide the replica set name (without quotations preferably, saw people complaining from using them):
1 2 |
replication: replSetName: mongodb-rs |
Then… save. Read more Setup a MongoDB cluster (aka Replica Set) on Ubuntu