代码改变世界

【MongoDB】移除复制集成员

  abce  阅读(250)  评论(0编辑  收藏  举报

1.使用rs.remove()
(1)关闭mongodb实例
先关闭要移除的mongodb实例

1
db.shutdownServer()

(2)连接到复制集的primary节点
使用db.hello()查看复制集的信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
> db.hello()
{
        "topologyVersion" : {
                "processId" : ObjectId("63956ad40ce6275c36c30464"),
                "counter" : NumberLong(8)
        },
        "hosts" : [
                "192.168.137.24:27017",
                "192.168.137.25:27017",
                "192.168.137.26:27017"
        ],
        "passives" : [
                "13.14.250.11:27017"
        ],
        "setName" : "abce_rs",
        "setVersion" : 2,
        "isWritablePrimary" : true,
        "secondary" : false,
        "primary" : "192.168.137.26:27017",
        "me" : "192.168.137.26:27017",
        "electionId" : ObjectId("7fffffff0000000000000005"),
        "lastWrite" : {
                "opTime" : {
                        "ts" : Timestamp(1678089543, 8),
                        "t" : NumberLong(5)
                },
                "lastWriteDate" : ISODate("2023-03-06T07:59:03Z"),
                "majorityOpTime" : {
                        "ts" : Timestamp(1678089543, 8),
                        "t" : NumberLong(5)
                },
                "majorityWriteDate" : ISODate("2023-03-06T07:59:03Z")
        },
        "maxBsonObjectSize" : 16777216,
        "maxMessageSizeBytes" : 48000000,
        "maxWriteBatchSize" : 100000,
        "localTime" : ISODate("2023-03-06T07:59:03.595Z"),
        "logicalSessionTimeoutMinutes" : 30,
        "connectionId" : 1486170,
        "minWireVersion" : 0,
        "maxWireVersion" : 13,
        "readOnly" : false,
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1678089543, 8),
                "signature" : {
                        "hash" : BinData(0,"5cRWwdv97PAUESoPYgMqCgfsWYk="),
                        "keyId" : NumberLong("7161241075632308230")
                }
        },
        "operationTime" : Timestamp(1678089543, 8)
}

(3)使用rs.remove()移除

1
2
rs.remove("mongod3.example.net:27017")
rs.remove("mongod3.example.net")

  

 

2.使用rs.reconfig()

也可以通过重新构建复制集来移除节点。

从4.4版本开始,rs.reconfig()支持增加或移除可投票节点,一次只能移除一个节点。
(1)先关闭要移除的mongodb实例

1
db.shutdownServer()

(2)连接到复制集的primary节点
使用db.hello()查看复制集的信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
> db.hello()
{
        "topologyVersion" : {
                "processId" : ObjectId("63956ad40ce6275c36c30464"),
                "counter" : NumberLong(8)
        },
        "hosts" : [
                "192.168.137.24:27017",
                "192.168.137.25:27017",
                "192.168.137.26:27017"
        ],
        "passives" : [
                "13.14.250.11:27017"
        ],
        "setName" : "abce_rs",
        "setVersion" : 2,
        "isWritablePrimary" : true,
        "secondary" : false,
        "primary" : "192.168.137.26:27017",
        "me" : "192.168.137.26:27017",
        "electionId" : ObjectId("7fffffff0000000000000005"),
        "lastWrite" : {
                "opTime" : {
                        "ts" : Timestamp(1678089543, 8),
                        "t" : NumberLong(5)
                },
                "lastWriteDate" : ISODate("2023-03-06T07:59:03Z"),
                "majorityOpTime" : {
                        "ts" : Timestamp(1678089543, 8),
                        "t" : NumberLong(5)
                },
                "majorityWriteDate" : ISODate("2023-03-06T07:59:03Z")
        },
        "maxBsonObjectSize" : 16777216,
        "maxMessageSizeBytes" : 48000000,
        "maxWriteBatchSize" : 100000,
        "localTime" : ISODate("2023-03-06T07:59:03.595Z"),
        "logicalSessionTimeoutMinutes" : 30,
        "connectionId" : 1486170,
        "minWireVersion" : 0,
        "maxWireVersion" : 13,
        "readOnly" : false,
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1678089543, 8),
                "signature" : {
                        "hash" : BinData(0,"5cRWwdv97PAUESoPYgMqCgfsWYk="),
                        "keyId" : NumberLong("7161241075632308230")
                }
        },
        "operationTime" : Timestamp(1678089543, 8)
}

(3)执行rs.conf() 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
> rs.conf()
{
        "_id" : "abce_rs",
        "version" : 2,
        "term" : 5,
        "members" : [
                {
                        "_id" : 0,
                        "host" : "192.168.137.24:27017",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {
 
                        },
                        "secondaryDelaySecs" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 1,
                        "host" : "192.168.137.25:27017",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {
 
                        },
                        "secondaryDelaySecs" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 2,
                        "host" : "192.168.137.26:27017",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {
 
                        },
                        "secondaryDelaySecs" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 3,
                        "host" : "13.14.250.11:27017",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 0,
                        "tags" : {
 
                        },
                        "secondaryDelaySecs" : NumberLong(0),
                        "votes" : 0
                }
        ],
        "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("6361d6c6ba61e054186e592a")
        }
}

(3)保存当前的配置信息

1
cfg = rs.conf()

(4)修改配置信息
比如移除节点(_id为2)

1
cfg.members.splice(2,1)

(5)使用修改后的信息重载复制集的配置

1
rs.reconfig(cfg)

(6)确认

1
rs.conf()

 

  

 

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2022-03-07 MySQL索引的维护
点击右上角即可分享
微信分享提示