Thursday, August 5, 2021

.:: How To Replicate Set (Redundant /Mirroring) MongoDB 5.0 on Centos 7 :::.


A. Configuring the Replica Set
1. Setting up the environment Add /etc/host to nodeA nodeB 

[root@teguhth01 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.10.10.21 teguhth01 NodeA
10.10.10.22 teguhth02 NodeB
[root@teguhth01 ~]# 

[root@teguhth02 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.10.10.21 teguhth01 NodeA
10.10.10.22 teguhth02 NodeB
[root@teguhth02 ~]# 

2. Starting the MongoDB Instance all node
[root@teguhth01 tmp]# cat /etc/mongod.conf 
---------------------------------
# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1,NodeA 
 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.

replication:
  replSetName: ReplicaMongoDB
---------------------------------
  
[root@teguhth01 tmp]#  

---------------------------------
---------------------------------
  
[root@teguhth02 /]# cat /etc/mongod.conf 
---------------------------------
# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1,NodeB
# Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.

replication:
 replSetName: ReplicaMongoDB
---------------------------------
[root@teguhth02 /]# 


3. Restart mongod all node
[root@teguhth01 tmp]# systemctl restart mongod
[root@teguhth01 tmp]# 

[root@teguhth02 /]# systemctl restart mongod


4. Initializing the Replica Set (enough set NodeA)
4.1 NodeA 

rs.initiate( {
_id : "ReplicaMongoDB",
members: [
{ _id: 0, host: "NodeA:27017" },
{ _id: 1, host: "NodeB:27017" }
]
})

or 

rs.initiate( {_id : "ReplicaMongoDB", members: [{ _id: 0, host: "NodeA:27017" },{ _id: 1, host: "NodeB:27017" }]});

log 

> rs.initiate( {_id : "ReplicaMongoDB", members: [{ _id: 0, host: "NodeA:27017" },{ _id: 1, host: "NodeB:27017" }]});
{ "ok" : 1 }
ReplicaMongoDB:SECONDARY> 

4.2 NodeB

> db.getMongo().setSecondaryOk();


5. Check config to All Node 
5.1 NodeA 

ReplicaMongoDB:PRIMARY> rs.conf();
{
"_id" : "ReplicaMongoDB",
"version" : 1,
"term" : 1,
"members" : [
{
"_id" : 0,
"host" : "NodeA:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"secondaryDelaySecs" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "NodeB:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"secondaryDelaySecs" : NumberLong(0),
"votes" : 1
}
],
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"catchUpTimeoutMillis" : -1,
"catchUpTakeoverDelayMillis" : 30000,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("610b7d869cad55f332e730ce")
}
}
ReplicaMongoDB:PRIMARY> 

5.2 NodeB 

ReplicaMongoDB:SECONDARY> rs.conf();
{
"_id" : "ReplicaMongoDB",
"version" : 1,
"term" : 1,
"members" : [
{
"_id" : 0,
"host" : "NodeA:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"secondaryDelaySecs" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "NodeB:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"secondaryDelaySecs" : NumberLong(0),
"votes" : 1
}
],
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"catchUpTimeoutMillis" : -1,
"catchUpTakeoverDelayMillis" : 30000,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("610b7d869cad55f332e730ce")
}
}
ReplicaMongoDB:SECONDARY> 

6. Test Replication from NodeA

6.1 insert NodeA

use mobilelegend;
db.createCollection("MarkmanCollection");
db.MarkmanCollection.insertMany([
{name: "Miya", description: "01 Miya Panah", record: 1},
{name: "Hanabi", description: "02 Hanabi Bumerang", record: 2}]);

log 

ReplicaMongoDB:PRIMARY> use mobilelegend;
switched to db mobilelegend
ReplicaMongoDB:PRIMARY> db.createCollection("MarkmanCollection");
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1628146206, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1628146206, 1)
}
ReplicaMongoDB:PRIMARY> db.MarkmanCollection.insertMany([
... {name: "Miya", description: "01 Miya Panah", record: 1},
... {name: "Hanabi", description: "02 Hanabi Bumerang", record: 2}]);
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("610b8a2262934282918f20f6"),
ObjectId("610b8a2262934282918f20f7")
]
}
ReplicaMongoDB:PRIMARY> 

6.2 Check Node A & Node B 
sintax 

show dbs;
use mobilelegend;
show collections;
db.MarkmanCollection.find().sort({record: 1});
db.MarkmanCollection.find().sort({record: 1}).pretty();

Log NodeA 

ReplicaMongoDB:PRIMARY> show dbs;
admin            0.000GB
config           0.000GB
local            0.000GB
mobilelegend     0.000GB
replicatestdata  0.000GB
ReplicaMongoDB:PRIMARY> use mobilelegend;
switched to db mobilelegend
ReplicaMongoDB:PRIMARY> show collections;
MarkmanCollection
ReplicaMongoDB:PRIMARY> 

ReplicaMongoDB:PRIMARY> db.MarkmanCollection.find().sort({record: 1});
{ "_id" : ObjectId("610b8a2262934282918f20f6"), "name" : "Miya", "description" : "01 Miya Panah", "record" : 1 }
{ "_id" : ObjectId("610b8a2262934282918f20f7"), "name" : "Hanabi", "description" : "02 Hanabi Bumerang", "record" : 2 }
ReplicaMongoDB:PRIMARY> db.MarkmanCollection.find().sort({record: 1}).pretty();
{
"_id" : ObjectId("610b8a2262934282918f20f6"),
"name" : "Miya",
"description" : "01 Miya Panah",
"record" : 1
}
{
"_id" : ObjectId("610b8a2262934282918f20f7"),
"name" : "Hanabi",
"description" : "02 Hanabi Bumerang",
"record" : 2
}
ReplicaMongoDB:PRIMARY> 

log NodeB 

ReplicaMongoDB:SECONDARY> show dbs;
admin            0.000GB
config           0.000GB
local            0.000GB
mobilelegend     0.000GB
replicatestdata  0.000GB
ReplicaMongoDB:SECONDARY> use mobilelegend;
switched to db mobilelegend
ReplicaMongoDB:SECONDARY> show collections;
MarkmanCollection
ReplicaMongoDB:SECONDARY> 
ReplicaMongoDB:SECONDARY> db.MarkmanCollection.find().sort({record: 1});
{ "_id" : ObjectId("610b8a2262934282918f20f6"), "name" : "Miya", "description" : "01 Miya Panah", "record" : 1 }
{ "_id" : ObjectId("610b8a2262934282918f20f7"), "name" : "Hanabi", "description" : "02 Hanabi Bumerang", "record" : 2 }
ReplicaMongoDB:SECONDARY> db.MarkmanCollection.find().sort({record: 1}).pretty();
{
"_id" : ObjectId("610b8a2262934282918f20f6"),
"name" : "Miya",
"description" : "01 Miya Panah",
"record" : 1
}
{
"_id" : ObjectId("610b8a2262934282918f20f7"),
"name" : "Hanabi",
"description" : "02 Hanabi Bumerang",
"record" : 2
}
ReplicaMongoDB:SECONDARY> 

7. Test Replication from NodeB

Note : Write / insert only can do to Primary server not secondary

No comments:

Post a Comment

Popular Posts