For a statement based replication oplog file is used to apply all the transaction on the primary node to the secondary node.The default value for oplog size(MB) is 5% of free space on disk.
Follow the below steps to setup a replication environment:
1. Create instances for primary and secondary members for the replica set in different servers.
On Server 1
mkdir /usr/apps/database/db1
mongod --port 27001 --replSet abc --dbpath /usr/apps/database/db1 --logpath /usr/apps/database/log.1 --logappend --oplogSize 50 --smallfiles --fork
On Server 2
mkdir /usr/apps/database/db2
mongod --port 27002 --replSet abc --dbpath /usr/apps/database/db2 --logpath /usr/apps/database/log.2 --logappend --oplogSize 50 --smallfiles --fork
On Server 3
mkdir /usr/apps/database/db3
mongod --port 27003 --replSet abc --dbpath /usr/apps/database/db3 --logpath /usr/apps/database/log.3 --logappend --oplogSize 50 --smallfiles --fork
OR
On primary server
Follow the below steps to setup a replication environment:
1. Create instances for primary and secondary members for the replica set in different servers.
On Server 1
mkdir /usr/apps/database/db1
mongod --port 27001 --replSet abc --dbpath /usr/apps/database/db1 --logpath /usr/apps/database/log.1 --logappend --oplogSize 50 --smallfiles --fork
On Server 2
mkdir /usr/apps/database/db2
mongod --port 27002 --replSet abc --dbpath /usr/apps/database/db2 --logpath /usr/apps/database/log.2 --logappend --oplogSize 50 --smallfiles --fork
On Server 3
mkdir /usr/apps/database/db3
mongod --port 27003 --replSet abc --dbpath /usr/apps/database/db3 --logpath /usr/apps/database/log.3 --logappend --oplogSize 50 --smallfiles --fork
OR
On primary server
$ mongod --master --oplogSize 500
On Other servers
$ mongod --slave --source localhost:27017 --port 3000
--dbpath /data/slave
2.Specify Config
cfg = { _id : <setName>,
Members: [
{ _id: 0,
host:<name: port>,<options> },
{ _id: 1,
host:<name: port>,<options> },
..
]
}
> cfg = {"_id":"abc","members":[{_id:0,host:"localhost:27001"},{_id:1,host:"localhost:27002"},{_id:2,host:"localhost:27003"}]}
3.Initiate Repl Set
> rs.initiate(cfg)
{
"ok" : 1,
"operationTime" : Timestamp(1553514943, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1553514943, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
4. Check Replication status
abc:PRIMARY> rs.status()
{
"set" : "abc",
"date" : ISODate("2019-03-25T11:59:04.908Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1553515136, 1),
"t" : NumberLong(1)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1553515136, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1553515136, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1553515136, 1),
"t" : NumberLong(1)
}
},
"lastStableCheckpointTimestamp" : Timestamp(1553515136, 1),
"members" : [
{
"_id" : 0,
"name" : "localhost:27001",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 4151,
"optime" : {
"ts" : Timestamp(1553515136, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2019-03-25T11:58:56Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1553514955, 1),
"electionDate" : ISODate("2019-03-25T11:55:55Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "localhost:27002",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 201,
"optime" : {
"ts" : Timestamp(1553515136, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1553515136, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2019-03-25T11:58:56Z"),
"optimeDurableDate" : ISODate("2019-03-25T11:58:56Z"),
"lastHeartbeat" : ISODate("2019-03-25T11:59:03.464Z"),
"lastHeartbeatRecv" : ISODate("2019-03-25T11:59:04.405Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "localhost:27001",
"syncSourceHost" : "localhost:27001",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "localhost:27003",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 201,
"optime" : {
"ts" : Timestamp(1553515136, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1553515136, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2019-03-25T11:58:56Z"),
"optimeDurableDate" : ISODate("2019-03-25T11:58:56Z"),
"lastHeartbeat" : ISODate("2019-03-25T11:59:03.464Z"),
"lastHeartbeatRecv" : ISODate("2019-03-25T11:59:04.482Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "localhost:27001",
"syncSourceHost" : "localhost:27001",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1553515136, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1553515136, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
Some important properties to check from rs.status are:
OptimeDate – Time of last operation.
Heartbt- pinging & response from member.
Self : true – member talking to right now
State : 1 – up , 8 down
We can see all the replica set commands by using
rs.help()
For example:
> db.printReplicationInfo() # tells you how long your oplog will last
> db.printSlaveReplicationInfo() # tells you how far behind the slave is
5. To find current primary in the replica set
abc:PRIMARY> rs.status().members.find(r=>r.state===1).name
localhost:27001
or
abc:PRIMARY>m = db.isMaster().ismaster
or
abc:PRIMARY>m = db.isMaster().ismaster
6.To allow read from slave member of the replica set
abc:SECONDARY> rs.slaveOk()
If primary fails ,secondary becomes primary by automatic failover and after primary comes up later, it again becomes primary.
7. No of Roll Backs
7. No of Roll Backs
db.serverStatus()['repl'] – rbid – no of rollbacks
8. Add a Member
rs.add("abc:27014")
9. Add an Arbiter
rs.addArb("abc:28000")
10. Remove a Member
rs.remove("abc:28000")
8. Add a Member
rs.add("abc:27014")
9. Add an Arbiter
rs.addArb("abc:28000")
10. Remove a Member
rs.remove("abc:28000")
Comments
Post a Comment