MongoDB demo on Replica Set

$ pwd
$ mkdir -p data/rs1 data/rs2 data/rs3

Step #01: Start the nodes

$  mongod --replSet rs1 --logpath "1.log" --dbpath ./rs1 --port 27017 --fork$  mongod --replSet rs1 --logpath "2.log" --dbpath ./rs2 --port 27018 --fork$  mongod --replSet rs1 --logpath "3.log" --dbpath ./rs3 --port 27019 --fork

Step #02: Setting the Replica Set

config = { _id: "rs1", members:[
{ _id : 0, host : "localhost:27017", priority:0, slaveDelay:5 },
{ _id : 1, host : "localhost:27018"},
{ _id : 2, host : "localhost:27019"} ]
script init_replica_pablo.js
mongo --port 27018 < init_replica_pablo.js

Step #03: Insert Data in Primary

$ mongo --port 27018
Insert data in primary

Step #04: What is happening on Secondary

verify on secondary with: rs.slaveOk()

Step #05: Shut down Primary!

mongo:27018 was the PRIMARY

Step #05.i ~ Verify status with

rs.stats() — we see the PRIMARY{ ns : “test.people”}).pretty()
rs1:PRIMARY>{ ns : “test.people”}).pretty()
rs1:PRIMARY>{ ns : “test.people”}).pretty()
rs1:SECONDARY>{ ns : “test.people”}).pretty()

Step #05.ii ~ Shut Down Primary

mongo:27018 was the PRIMARY
Before KILL PRIMARY in Replica Set
After KILL PRIMARY in Replica Set — 27019 is the new king!




Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store