Mongo DB Replication POC PDF
Mongo DB Replication POC PDF
This document is STRICTLY CONFIDENTIAL and PROPRIETARY. Any use or disclosure for purposes
other than intent of the original distribution of this document is prohibited. Copyright © 2020. All
rights reserved. It cannot be reproduced without the express permission of Xybion except for
internal distribution within Xybion.
Page 1 of 13
Contents
About MongoDB Replication ............................................................................................................. 3
MongoDB Installation and Replication setup ..................................................................................... 4
Creating service user: .................................................................................................................... 5
Creating the base directory structure ............................................................................................ 5
Download and installing MongoDB TarBall .................................................................................... 6
Creating config file for Mongodb ................................................................................................... 6
Config – 1 .................................................................................................................................. 6
Config – 2 .................................................................................................................................. 6
Creating systemd service file ......................................................................................................... 7
Create admin users ........................................................................................................................ 8
Starting the replication process ..................................................................................................... 8
Testing the replication ................................................................................................................. 11
Populating the data ..................................................................................................................... 12
Enable TLS Encryption over data motion ...................................................................................... 12
Reference ........................................................................................................................................ 13
Page 2 of 13
About MongoDB Replication
MongoDB achieves replication using replica set. A replica set is a group of mongo instances that host
the same data set. In a replica, one node is primary node that receives all write operations. All other
instances, such as secondaries, apply operations from the primary so that they have the same data
set. Replica set can have only one primary node.
1. Replica set is a group of two or more nodes (generally minimum 3 nodes are required).
2. In a replica set, one node is primary node and remaining nodes are secondary.
3. All data replicates from primary to secondary node.
4. At the time of automatic failover or maintenance, election establishes for primary and a new
primary node is elected.
After the recovery of failed node, it again joins the replica set and works as a secondary node.
Page 3 of 13
A typical diagram of MongoDB replication is shown in which client application always interact with
the primary node and the primary node then replicates the data to the secondary nodes.
Page 4 of 13
Prerequisite:
Please note: Below operation should be done on all the cluster machine.
Once service user is created login as root and provide below sudo access.
visudo
appuser ALL=(ALL:ALL) NOPASSWD: /bin/systemctl restart mongod
│── xybion
│ │── apps
│ │─ certs
│ │── environments
│ │── scripts
│ │ │── backup
│ │ └── cronlogs
│ └── src
└── xybion-data
│── logs
│── mongodata
mkdir -p /opt/xybion/apps
mkdir -p /opt/xybion/certs
mkdir -p /opt/xybion/environments
mkdir -p /opt/xybion/scripts/backup/
mkdir -p /opt/xybion/scripts/cronlogs
mkdir -p /opt/xybion/src
mkdir -p /opt/xybion-data/logs
mkdir -p /opt/xybion-data/mongodata
Page 5 of 13
Now change the ownership of the directory.
chown -R appuser:appuser /opt/xybion*
Config – 1
This config will have basic parameters to start the MongoDB and do basic operation before starting
the cluster.
Config contents:
systemLog:
destination: file
path: "/opt/xybion-data/logs/mongo.log"
logAppend: true
logRotate: reopen
storage:
dbPath: /opt/xybion-data/mongodata
processManagement:
fork: true
pidFilePath: /var/run/mongodb/mongod.pid
net:
bindIp: 0.0.0.0
port: 27017
setParameter:
enableLocalhostAuthBypass: false
replication:
replSetName: "PROVIDE THE REPLICA SET NAME (EG: rs-xdp-poc)"
Config – 2
Config 2 has all the parameter to bootstrap the replica set cluster with SSL enabled to encrypt data
in motion.
Page 6 of 13
Config file path: /opt/xybion/apps/mongodb/bin/mongod.conf-001
Config contents:
systemLog:
destination: file
path: "/opt/xybion-data/logs/mongo.log"
logAppend: true
logRotate: reopen
storage:
dbPath: /opt/xybion-data/mongodata
processManagement:
fork: true
pidFilePath: /var/run/mongodb/mongod.pid
net:
bindIp: 0.0.0.0
port: 27017
ssl:
mode: requireSSL
PEMKeyFile: /opt/xybion/certs/mongo.pem
CAFile: /opt/xybion/certs/mongoca.pem
clusterFile: /opt/xybion/certs/mongo.pem
setParameter:
enableLocalhostAuthBypass: false
replication:
replSetName: "rs-xdp-poc"
security:
authorization: "enabled"
clusterAuthMode: x509
Config contents:
[Unit]
Description=MongoDB Database Server
Documentation=https://docs.mongodb.org/manual
After=network.target
[Service]
User=appuser
Group=appuser
ExecStart=/opt/xybion/apps/mongodb/bin/mongod --config /opt/xybion/apps/mongodb/bin/mongod.conf
ExecStartPre=/bin/mkdir -p /var/run/mongodb
ExecStartPre=/bin/chown appuser:appuser /var/run/mongodb
ExecStartPre=/bin/chmod 0755 /var/run/mongodb
PermissionsStartOnly=true
Page 7 of 13
PIDFile=/var/run/mongodb/mongod.pid
# Hacks and Tweaks - Begin #
LimitFSIZE=infinity
LimitCPU=infinity
LimitAS=infinity
LimitNOFILE=64000
LimitNPROC=64000
LimitMEMLOCK=infinity
TasksMax=infinity
# Hacks and Tweaks - End #
TasksAccounting=false
Type=forking
[Install]
WantedBy=multi-user.target
Now issue daemon reload command to reload the Systemd service files and followed by start the
service.
systemctl daemon-reload
systemctl start mongod
systemctl status mongod
Please note: Make sure the same steps have been followed in all the cluster machines.
Now the mongo will login without any password as the authentication is disabled. To enable
authentication, we need to create the admin users.
./opt/xybion/apps/mongodb/bin/mongo
use admin
db.createUser({user: 'admin', pwd: redacted', roles:['root']})
Page 8 of 13
members: [
{ id: 0, host: "mongo-rs001.xybion.com:27017","priority": 10 },
{ id: 1, host: "mongo-rs002.xybion.com:27017", "priority": 5 },
{ id: 2, host: "mongo-rs003.xybion.com:27017", "priority": 0.5 }
]
}
)
Once the replication is started, we can see the status of it using the below commands:
rs.status().members
Page 9 of 13
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "replica003.xybion.com:27017",
"syncSourceHost" : "replica003.xybion.com:27017",
"syncSourceId" : 2,
"infoMessage" : "",
"configVersion" : 2
},
{
"_id" : 2,
"name" : "replica003.xybion.com:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 9184,
"optime" : {
"ts" : Timestamp(1585830991, 1),
"t" : NumberLong(17)
},
"optimeDurable" : {
"ts" : Timestamp(1585830991, 1),
"t" : NumberLong(17)
},
"optimeDate" : ISODate("2020-04-02T12:36:31Z"),
"optimeDurableDate" : ISODate("2020-04-02T12:36:31Z"),
"lastHeartbeat" : ISODate("2020-04-02T12:36:34.539Z"),
"lastHeartbeatRecv" : ISODate("2020-04-02T12:36:34.540Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "replica001.xybion.com:27017",
"syncSourceHost" : "replica001.xybion.com:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 2
}
Page 10 of 13
Testing the replication
Let insert a simple document in primary node.
./opt/xybion/apps/mongodb/bin/mongo
use testdb
db.createCollection("testcollection")
db.testcollection.insertOne(
{
DB_Name: "mongodb",
Operation_Name: "replication"
})
Now check whether the data reach the secondary nodes. Login to secondary node and access mongo
shell. Then use the below commands.
rs.slaveOk()
use testdb
db. testcollection.find().pretty()
Page 11 of 13
Populating the data
Now we can populate the data to the cluster using the below commands.
./opt/xybion/apps/mongodb/bin/mongorestore --db DBNAME /path/to/db/directory/
Once DB population is done, the same data should be available in the replica nodes.
• mongo.pem – This will contain the private key and hostname certificate.
• mongoca.pem – This will contain the root certificates of GoDaddy.
Page 12 of 13
Reference
• https://docs.mongodb.com/manual/tutorial/troubleshoot-replica-sets/#troubleshoot-
replica-sets
• https://docs.mongodb.com/manual/tutorial/adjust-replica-set-member-priority/
• https://docs.mongodb.com/manual/tutorial/configure-ssl/
• https://subscription.packtpub.com/book/big_data_and_business_intelligence/97817871264
80/4/ch04lvl1sec41/changing-priority-to-replica-set-nodes
Page 13 of 13