mongodb adds an example of an arbiter node method
- 2020-06-15 10:27:56
- OfStack
preface
When replica set of mongodb was created, it was made 1 master and 2 slave, not 1 master and 1 slave. Here we delete one point from replica set and add it to replica set as a mediation node:
1. Initial state:
shard1ReplSet:PRIMARY> rs.status();rs.status();
{
"set" : "shard1ReplSet",
"date" : ISODate("2017-02-21T07:48:03.058Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"appliedOpTime" : {
"ts" : Timestamp(1487663274, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1487587982, 1),
"t" : NumberLong(-1)
}
},
"members" : [
{
"_id" : 0,
"name" : "10.13.0.130:22001",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 76672,
"optime" : {
"ts" : Timestamp(1487663274, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2017-02-21T07:47:54Z"),
"electionTime" : Timestamp(1487587993, 1),
"electionDate" : ISODate("2017-02-20T10:53:13Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 1,
"name" : "10.13.0.131:22001",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 75300,
"optime" : {
"ts" : Timestamp(1487663274, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1487587982, 1),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("2017-02-21T07:47:54Z"),
"optimeDurableDate" : ISODate("2017-02-20T10:53:02Z"),
"lastHeartbeat" : ISODate("2017-02-21T07:48:02.150Z"),
"lastHeartbeatRecv" : ISODate("2017-02-21T07:48:02.215Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "10.13.0.132:22001",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "10.13.0.132:22001",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 75300,
"optime" : {
"ts" : Timestamp(1487663274, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1487587982, 1),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("2017-02-21T07:47:54Z"),
"optimeDurableDate" : ISODate("2017-02-20T10:53:02Z"),
"lastHeartbeat" : ISODate("2017-02-21T07:48:02.889Z"),
"lastHeartbeatRecv" : ISODate("2017-02-21T07:48:01.503Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "10.13.0.130:22001",
"configVersion" : 1
}
],
"ok" : 1
}
shard1ReplSet:PRIMARY>
shard1ReplSet:PRIMARY>
shard1ReplSet:PRIMARY>
shard1ReplSet:PRIMARY>
2. Delete nodes:
shard1ReplSet:PRIMARY> rs.remove("10.13.0.132:22001"); rs.remove("10.13.0.132:22001");
{ "ok" : 1 }
shard1ReplSet:PRIMARY> rs.status();rs.status();
{
"set" : "shard1ReplSet",
"date" : ISODate("2017-02-21T07:50:52.934Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"appliedOpTime" : {
"ts" : Timestamp(1487663447, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1487587982, 1),
"t" : NumberLong(-1)
}
},
"members" : [
{
"_id" : 0,
"name" : "10.13.0.130:22001",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 76841,
"optime" : {
"ts" : Timestamp(1487663447, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2017-02-21T07:50:47Z"),
"electionTime" : Timestamp(1487587993, 1),
"electionDate" : ISODate("2017-02-20T10:53:13Z"),
"configVersion" : 2,
"self" : true
},
{
"_id" : 1,
"name" : "10.13.0.131:22001",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 75470,
"optime" : {
"ts" : Timestamp(1487663447, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1487587982, 1),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("2017-02-21T07:50:47Z"),
"optimeDurableDate" : ISODate("2017-02-20T10:53:02Z"),
"lastHeartbeat" : ISODate("2017-02-21T07:50:51.182Z"),
"lastHeartbeatRecv" : ISODate("2017-02-21T07:50:52.212Z"),
"pingMs" : NumberLong(0),
"configVersion" : 2
}
],
"ok" : 1
}
shard1ReplSet:PRIMARY>
shard1ReplSet:PRIMARY>
shard1ReplSet:PRIMARY>
3. Add to arbiter node:
shard1ReplSet:PRIMARY> rs.addArb("10.13.0.132:22001");rs.addArb("10.13.0.132:22001");
{ "ok" : 1 }
shard1ReplSet:PRIMARY>
shard1ReplSet:PRIMARY> rs.status();rs.status();
{
"set" : "shard1ReplSet",
"date" : ISODate("2017-02-21T07:54:05.161Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"appliedOpTime" : {
"ts" : Timestamp(1487663637, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1487587982, 1),
"t" : NumberLong(-1)
}
},
"members" : [
{
"_id" : 0,
"name" : "10.13.0.130:22001",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 77034,
"optime" : {
"ts" : Timestamp(1487663637, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2017-02-21T07:53:57Z"),
"electionTime" : Timestamp(1487587993, 1),
"electionDate" : ISODate("2017-02-20T10:53:13Z"),
"configVersion" : 3,
"self" : true
},
{
"_id" : 1,
"name" : "10.13.0.131:22001",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 75662,
"optime" : {
"ts" : Timestamp(1487663637, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1487587982, 1),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("2017-02-21T07:53:57Z"),
"optimeDurableDate" : ISODate("2017-02-20T10:53:02Z"),
"lastHeartbeat" : ISODate("2017-02-21T07:54:03.210Z"),
"lastHeartbeatRecv" : ISODate("2017-02-21T07:54:02.211Z"),
"pingMs" : NumberLong(0),
"configVersion" : 3
},
{
"_id" : 2,
"name" : "10.13.0.132:22001",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 5,
"lastHeartbeat" : ISODate("2017-02-21T07:54:03.214Z"),
"lastHeartbeatRecv" : ISODate("2017-02-21T07:54:02.274Z"),
"pingMs" : NumberLong(0),
"configVersion" : 3
}
],
"ok" : 1
}
shard1ReplSet:PRIMARY>
Note 1 After mongodb 3.4, arbiter is not supported, although config server is required to be replica set.
When added, an error will be reported:
cfgReplSet:PRIMARY> rs.addArb("10.13.0.132:21000");rs.addArb("10.13.0.132:21000");
{
"ok" : 0,
"errmsg" : "Arbiters are not allowed in replica set configurations being used for config servers",
"code" : 103,
"codeName" : "NewReplicaSetConfigurationIncompatible"
}
cfgReplSet:PRIMARY>
Note 2: rs. reconfig() can also be operated with a similar effect to re. remove+ rs. addArb
conclusion
The above is all about mongodb add arbiter node, I hope the content of this article can bring you a certain help in your study or work, if you have any questions, you can leave a message to communicate.