1) Install MongoDB on local machine
https://docs.mongodb.com/manual/installation/
2) Create data directories for each replicate set
root@prakash:~/mongo# mkdir primary
root@prakash:~/mongo# mkdir secondary
root@prakash:~/mongo# mkdir arbiter
root@prakash:~/mongo# ls -latr total 20 drwxr-xr-x 49 prakashs prakashs 4096 Jun 26 17:05 .. drwxr-xr-x 2 root root 4096 Jun 27 22:22 arbiter drwxrwxr-x 5 prakashs prakashs 4096 Jun 27 22:22 . drwxr-xr-x 4 root root 4096 Jun 27 22:24 primary drwxr-xr-x 4 root root 4096 Jun 27 22:27 secondary
3) Start MongoDB Primary instance with port, data path and replica set name
root@prakash:~/mongo# mongod --port 28000 --dbpath /home/prakashs/mongo/primary --replSet rs01 &
[1] 29566
3) Simiarly start secondary MongoDB instance with related inputs
root@prakash:~/mongo# mongod --port 28001 --dbpath /home/prakashs/mongo/secondary --replSet rs01 &
[1] 29648
4) Add an arbiter instance to be a part of the replica set but do not hold data. This instance will take part in replica set voting only so arbiter instance can be run on any available system.
For testing purpose I am adding all three instance on the same machine
root@prakash:~/mongo# mongod --port 28002 --dbpath /home/prakashs/mongo/arbiter --replSet rs01 &
[1] 29795
5) Check if three MongoDB instance running
root@prakash:~/mongo# ps aux | grep replSet
29566 0.7 0.8 394268 65924 pts/2 Sl 22:23 0:08 mongod --port 28000 --dbpath /home/prakashs/mongo/primary --replSet rs01
29648 0.8 0.8 393244 67272 pts/9 Sl 22:26 0:08 mongod --port 28001 --dbpath /home/prakashs/mongo/secondary --replSet rs01
29795 1.3 0.8 393240 64920 pts/25 Sl 22:37 0:03 mongod --port 28002 --dbpath /home/prakashs/mongo/arbiter --replSet rs01
6) Login into Primary mongodb to initiate replica set and adding replica members (secondary & arbiter)
root@prakash:~/mongo# mongo --port 28000
MongoDB shell version: 3.0.12
connecting to: 127.0.0.1:28000/test
Server has startup warnings:
> rs.initiate() { "info2" : "no configuration explicitly specified -- making one", "me" : "prakash:28000", "ok" : 1 }
rs01:SECONDARY>
rs01:PRIMARY> rs.add("prakash:28001") { "ok" : 1 }
rs01:PRIMARY> rs.addArb("prakash:28002") { "ok" : 1 }
rs01:PRIMARY>
6) Insert test values on PRIMARY and validate the same on secondary
rs01:PRIMARY> use test
switched to db test
rs01:PRIMARY> db.foo.insert( {id:1, val:"test"})
WriteResult({ "nInserted" : 1 })
rs01:PRIMARY> db.foo.insert( {id:2, val:"test-test"})
WriteResult({ "nInserted" : 1 })
rs01:PRIMARY>
rs01:PRIMARY> db.foo.find()
{ "_id" : ObjectId("57719f40512dbbf606c3bb3c"), "id" : 1, "val" : "test" }
{ "_id" : ObjectId("57719f48512dbbf606c3bb3d"), "id" : 2, "val" : "test-test" }
rs01:PRIMARY> exit
bye
root@prakash:~/mongo# mongo --port 28001 MongoDB shell version: 3.0.12 connecting to: 127.0.0.1:28001/test rs01:SECONDARY> rs.slaveOk() rs01:SECONDARY> show collections foo system.indexes rs01:SECONDARY> db.foo.find() { "_id" : ObjectId("57719f40512dbbf606c3bb3c"), "id" : 1, "val" : "test" } { "_id" : ObjectId("57719f48512dbbf606c3bb3d"), "id" : 2, "val" : "test-test" } rs01:SECONDARY>
7) Try to read from ARBITER
root@prakash:~/mongo# mongo --port 28002
MongoDB shell version: 3.0.12
connecting to: 127.0.0.1:28002/test
Server has startup warnings:
rs01:ARBITER> rs.slaveOk()
rs01:ARBITER> show collections
rs01:ARBITER> db.foo.find()
Error: error: {
"$err" : "not master or secondary; cannot currently read from this replSet member",
"code" : 13436
}
rs01:ARBITER>
7) Let’s kill primary instance so that secondary will become Primary
root@prakash:~/mongo# ps aux | grep replSet
29566 0.6 1.0 26026232 83256 pts/2 Sl 22:23 0:13 mongod --port 28000 --dbpath /home/prakashs/mongo/primary --replSet rs01
29648 0.7 1.0 26021100 85920 pts/9 Sl 22:26 0:12 mongod --port 28001 --dbpath /home/prakashs/mongo/secondary --replSet rs01
29795 0.7 0.8 448620 66108 pts/25 Sl 22:37 0:08 mongod --port 28002 --dbpath /home/prakashs/mongo/arbiter --replSet rs01
30486 0.0 0.0 15948 2268 pts/26 S+ 22:55 0:00 grep --color=auto replSet
root@prakash:~/mongo# kill -9 29566 root@prakash:~/mongo# ps aux | grep replSet 29648 0.7 1.0 26021100 85920 pts/9 Sl 22:26 0:12 mongod --port 28001 --dbpath /home/prakashs/mongo/secondary --replSet rs01 29795 0.7 0.8 448620 66304 pts/25 Sl 22:37 0:08 mongod --port 28002 --dbpath /home/prakashs/mongo/arbiter --replSet rs01 30505 0.0 0.0 15948 2264 pts/26 S+ 22:55 0:00 grep --color=auto replSet
Try to connect Prirmary failed
root@prakash:~/mongo# mongo --port 28000
MongoDB shell version: 3.0.12
connecting to: 127.0.0.1:28000/test
2016-06-27T22:56:04.661+0100 W NETWORK Failed to connect to 127.0.0.1:28000, reason: errno:111 Connection refused
2016-06-27T22:56:04.663+0100 E QUERY Error: couldn't connect to server 127.0.0.1:28000 (127.0.0.1), connection attempt failed
at connect (src/mongo/shell/mongo.js:181:14)
at (connect):1:6 at src/mongo/shell/mongo.js:181
exception: connect failed
Connect secondary instance which is Primary now,
root@prakash:~/mongo# mongo --port 28001
MongoDB shell version: 3.0.12
connecting to: 127.0.0.1:28001/test
rs01:PRIMARY> db.foo.insert( {id:3, val:"test3"}) WriteResult({ "nInserted" : 1 }) rs01:PRIMARY> db.foo.find() { "_id" : ObjectId("57719f40512dbbf606c3bb3c"), "id" : 1, "val" : "test" } { "_id" : ObjectId("57719f48512dbbf606c3bb3d"), "id" : 2, "val" : "test-test" } { "_id" : ObjectId("5771a19601e9d457d209aa88"), "id" : 3, "val" : "test3" }
8) Let’s bring back original primary
root@prakash:~/mongo# mongod --port 28000 --dbpath /home/prakashs/mongo/primary --replSet rs01 &
[1] 31285
root@prakash:~/mongo# mongo --port 28000 MongoDB shell version: 3.0.12 connecting to: 127.0.0.1:28000/test rs01:SECONDARY> rs.slaveOk() rs01:SECONDARY> use test switched to db test rs01:SECONDARY> db.foo.find() { "_id" : ObjectId("57719f40512dbbf606c3bb3c"), "id" : 1, "val" : "test" } { "_id" : ObjectId("57719f48512dbbf606c3bb3d"), "id" : 2, "val" : "test-test" } { "_id" : ObjectId("5771a19601e9d457d209aa88"), "id" : 3, "val" : "test3" } rs01:SECONDARY>