目录
副本集(replica set)是一组mongod进程维护的相同的数据集,提供了MongoDB的冗余和高可用,如果副本集中的成员出现故障,变得不可用,需要将其进行移除副本集,本篇将对现有副本集环境中出现故障的成员进行移除(假定副本集成员192.168.56.105出现故障)。
1 软件环境
使用的软件分别为:
- VirtualBox 6.0
- Oracle Linux 6.7
- MongoDB 4.2.0
2 移除副本集成员
移除副本集成员,可以使用下面中的任意一种方法。
2.1 使用rs.remove()移除成员
1)关闭将要移除的成员
rep1:SECONDARY> db.shutdownServer()
2019-10-02T22:25:11.615+0800 I NETWORK [js] DBClientConnection failed to receive message from localhost:27017 - HostUnreachable: Connection closed by peer
server should be down...
2019-10-02T22:25:11.617+0800 I NETWORK [js] trying reconnect to localhost:27017 failed
2019-10-02T22:25:11.623+0800 I NETWORK [js] reconnect localhost:27017 failed failed
2019-10-02T22:25:11.626+0800 I NETWORK [js] trying reconnect to localhost:27017 failed
2019-10-02T22:25:11.626+0800 I NETWORK [js] reconnect localhost:27017 failed failed
2)连接到当前主库,执行如下命令,若不知道主库,可使用db.isMaster()判断
rep1:PRIMARY> rs.remove('192.168.56.105:27017')
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1570026484, 1),
"signature" : {
"hash" : BinData(0,"n/lSB5CsYD6cVjnpiBi8kTSxNbs="),
"keyId" : NumberLong("6743187251305381890")
}
},
"operationTime" : Timestamp(1570026484, 1)
}
2.2 使用rs.reconfig()移除成员
1)关闭将要移除的成员
rep1:SECONDARY> db.shutdownServer()
2019-10-02T22:33:22.484+0800 I NETWORK [js] DBClientConnection failed to receive message from localhost:27017 - HostUnreachable: Connection closed by peer
server should be down...
2019-10-02T22:33:22.486+0800 I NETWORK [js] trying reconnect to localhost:27017 failed
2019-10-02T22:33:22.486+0800 I NETWORK [js] reconnect localhost:27017 failed failed
2)连接到当前主库,执行如下命令,若不知道主库,可使用db.isMaster()判断
rep1:PRIMARY> var cfg=rs.conf()
rep1:PRIMARY> cfg.members.splice(3,1)
[
{
"_id" : 3,
"host" : "192.168.56.105:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
]
rep1:PRIMARY> rs.reconfig(cfg)
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1570026992, 1),
"signature" : {
"hash" : BinData(0,"fSofKob0xAqxxd72GXms588DmaA="),
"keyId" : NumberLong("6743187251305381890")
}
},
"operationTime" : Timestamp(1570026992, 1)
}
2.3 查看移除成员后的配置信息
rep1:PRIMARY> rs.conf()
{
"_id" : "rep1",
"version" : 7,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"members" : [
{
"_id" : 0,
"host" : "192.168.56.102:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "192.168.56.103:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "192.168.56.104:27017",
"arbiterOnly" : true,
"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("5d949d0840e7ee7b725ee3c6")
}
}
rep1:PRIMARY>
至此,完成了副本集中成员出现故障,或者移除某个成员的演示。