Replica sets in MongoDB

Solution:

Use replica sets with PRIMARY and SECONDARY nodes.

How the solution solves the problem:

Create multiple daemons on different ports numbers. Say consider 3 daemons….
daemon on port 27018 , dbpath- /home/db1
daemon on port 27019, dbpath- /home/db2
daemon on port 27020 , dbpath- /home/db3
But all these daemons should contain the same data…

Setting up replica sets in local:

Step 1:
Now lets create 3 daemons of type replica sets with name as rs0 as shown below:
ps: make sure the replica set name(rs0) should be same for all the daemons.
daemon on port 27018 , dbpath- /home/db1 — rs0
daemon on port 27019, dbpath- /home/db2 — rs0
daemon on port 27020 , dbpath- /home/db3 — rs0

mkdir /home/db1 /home/db2 /home/db3
chmod 777 /home/db1 /home/db2 /home/db3
(new terminal)mongod --dbpath /home/db1 --port 27018 --replSet "rs0"
(new terminal)mongod --dbpath /home/db2 --port 27019 --replSet "rs0"
(new terminal)mongod --dbpath /home/db3 --port 27020 --replSet "rs0"
mongo --port 27018
rs.initiate()
rs.add("localhost:27019")
rs.add("localhost:27020")
mongo --port 27019
mongo --port 27020
rs.config()

--

--

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