How to create Replica sets in Mongodb.
Hi guys,This story is about how to create replica sets in mongodb.
Firstly what is replica set and why we need to create one?
In layman language ,replica sets are like making duplicate copies of your data to multiple places.How replica sets are helpful in high availability i’ll explain in between.
Before going into the details of how replication works we need to understand the working of database,how it operates.
Suppose you have created a cool website for your organization ,in the starting there were only 30–40 people who were using this website.Everything was going well there was not so much load on the database.But your website got a sudden hype and the number of concurrent user increase from 40 to 150 and keep on growing.Now what happen is all the load is happening on single database and the request are taking more time than usual to respond , that is when the concept of replication hits.Instead of throwing the load on single instance we create multiple instance and that load get distributed among all.
For this to understand you must have basic understanding of how mongoDB works.mongoDB is a nosql.It is different from all other relation database and quite popular and have impressive performance.One more use of replication is it With multiple copies of data on different database servers, replication provides a level of fault tolerance against the loss of a single database server means if due to any problem the database got crashed or dumped there is nothing to worry about because there are multiple copies of same data on the other nodes that will recover the lost data.
Let’s just jump into how to create replica sets.
For understanding replication you must understand the concept of primary and secondary nodes.By default there is only one primary node .All the write operations are happen on primary node .The minimum architecture of the replica set must have three nodes,One primary node and two secondary node.When the primary node goes down ,there goes a election between the secondary nodes that decide who got to be the next primary node and hence continue serving the request ,when the old primary node recover from the failure it joins the group again.
Write operation can only be performed on the primary nodes and the data get replicated on the other nodes.On the other hand read operation can be performed on any of the nodes .
If one node is unavailable right now the other nodes are capable of performing the desired function.
High Availability: Replica sets use elections to support high availability. Elections occur when the primary becomes unavailable and the replica set members autonomously select a new primary
Lets jump into the code.
First of all what we need to do is create separated storage folder for each replica set.The same thing we do after installing mongodb. Create data/db
folder to store data.If you want to create there replica set you have to create there separate folders for storage info.
you can simply create three separate folders and a folder inside by following simple commands.
cd c:\
mkdir data
mkdir data1
mkdir data2cd data
mkdir db
cd ..cd data1
mkdir db1
cd..cd data2
mkdir db2
cd ..
Now you have created three separate storage places for replica sets.
Now you need to create three seperate log files ,config file and separate db paths.
lets create a log file ,I’m going to create for one but you need to create for each of them.
cd c:\
mkdir logFiles
cd logFiles
mkdir mongologs
cd mongologs
touch mongolog1.log
touch mongolog2.log
touch mongolog2.log
lets create config file for each replica set that holds information related to each set.
mongoConf1.conf
dbpath=C:\data\dblogpath= C:\logFiles\mongologs\mongolog1.loglogappend=trueport=27017replSet=repl1
mongoConf2.conf
dbpath=C:\data1\dblogpath= C:\logFiles\mongologs\mongolog2.loglogappend=trueport=27018replSet=repl1
mongoConf3.conf
dbpath=C:\data2\dblogpath= C:\logFiles\mongologs\mongolog3.loglogappend=trueport=27019replSet=repl1
You need to specify the db path,the log path,port number,log appends and the replica set name equals to number of replica sets you want to create.You need to define a name for the replica set in this case we have given (repl1).You need to define separate ports for each file.
Now simply run the mongod server for each replica set.
mongod -f [path to mongoConf1.conf]
If you have three replica sets open three terminals and fire the above command but you need to change the path to mongoconfig file for each replica set.
Run Mongo shell while specifying the port
mongo --port 27017
You can check the status of replica sets using the rs
command.
rs.status()
You will get error like replica sets are not been initialise.
Now you need to configure replica set from MongoDB shell.
//putting all the config into a variable//_id is the name of the replica sets
//we will specify the same name that we use in the mongoconfig file
//in member array you need to list all the ports for which you have specify the replica set.config = { _id: "repl1", members:[
... { _id : 0, host : "localhost:27017"},
... { _id : 1, host : "localhost:27018"},
... { _id : 2, host : "localhost:27019"}
... ] }//lets initiate the configuration using rsrs.initiate(config)
Now you have configure the replica sets and you can check the status which one is primary and secondaries using the command.
rs.status()
Congratulation you have created replica sets.Your can check the data/db folder for each replica set ,each will contain the same data.If one got delete the other ones got your back,cool right. In the next story we’ll add authorization to replica sets.
Thanks for your time.If any problem simply leaves a comment.:)