代码改变世界

【MongoDB】移除复制集成员

2023-03-07 09:26  abce  阅读(238)  评论(0编辑  收藏  举报

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

db.shutdownServer()

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

> 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()移除

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

  

 

2.使用rs.reconfig()

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

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

db.shutdownServer()

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

> 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() 

> 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)保存当前的配置信息

cfg = rs.conf()

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

cfg.members.splice(2,1)

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

rs.reconfig(cfg)

(6)确认

rs.conf()