MongoDB副本集replica set(三)--添加删除成员

上一篇文章中,我们搭建了3个节点的副本集,集群信息如下:

rstest:PRIMARY> rs.config()
{
    "_id" : "rstest",
    "version" : 2,
    "protocolVersion" : NumberLong(1),
    "writeConcernMajorityJournalDefault" : true,
    "members" : [
        {
            "_id" : 0,
            "host" : "192.168.10.41:27017",
            "arbiterOnly" : false,
            "buildIndexes" : true,
            "hidden" : false,
            "priority" : 2,
            "tags" : {
                
            },
            "slaveDelay" : NumberLong(0),
            "votes" : 1
        },
        {
            "_id" : 1,
            "host" : "192.168.10.42:27017",
            "arbiterOnly" : false,
            "buildIndexes" : true,
            "hidden" : false,
            "priority" : 1,
            "tags" : {
                
            },
            "slaveDelay" : NumberLong(0),
            "votes" : 1
        },
        {
            "_id" : 2,
            "host" : "192.168.10.43:27017",
            "arbiterOnly" : false,
            "buildIndexes" : true,
            "hidden" : false,
            "priority" : 0,
            "tags" : {
                
            },
            "slaveDelay" : NumberLong(0),
            "votes" : 1
        }
    ],
    "settings" : {
        "chainingAllowed" : true,
        "heartbeatIntervalMillis" : 2000,
        "heartbeatTimeoutSecs" : 10,
        "electionTimeoutMillis" : 10000,
        "catchUpTimeoutMillis" : -1,
        "catchUpTakeoverDelayMillis" : 30000,
        "getLastErrorModes" : {
            
        },
        "getLastErrorDefaults" : {
            "w" : 1,
            "wtimeout" : 0
        },
        "replicaSetId" : ObjectId("5ef1b03c01bba8d9a6759c18")
    }
}


(一)删除副本集成员

删除副本集成员可以使用rs.remove()或者rs.reconfig()。

方法一:使用rs.remove删除"IP为192.168.10.42"的成员

STEP1:关闭要删除节点的实例

[root@mongodbserver2 ~]# mongo -u replica -p replica --authenticationDatabase admin 
 MongoDB shell version v4.2.7
 connecting to: mongodb://127.0.0.1:27017/?authSource=admin&compressors=disabled&gssapiServiceName=mongodb
 Implicit session: session { "id" : UUID("946853b3-ad91-44d0-b7df-3001cbfc1af0") }
 MongoDB server version: 4.2.7

rstest:SECONDARY> use admin
 switched to db admin
 rstest:SECONDARY> db.shutdownServer()
 2020-06-28T23:47:02.129+0800 I  NETWORK  [js] DBClientConnection failed to receive message from 127.0.0.1:27017 - HostUnreachable: Connection closed by peer
 server should be down...
 2020-06-28T23:47:02.134+0800 I  NETWORK  [js] trying reconnect to 127.0.0.1:27017 failed
 2020-06-28T23:47:02.134+0800 I  NETWORK  [js] reconnect 127.0.0.1:27017 failed failed 
 2020-06-28T23:47:02.137+0800 I  NETWORK  [js] trying reconnect to 127.0.0.1:27017 failed
 2020-06-28T23:47:02.137+0800 I  NETWORK  [js] reconnect 127.0.0.1:27017 failed failed 
 >

STEP2:连接到主节点,不知道主节点,可以使用db.isMaster()确认

STEP3:在主节点上删除成员

rstest:PRIMARY> rs.remove("192.168.10.42:27017")
{
    "ok" : 1,
    "$clusterTime" : {
        "clusterTime" : Timestamp(1593359559, 1),
        "signature" : {
            "hash" : BinData(0,"UpD/BEf5OF484ZeHTHCEDReiJKw="),
            "keyId" : NumberLong("6841443127941660675")
        }
    },
    "operationTime" : Timestamp(1593359559, 1)
}
rstest:PRIMARY>


方法二:使用rs.reconfig删除成员

这里不再演示,见官方文档:https://docs.mongodb.com/manual/tutorial/remove-replica-set-member/


(二)添加副本集成员

这里演示如何把上面已经删除的节点重新添加回来。

STEP1:启动节点,节点的启动参数必须含有集群参数。主要参数如下:

# network interfaces
 net:
    port: 27017
    bindIp: 0.0.0.0  # Listen to local interface only, comment to listen on all interfaces.
 
 security:
    authorization: enabled
    keyFile: /mongo/mongo-keyfile

 replication:
  replSetName: rstest

STEP2:在主节点上执行添加节点命令

rs.add( { host: "192.168.10.42:27017", priority: 0, votes: 0 } )

注意:当新添加服务器的priority和votes大于0时,在其初始同步期间,即使该服务器由于数据不一致而无法提供读取服务或成为主服务器,但是他仍会作为有表决权的成员,这可能会导致多数投票成员在线但是无法选举主成员的情况。

STEP3:确保新加入的成员状态已经转变为SECONDARY

rs.status()

STEP4:如果新成员状态已经转变为SECONDARY,如果需要,可以使用rs.reconfig()更改新成员的priority和votes。

var cfg = rs.conf();
cfg.members[3].priority = 1
cfg.members[3].votes = 1
rs.config(cfg)



【完】

MongoDB副本集replica set(三)--添加删除成员

上一篇:Mysql之DDL操作


下一篇:MySQL使用子查询作为delete或update的条件