栏目头部广告

MongoDB切换主节点技巧

1、需求与现有副本集情况介绍

背景:业务侧针对MongoDB副本集的一个Secondary节点B进行了改配操作,由于改配后的B节点,配置高,性能好,业务想以最短的时间,最小的代价把B节点提升为Primary主节点。

现有MongoDB 5节点副本集集群采用UCloud云数据库,节点信息如下:即节点A:192.168.0.182 Primary节点;B:192.168.0.240 Secondary节点;C:192.168.0.217 Secondary节点;D:192.168.0.60;E:192.168.0.145 arbiter节点。

2-22100113511Y94.png

2、配置调整

(1)查看当前副本集角色

udb-a0r3omr444m:PRIMARY> rs.status()
{
 "set" : "udb-a0r3omr444m",
 "date" : ISODate("2022-10-01T05:53:11.213Z"),
 "myState" : 1,
 "term" : NumberLong(1),
 "syncingTo" : "",
 "syncSourceHost" : "",
 "syncSourceId" : -1,
 "heartbeatIntervalMillis" : NumberLong(2000),
 "optimes" : {
   "lastCommittedOpTime" : {
   "ts" : Timestamp(1664603583, 1),
   "t" : NumberLong(1)
   },
   "readConcernMajorityOpTime" : {
     "ts" : Timestamp(1664603583, 1),
     "t" : NumberLong(1)
   },
   "appliedOpTime" : {
     "ts" : Timestamp(1664603583, 1),
     "t" : NumberLong(1)
   },
   "durableOpTime" : {
     "ts" : Timestamp(1664603583, 1),
     "t" : NumberLong(1)
   }
 },
 "lastStableCheckpointTimestamp" : Timestamp(1664603543, 1),
 "members" : [
 {
   "_id" : 0,
   "name" : "192.168.0.182:27017",
   "health" : 1,
   "state" : 1,
   "stateStr" : "PRIMARY",
   "uptime" : 786,
   "optime" : {
     "ts" : Timestamp(1664603583, 1),
     "t" : NumberLong(1)
   },
   "optimeDate" : ISODate("2022-10-01T05:53:03Z"),
   "syncingTo" : "",
   "syncSourceHost" : "",
   "syncSourceId" : -1,
   "infoMessage" : "",
   "electionTime" : Timestamp(1664602821, 2),
   "electionDate" : ISODate("2022-10-01T05:40:21Z"),
   "configVersion" : 5,
   "self" : true,
   "lastHeartbeatMessage" : ""
 },
 {
   "_id" : 1,
   "name" : "192.168.0.240:27017",
   "health" : 1,
   "state" : 2,
   "stateStr" : "SECONDARY",
   "uptime" : 729,
   "optime" : {
       "ts" : Timestamp(1664603583, 1),
       "t" : NumberLong(1)
   },
   "optimeDurable" : {
       "ts" : Timestamp(1664603583, 1),
       "t" : NumberLong(1)
   },
   "optimeDate" : ISODate("2022-10-01T05:53:03Z"),
   "optimeDurableDate" : ISODate("2022-10-01T05:53:03Z"),
   "lastHeartbeat" : ISODate("2022-10-01T05:53:09.400Z"),
   "lastHeartbeatRecv" : ISODate("2022-10-01T05:53:09.907Z"),
   "pingMs" : NumberLong(0),
   "lastHeartbeatMessage" : "",
   "syncingTo" : "192.168.0.182:27017",
   "syncSourceHost" : "192.168.0.182:27017",
   "syncSourceId" : 0,
   "infoMessage" : "",
   "configVersion" : 5
 },
 {
   "_id" : 2,
   "name" : "192.168.0.145:27017",
   "health" : 1,
   "state" : 7,
   "stateStr" : "ARBITER",
   "uptime" : 724,
   "lastHeartbeat" : ISODate("2022-10-01T05:53:09.384Z"),
   "lastHeartbeatRecv" : ISODate("2022-10-01T05:53:09.410Z"),
   "pingMs" : NumberLong(0),
   "lastHeartbeatMessage" : "",
   "syncingTo" : "",
   "syncSourceHost" : "",
   "syncSourceId" : -1,
   "infoMessage" : "",
   "configVersion" : 5
 },
 {
   "_id" : 3,
   "name" : "192.168.0.217:27017",
   "health" : 1,
   "state" : 2,
   "stateStr" : "SECONDARY",
   "uptime" : 686,
   "optime" : {
   "ts" : Timestamp(1664603583, 1),
   "t" : NumberLong(1)
 },
 "optimeDurable" : {
   "ts" : Timestamp(1664603583, 1),
   "t" : NumberLong(1)
 },
 "optimeDate" : ISODate("2022-10-01T05:53:03Z"),
 "optimeDurableDate" : ISODate("2022-10-01T05:53:03Z"),
 "lastHeartbeat" : ISODate("2022-10-01T05:53:09.379Z"),
 "lastHeartbeatRecv" : ISODate("2022-10-01T05:53:09.910Z"),
 "pingMs" : NumberLong(0),
 "lastHeartbeatMessage" : "",
 "syncingTo" : "192.168.0.182:27017",
 "syncSourceHost" : "192.168.0.182:27017",
 "syncSourceId" : 0,
 "infoMessage" : "",
 "configVersion" : 5
 },
 {
   "_id" : 4,
   "name" : "192.168.0.60:27017",
   "health" : 1,
   "state" : 2,
   "stateStr" : "SECONDARY",
   "uptime" : 685,
   "optime" : {
     "ts" : Timestamp(1664603583, 1),
     "t" : NumberLong(1)
   },
   "optimeDurable" : {
     "ts" : Timestamp(1664603583, 1),
     "t" : NumberLong(1)
   },
   "optimeDate" : ISODate("2022-10-01T05:53:03Z"),
   "optimeDurableDate" : ISODate("2022-10-01T05:53:03Z"),
   "lastHeartbeat" : ISODate("2022-10-01T05:53:09.408Z"),
   "lastHeartbeatRecv" : ISODate("2022-10-01T05:53:09.956Z"),
   "pingMs" : NumberLong(0),
   "lastHeartbeatMessage" : "",
   "syncingTo" : "192.168.0.240:27017",
   "syncSourceHost" : "192.168.0.240:27017",
   "syncSourceId" : 1,
   "infoMessage" : "",
   "configVersion" : 5
 }
   ],
 "ok" : 1,
 "operationTime" : Timestamp(1664603583, 1),
 "$clusterTime" : {
   "clusterTime" : Timestamp(1664603583, 1),
   "signature" : {
     "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
     "keyId" : NumberLong(0)
   }
 }
}

