MongoDB replica set

Three Node MongoDB Replica

Introduction

Replica set

Installation

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=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.4/x86_64/gpgcheck=1enabled=1gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc

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:
mongo

Setting up replica set

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

Initiating the replica set

rs.initiate( {
_id : "REPLICA_NAME",
members: [
{ _id: 0, host: "mongodb0.example.net:27017" },
{ _id: 1, host: "192.168.40.5:27017" },
{ _id: 2, host: "mongodb2.example.net:27017" }
]
})
rs.conf()
use admin
db.shutdownServer()
rs.stepDown();

Notes

  1. In /etc/mongod.conf file never use 0.0.0.0 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:
exclude=mongodb-org,mongodb-org-server,mongodb-org-shell,mongodb-org-mongos,mongodb-org-tools

--

--

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