MongoDB demo on Replica Set

$ pwd
/tmp/mongorsets/data
$ 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"} ]
};
rs.initiate(config);
rs.status();
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 oplog.rs

rs.stats() — we see the PRIMARY
db.oplog.rs.find({ ns : “test.people”}).pretty()
rs1:PRIMARY> db.oplog.rs.find({ ns : “test.people”}).pretty()
rs1:PRIMARY> db.oplog.rs.find({ ns : “test.people”}).pretty()
rs1:SECONDARY> db.oplog.rs.find({ 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!

Resources

--

--

--

#cloud . #mobile ~} Sharing IT while learning It! ... Opinions are for my own

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to use Jest with Typescript

Choosing the best state management tool for your application.

How to use Ng-select with highlight in Angular 6

Handling Access Tokens for Google APIs with React & Node.js Tutorial

You Don’t Know JS: My learnings from Scope & Closures

Function/Class Components — is there a difference?

Phase 2: Problem Solver, React App Project

Routing in React using React-Router

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
Pablo Ezequiel Inchausti

Pablo Ezequiel Inchausti

#cloud . #mobile ~} Sharing IT while learning It! ... Opinions are for my own

More from Medium

Mongoose, an ORM (Object-relational mapping), is a fantastic NPM(Node Package Manager) library.

Using express-session middleware and MongoDB session store in Nodejs for production apps

Playing with Nodejs :( First Step to Node ✔💻

Introduction to NodeJS