Setting up a replica cluster with MongoDB 4.4
I’ve been working with a very optimization hungry application that will handle a huge amount of important ( approx50 billion read and writes ) data that requires replication and sharing at the same time. Here I’ll explain how you’ll set up multiple replications to your MongoDB 4.4 database
Follow this instruction for installing MongoDB on your server on ubuntu. I’m using the ubuntu cloud server for this demo.
First, let’s make our MongoDB server available to the public, run this command bellow. go to “/etc/mongod.conf” and change the bind IP from 127.0.0.1 to 0.0.0.0 and restart MongoDB
sudo service mongod restart
We’ll have 4 mongo replica instances where 1 will be used as primary and others will use as secondary. In order to do that let’s create 4 directories where individual replica sets will store their data.
Now with ps -ef | grep mongod command you can see all your replica MongoDB instances are running then we’ll log in to one of the replica servers I’ll log into the replica that is running in port 27018.
Run this code inside in the replica server of 27018 and wait some time for turn itself into the primary node. You can put any IP instead of “localhost”. Now go to 27017,27109,27020 mongo replica instances and execute rs.secondaryOk() . This means other nodes accepted themselves as a secondary node. you can tweak their priority and voting rights with rs config also from the primary.
And That’s it! this is how you setup replica servers.