(2)C节点冻结120s

[root@blogs-v2 ~]# mongo 192.168.0.217:27017/admin -u root -p
MongoDB shell version v4.0.27
Enter password:
connecting to: mongodb://192.168.0.217:27017/admin?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("b0714374-1a2a-41f5-9592-9a5ee8f18c2d") }
MongoDB server version: 4.0.3
udb-a0r3omr444m:SECONDARY>
udb-a0r3omr444m:SECONDARY> rs.freeze(120)         # C节点冻结120s
{
 "ok" : 1,
 "operationTime" : Timestamp(1664603883, 1),
 "$clusterTime" : {
   "clusterTime" : Timestamp(1664603883, 1),
   "signature" : {
     "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
     "keyId" : NumberLong(0)
   }
 }
}

(3)D节点冻结120s

[root@blogs-v2 ~]# mongo 192.168.0.60:27017/admin -u root -p
MongoDB shell version v4.0.27
Enter password:
connecting to: mongodb://192.168.0.60:27017/admin?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("184f5eff-bfa9-4db9-87ab-54911a6897ff") }
MongoDB server version: 4.0.3
udb-a0r3omr444m:SECONDARY>
udb-a0r3omr444m:SECONDARY> rs.freeze(120)         # D节点冻结120s
{
 "ok" : 1,
 "operationTime" : Timestamp(1664603883, 1),
 "$clusterTime" : {
   "clusterTime" : Timestamp(1664603883, 1),
   "signature" : {
     "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
     "keyId" : NumberLong(0)
   }
 }
}

(4)A节点降级120s

[root@blogs-v2 ~]# mongo 192.168.0.182:27017/admin -u root -p
MongoDB shell version v4.0.27
Enter password:
connecting to: mongodb://192.168.0.182:27017/admin?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("cf937160-092d-4073-8788-562e7b928d50") }
MongoDB server version: 4.0.3
udb-a0r3omr444m:PRIMARY>
udb-a0r3omr444m:PRIMARY> rs.stepDown(120)        # 降级当前主节点,120s内不参与选举
2022-10-01T13:57:13.566+0800 I NETWORK  [js] DBClientConnection failed to receive message from 192.168.0.182:27017 - HostUnreachable: Connection closed by peer
2022-10-01T13:57:13.566+0800 E QUERY    [js] Error: error doing query: failed: network error while attempting to run command 'replSetStepDown' on host '192.168.0.182:27017'  :
DB.prototype.runCommand@src/mongo/shell/db.js:170:1
DB.prototype.adminCommand@src/mongo/shell/db.js:187:1
rs.stepDown@src/mongo/shell/utils.js:1505:12
@(shell):1:1
2022-10-01T13:57:13.568+0800 I NETWORK  [js] trying reconnect to 192.168.0.182:27017 failed
2022-10-01T13:57:13.572+0800 I NETWORK  [js] reconnect 192.168.0.182:27017 ok

(5)查看B节点选举情况

[root@blogs-v2 ~]# mongo 192.168.0.240:27017/admin -u root -p
MongoDB shell version v4.0.27
Enter password:
connecting to: mongodb://192.168.0.240:27017/admin?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("3416021c-089e-4927-9c60-775a9a692b22") }
MongoDB server version: 4.0.3
udb-a0r3omr444m:PRIMARY>

注:可以看出此时B节点已经是Primary节点了,选举非常迅速!!!

(6)查看控制台集群属性

2-22100114055La.png

注:至此,MongoDB副本集节点切换完毕。

文章页广告

随便看看

栏目底部广告
`