上一篇文章中,我们搭建了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)

【完】

最新文章

  1. C#中常用的系统内置委托
  2. ArcEngine :The XY domain on the spatial reference is not set or invalid错误
  3. Windows 7 添加MBR文件启动
  4. UIImagePickerController 获取相片视频
  5. 行转列求和:不加 in 条件,sum的数据会不会准确?
  6. ProxyFactory的一个问题
  7. 安装64位ubuntu 14.04-搭建android开发环境
  8. 看到当年自己学SQL Server 的笔记
  9. ASP.NET MVC:Razor 引入命名空间
  10. Mac环境下.Net开发
  11. 在linux下使用百度ueditor编辑器上传图片
  12. vfd折腾(二)
  13. form表单中新增button按钮,点击按钮表单会进行提交
  14. OJ:重载 << 运算符
  15. a标签下载
  16. HTTP协议冷知识大全
  17. Ajax请求全局配置
  18. ngxin开启rewrite伪静态
  19. Vivado中xilinx_BRAM IP核使用
  20. windows平台安装redis服务

热门文章

  1. Alpha冲刺 —— 5.7
  2. 使用turtle库绘制一个红色五角星图形‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪
  3. Java实现 LeetCode 92 反转链表 II
  4. java正则匹配 指定内容以外的 内容
  5. 快速幂解法--x^n
  6. SmokePing 快速搭建
  7. 关于adb命令的基本使用
  8. 一文说通Dotnet Core的后台任务
  9. 利用BeanMap进行对象与Map的相互转换
  10. win系统DOS批处理命令:每日根据定时计划,弹出相应的提醒