前置
搭建集群可参考用Docker搭建MongoDB集群(副本集)并启用认证用户名密码登录
管理员用户连接MongoDB
mongosh -u admin1 -p 1234567
添加节点
1. 查看当前集群配置
rs.config()
返回结果:
{
_id: 'rs0',
version: 5,
term: 5,
members: [
{
_id: 0,
host: '192.168.10.2:30001',
arbiterOnly: false,
buildIndexes: true,
hidden: false,
priority: 1,
tags: {},
secondaryDelaySecs: Long("0"),
votes: 1
},
{
_id: 1,
host: '192.168.10.2:30002',
arbiterOnly: false,
buildIndexes: true,
hidden: false,
priority: 1,
tags: {},
secondaryDelaySecs: Long("0"),
votes: 1
},
{
_id: 2,
host: '192.168.10.2:30003',
arbiterOnly: true,
buildIndexes: true,
hidden: false,
priority: 0,
tags: {},
secondaryDelaySecs: Long("0"),
votes: 1
},
{
_id: 3,
host: '192.168.10.2:30004',
arbiterOnly: false,
buildIndexes: true,
hidden: false,
priority: 1,
tags: {},
secondaryDelaySecs: Long("0"),
votes: 1
}
],
protocolVersion: Long("1"),
writeConcernMajorityJournalDefault: true,
settings: {
chainingAllowed: true,
heartbeatIntervalMillis: 2000,
heartbeatTimeoutSecs: 10,
electionTimeoutMillis: 10000,
catchUpTimeoutMillis: -1,
catchUpTakeoverDelayMillis: 30000,
getLastErrorModes: {},
getLastErrorDefaults: { w: 1, wtimeout: 0 },
replicaSetId: ObjectId("63ad544635d3487bbd1a2215")
}
}
可以看到,members
数组中是节点配置,增加节点删除节点,其实就是增删操作members
数组。
2.添加新节点
新节点的配置文件要与集群中的节点配置文件一致。
建议先将priority及votes设为0,即不会选为主(priority默认1),也没有投票特性(votes默认1,有投票权)。
等该节点同步完,再将priority、votes设为1。
rs.add( { host: "192.168.10.2:30005", priority: 0, votes: 0 } )
返回ok即添加完成,等待他同步完。
3. 修改新节点的priority、votes属性
新节点同步完后,状态会变为SECONDARY
,然后修改priority、votes属性。
先rs.config()
查看一下当前配置,这里只截取了新节点的。
{
_id: 4,
host: '192.168.10.2:30005',
arbiterOnly: false,
buildIndexes: true,
hidden: false,
priority: 0,
tags: {},
secondaryDelaySecs: Long("0"),
votes: 0
}
如果没有删除过节点,_id
的值和members
数组下标一致。不确定就数一下,或者输出看一下。
修改priority、votes属性
var cfg = rs.conf()
cfg.members[4].priority = 1
cfg.members[4].votes = 1
rs.reconfig(cfg)
如果修改属性遇到报错MongoServerError: Reconfig attempted to install a config that would change the implicit default write concern. Use the setDefaultRWConcern command to set a cluster-wide write concern and try the reconfig again.
可先执行这条命令,然后再修改priority、votes属性
db.adminCommand( {"setDefaultRWConcern" : 1, "defaultWriteConcern" : { "w" : 2 } } )
修改成功后再rs.config()
查看配置,可以发现节点4的配置已经和其他节点一致了,至此添加节点完成
{
_id: 3,
host: '192.168.10.2:30004',
arbiterOnly: false,
buildIndexes: true,
hidden: false,
priority: 1,
tags: {},
secondaryDelaySecs: Long("0"),
votes: 1
},
{
_id: 4,
host: '192.168.10.2:30005',
arbiterOnly: false,
buildIndexes: true,
hidden: false,
priority: 1,
tags: {},
secondaryDelaySecs: Long("0"),
votes: 1
}
另外,db.printSecondaryReplicationInfo()
可以查看副本节点的同步状态
source: 192.168.10.2:30002
{
syncedTo: 'Thu Jan 05 2023 02:40:51 GMT+0000 (Coordinated Universal Time)',
replLag: '0 secs (0 hrs) behind the primary '
}
---
source: 192.168.10.2:30004
{
syncedTo: 'Thu Jan 05 2023 02:40:51 GMT+0000 (Coordinated Universal Time)',
replLag: '0 secs (0 hrs) behind the primary '
}
---
source: 192.168.10.2:30005
{
syncedTo: 'Thu Jan 05 2023 02:40:51 GMT+0000 (Coordinated Universal Time)',
replLag: '0 secs (0 hrs) behind the primary '
}
如果用Studio 3T连接集群,刷新后可以看到当前的集群成员
删除节点
rs.remove("192.168.10.2:30004");
返回ok就是删除成功了,然后rs.config()
查看一下配置,发现端口为30004的节点3已经没了
{
_id: 2,
host: '192.168.10.2:30003',
arbiterOnly: true,
buildIndexes: true,
hidden: false,
priority: 0,
tags: {},
secondaryDelaySecs: Long("0"),
votes: 1
},
{
_id: 4,
host: '192.168.10.2:30005',
arbiterOnly: false,
buildIndexes: true,
hidden: false,
priority: 1,
tags: {},
secondaryDelaySecs: Long("0"),
votes: 1
}
Studio 3T刷新后也能看到端口为30004的节点3已经没了