rstest:PRIMARY> rs.config() { "_id" : rstest",version2protocolVersion" : NumberLong(1),1)">writeConcernMajorityJournalDefault" : truemembers : [ { 0host192.168.10.41:27017arbiterOnlyfalsebuildIndexeshiddenprioritytags : { },1)">slaveDelayvotes },{ 192.168.10.42:27017192.168.10.43:27017 } ],1)">settings : { chainingAllowedheartbeatIntervalMillis2000heartbeatTimeoutSecs10electionTimeoutMillis10000catchUpTimeoutMillis" : -catchUpTakeoverDelayMillis30000getLastErrorModes : { },1)">getLastErrorDefaults : { wwtimeoutreplicaSetId" : ObjectId(5ef1b03c01bba8d9a6759c18) } }
(一)删除副本集成员
删除副本集成员可以使用rs.remove()或者rs.reconfig()。
方法一:使用rs.remove删除"IP为192.168.10.42"的成员
[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... 02.134+0800 I NETWORK [js] trying reconnect to 27017 Failed 0800 I NETWORK [js] reconnect Failed Failed 02.137+ Failed Failed >
STEP2:连接到主节点,不知道主节点,可以使用db.isMaster()确认
STEP3:在主节点上删除成员
rstest:PRIMARY> rs.remove() { ok$clusterTimeclusterTime" : Timestamp(1593359559,signaturehash" : BinData(0,1)">UpD/BEf5OF484ZeHTHCEDReiJKw=keyId6841443127941660675) } },1)">operationTime) } rstest:PRIMARY>
这里不再演示,见官方文档:https://docs.mongodb.com/manual/tutorial/remove-replica-set-member/
(二)添加副本集成员
STEP1:启动节点,节点的启动参数必须含有集群参数。主要参数如下:
# network interfaces net: port: 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: ",priority: 0 } )
注意:当新添加服务器的priority和votes大于0时,在其初始同步期间,即使该服务器由于数据不一致而无法提供读取服务或成为主服务器,但是他仍会作为有表决权的成员,这可能会导致多数投票成员在线但是无法选举主成员的情况。
STEP3:确保新加入的成员状态已经转变为SECONDARY
rs.status()
STEP4:如果新成员状态已经转变为SECONDARY,如果需要,可以使用rs.reconfig()更改新成员的priority和votes。
var cfg = rs.conf(); cfg.members[3].priority = cfg.members[3].votes = rs.reconfig(cfg)
【完】