One of my previous works was to set up an infrastructure with MongoDB. If you asked me why MongoDB, I would say it was due to sort of legacy. The thing was, I had been struggling for replication installation until I found a way to complete. In this post, I share my steps to achieve with some references.

Foremost, a replica set means there are several database nodes maintaining the same data set. In reality, my application will connect to these nodes, Mongo itself will allow writing onto its primary node while secondary will clone data after a short particular period time. When the primary has a problem, the cluster will vote to differentiate new primary and secondaries.

My problem was the incorrect configuration for my cluster, therefore, these nodes can not communicate. My configuration used IP information which will probably cause some communication problems when these addresses change. Anyway, my post just shows how it works. Bear in mind that I use Docker to install MongoDB so need to install Docker first.

Steps

Install mongo nodes 1

The default port of mongo is 27017, I installed 3 nodes on 1 machine (suppose my real IP is 10.164.5.85) so I have to open 3 ports for mapping to each mongo container, otherwise I will meet further issues. You can start from 27017 but I choose from 27018. Three containers are mongo1, mongo2 and mongo3 respectively. Replica Set name is rs0.

sudo docker run \
-p 27018:27017 \
--name mongo1 \
--net curator-cluster \
mongo mongod --replSet rs0
sudo docker run  -it\
-p 27019:27017 \
--name mongo2 \
--net curator-cluster \
mongo mongod --replSet rs0
sudo docker run \
-p 27020:27017 \
--name mongo3 \
--net curator-cluster \
mongo mongod --replSet rs0

My configuration

If you make an incorrect configuration, these nodes can not find each other 2 or you can get MongoDB connection error 3.

config = {
  "_id" : "rs0",
  "members" : [
    {"_id" : 0, "host" : "10.164.5.85:27018"},
    {"_id" : 1, "host" : "10.164.5.85:27019"},
    {"_id" : 2, "host" : "10.164.5.85:27020"}
    ]
  }
rs.initiate(config)

Verification

To verify, you can use this command

mongo --host replicaSetName/host1[:porthost1],host2[:porthost1],host3[:porthost3] databaseToConnect

for instance:

mongo --host rs0/10.164.5.85:27018,10.164.5.85:27019,10.164.5.85:27020 schedulerinterface-staging

References


Bryan Nguyễn

Full Stack-Over-Flow Engineer