Replica set in MongoDB is a group of mongod processes that maintain the same data. Replica set provides redundancy and high availability and are the basis for all production deployment . this post will give simple and basic idea of create simple Mongod Replica set
We are going to create three mongodb for communicate each other to replica set
Lets say there are three database in replica set these are node1, node2, node3 and replica name is repl-test
Config file for node1database
~sudo nano /var/mongod/node1.conf
storage:
dbPath: /var/mongodb/db/node1
net:
bindIp: 192.168.103.100,localhost
port: 27011
security:
authorization: enabled
keyFile: /var/mongodb/pki/repl-keyfile
systemLog:
destination: file
path: /var/mongodb/db/node1/mongod.log
logAppend: true
processManagement:
fork: true
replication:
replSetName: repl-test
Then Save and Exit (CTRL + O and CTRL + X)
Lets create Directory for repl-keyfile
~sudo mkdir -p /var/mongodb/pki
~sudo chown User:User /var/mongodb/pki //Seting Permission
Now Run Command for create keyfile
~ openssl rand -base64 741 > /var/mongodb/pki/repl-keyfile
~ sudo chmod 400 /var/mongodb/pki/repl-keyfile //change permission
Now we have to create db path then have to set permission read
mkdir -p /var/mongodb/db/node1
~sudo chown User:User /var/mongodb/db //Seting Permission
Lets create node2 and node3 config files
node2.conf
storage:
dbPath: /var/mongodb/db/node2
net:
bindIp: 192.168.103.100,localhost
port: 27012
security:
authorization: enabled
keyFile: /var/mongodb/pki/repl-keyfile
systemLog:
destination: file
path: /var/mongodb/db/node2/mongod.log
logAppend: true
processManagement:
fork: true
replication:
replSetName: repl-test
node3.conf
storage:
dbPath: /var/mongodb/db/node3
net:
bindIp: 192.168.103.100,localhost
port: 27013
security:
authorization: enabled
keyFile: /var/mongodb/pki/repl-keyfile
systemLog:
destination: file
path: /var/mongodb/db/node3/mongod.log
logAppend: true
processManagement:
fork: true
replication:
replSetName: repl-test
Creating the data directories for node2 and node3:
mkdir /var/mongodb/db/{node2,node3}
Starting mongod processes with node1.conf, node2.conf and node3.conf:
mongod -f /var/mongod/node1.conf
mongod -f /var/mongod/node2.conf
mongod -f /var/mongod/node3.conf
We have created and started three database in replica set but there problem each node have no idea about there partners now all nodes al blind now we have to initiate replica set and add each node in replica set
First we have to connect to node1 (or Other node2, node 3) add user to authenticate because we have added authorization: enabled then we have to initiate replica set
mongo --port 27011 //Connect Mongo db
rs.initiate() //intiate replica set
use admin //swicth database to create user
db.createUser({
user: "repl-admin",
pwd: "repl-pass",
roles: [
{role: "root", db: "admin"}
]
}) // run command create user
Exiting out of the Mongo shell and connecting to the entire replica set:
mongo --host "repl-test/192.168.103.100:27011" -u "repl-admin"
-p "repl-pass" --authenticationDatabase "admin"
After you have connected with authentications run rs.status(). it will show everything about replica set it will show. how many members are int there. you can see there one member , because we did not add other nodes in the replica set. now lets add other nodes in the replica set
rs.add("192.168.103.100:27012")//host:port
rs.add("192.168.103.100:27013")//host:port
Now you run the
rs.isMaster()
It will shows all members, how many host int here. you can look at there are 3 hosts and also it shows primary database host. Now you able to change the primary to other replica member. Just run
rs.stepDown()
It will change the member for the primary you can run rs.isMaster(). it will show the master host and port
Simple idea about for creating replica set. If you want more about mongodb replica set you can check you can check Mongodb Documentation
Thanks