Replica Set
A replica set is a group of mongod 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.
Creating the mongod instances:
First we need to create a mongod isntance using below command. before
creating the instance make a directory with any name ex: db1 and give
this path in the below command Use any port you want
- mongod --port 20017 --dbpath C:\Users\michel\db1\ --replSet rs0
Create a another mongod isntance using the different terminal by using
the same command but give different path and port number is diffferent
- mongod --port 20018 --dbpath C:\Users\michel\db2\ --replSet rs0
Creating the mongo shell terminals:
Now use the new terminal to open mongo terminal using below command
- mongo --port 20017
then initiate the replica set using below command
- rs.initiate()
you will see info like
info2" : "no configuration specified. Using a default configuration for
the set", after seeing the rs0:SECONDARY> then press enter then you
will notice SECONDARY will replaces with PRIMARY. You can check weather
the replica set node is created or not using rs.status().
Adding the node2 to primary node:
In the open terminal of mogo --port 20017 enter the below command to add the port 20018.
- rs.add("localhost:20018");
then create collection and insert some dictionary
- use testdb
- db.testdb.posts.insert({"postname":"mongo replica set"})
- db.testdb.posts.find({})
Checking weather same collection created in replica set or not 20018 port:
Open a new terminal and enter mongo --port 20018 command
then enter rs.slaveOk()
- mongo --port 200018
- rs.slaveOk()
- db.testdb.posts.find({})
Comments
Post a Comment