Replication
Checkout the config file for mongod in path /etc/mongod.conf
in linux find your own config file according ti your os.
find for rule storage:
and check dbPath
field it will have value /var/lib/mongo
if in linux or other according to your os.
Go in that folder create three new folders for a new replica set named store1
, store2
and store3
.
step OPEN:
Open new terminal.
step START-SERVER
run command mongod --port {2701{*>7}} --dbPath {PathToAnyoneFileFrom3WeCreated} --replSet {ReplicaSetId}
in that new terminal.
Repeat OPEN and START-SERVER three times with increasing port by one and using a new folder path of which we created.
In terminal connect to mongo service and run command
rs.initiate({
_id: "<ReplicaSetId>",
members: [
{ _id: "<uniqueIdInReplicaSet>", host: "hostWithPortToNode" },
{ _id: "<uniqueIdInReplicaSet>", host: "hostWithPortToNode" },
{ _id: "<uniqueIdInReplicaSet>", host: "hostWithPortToNode" },
]
})
To check replica state and its details about service use command rs.status()
.
You can see which node is primary and which is secondary.
By default Secondary nodes do not have permission to either read or write permissions.
To allow secondary read connect to secondary after checking which node are secondary and run command db.getMongo().setSecondaryOk()
and db.getMongo().setSlaveOk()
.