How to create replica sets in MongoDB?

Vikas Kohli
3 min readApr 9, 2019

--

Let’s assume, we have three servers

a server with ip 1.1.1.1

b server with ip 2.2.2.2

c server with ip 3.3.3.3

Connect all the servers, here I m using AWS ubuntu servers

sudo ssh -i <path-to-pemfile> username@ipExamples:-For ‘a’ server:- sudo ssh -i a-pemfile.pem username@1.1.1.1For ‘b’ server:- sudo ssh -i b-pemfile.pem username@2.2.2.2For ‘c’ server:- sudo ssh -i apemfile.pem username@3.3.3.3

Now install the MongoDB in all the three servers a, b and c.

The MongoDB configuration file available in “/etc/mongod.conf”

# mongod.conf# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
dbPath: /var/lib/mongodb
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0
# how the process runs
processManagement:
timeZoneInfo: /usr/share/zoneinfo
#security:#operationProfiling:#replication#sharding:#Enterprise-Only Options:#auditLog:#snmp:

Start MongoDB by using the following command:-

mongod --fork --config /etc/mongod.conf

Run mongodb

mongo --port 27017 //if you are not changing port from 27017 then port flag is not mandatory

Create an admin user

use admindb.createUser( { user: "admin_user",pwd: "adminpassword",roles: [ "root" ]} );//Now create database user
use myreplicadb
db.createUser({user: "username",pwd: "123456",roles: [ {role:"readWrite", db:"myreplicadb"} ],})

Now enable the authentication in the /etc/mongod.conf file

security:
authorization: 'enabled' //enabling security

Create the key file by using the following command:

openssl rand -base64 756 > mongo.keymongo.key saved under /home/ubuntu
chmod 400 /home/ubuntu/mongo.key //changing permission

The above procedure should be done in all the three servers, but the key file that is generated above should be the same in all the servers.

View the mongo.key file:- cat /home/ubuntu/mongo.key and copy all the contents in that file and paste to the mongo.key files in the other servers.

Now we have to add the hostname of every server

Open host file:- nano /etc/hosts

Inside the file, we need to add the private IPs of every server including itself

11.11.11.11 replica-db1 #this one is private ip of the a's server22.22.22.22 replica-db2 #this one is private ip of the b's server33.33.33.33 replica-db3 #this one is private ip of the c's server

Similarly, add this in other servers too. After adding the same, add the hostname.

Open hostname file
nano /etc/hostname

Add your hostname that we've written in /etc/hosts filereplica-db1

Exit from the hostname file and run

hostname replica-db1
hostname -f
ping replica-db1 //if everything works fine, it will able to get all the packets

Similarly, in the other servers too. Now check by pinging b and c server from a’s server and vice-versa. If you’re not losing a packet, then you’re on the right track

Now add the key file and enable the replication in MongoDB configuration file

nano /etc/mongod.conf

security:
authorization: 'enabled'
keyFile: /home/ubuntu/mongo.key
replication:
replSetName: replDbSet

Now connect to the MongoDB but before that stop the MongoDB

ps aux | grep mongo #kill the mongo processkill -9 processorid#Now start the mongo process

mongod --fork --config /etc/mongod.conf

Similarly for the b and c server too.

Now I’ll make a server DB as primary, b as secondary and c as arbiter

For this went to a server, open the MongoDB terminal

mongo --port 27017 --authenticationDatabase admin -u admin_user -p ‘adminpassword’#initiate the replicationrs.initiate()
rs.add("replica-db2:27017")
rs.addArb("replica-db3:27017")
#You can check whether its listening or not by using
rs.status()

Now you can write from only a’s server but you can read from a server as well as from b’s server. The arbiter server will change the primary server from a’s to b in case a’s server is down.

For reconfiguration of the replication members, we need to use different variable

cfg = rs.conf()
cfg.members.splice(3,1)
rs.reconfig(cfg, {force : true}) //forcefully reconfig the new configuration

Thanks for the reading :)

--

--

Vikas Kohli
Vikas Kohli

Written by Vikas Kohli

B.E Software Developer, Enthusiastic, Ego-surfing

No responses yet