MongoDB replica set

Three Node MongoDB Replica


Replica set


Configure the package management system (yum)

  • First go to /etc/yum.repos.d directory:
cd /etc/yum.repos.d/
  • Create a new file (In this case mongodb-org-4.4.repo). Now add the following lines to the file.
[mongodb-org-4.4]name=MongoDB Repositorybaseurl=$releasever/mongodb-org/4.4/x86_64/gpgcheck=1enabled=1gpgkey=

Install the MongoDB packages

sudo yum install -y mongodb-org

Run MongoDB as a service

  • To start mongo as daemon(background process):
sudo systemctl start mongod
  • To check status:
sudo systemctl status mongod
  • To stop:
sudo systemctl stop mongod
  • Go to mongo console:

Setting up replica set

replSetName: “REPLICA_NAME”
bindIp: localhost,OTHER_IP_ADDRESS/HOST_NAME ( for allowing all ips)
sudo systemctl daemon-reload
sudo systemctl status mongod

Initiating the replica set

rs.initiate( {
members: [
{ _id: 0, host: "" },
{ _id: 1, host: "" },
{ _id: 2, host: "" }
use admin


  1. In /etc/mongod.conf file never use for ip_bind option it will allow all ips to interact with the db.
  2. The article assumes all default options for initial configuration. You can change the port number, add authorization from the /etc/mongod.conf file. Always verify mongod is running fine after any change in this file.
  3. With version change, commands and configurations for mongo change a lot. To stop updating the version after installation, add the line in the /etc/yum.conf file:



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store