mongodb replica sets reconfig and conver a Secondary to an Arbiter

replica set由于需求可能会调整节点的优先级,或者仲裁节点
那么先看一下语法:
rs.reconfig(configuration[,force])
Parameters:
  • configuration– Adocumentthat specifies the configuration of a replica set.
  • force– Optional. Specify{force:true}as the force parameter to force the replica set to accept the new configuration even if a majority of the members are not accessible. Use with caution, as this can lead torollbacksituations.

Initializes a newreplica setconfiguration. This function will disconnect the shell briefly and forces a reconnection as the replica set renegotiates which node will beprimary. As a result, the shell will display an error even if this command succeeds.

rs.reconfig()provides a wrapper around the “replSetReconfigdatabase command.

rs.reconfig()overwrites the existing replica set configuration. Retrieve the current configuration object withrs.conf(), modify the configuration as needed and then users.reconfig()to submit the modified configuration object.

To reconfigure a replica set, use the following sequence of operations:

conf = rs.conf()

// modify conf to change configuration

rs.reconfig(conf)

If you want to force the reconfiguration if a majority of the set isn’t connected to the current member, or you’re issuing the command against a secondary, use the following form:

conf = rs.conf()

// modify conf to change configuration

rs.reconfig(conf, { force: true } )

Warning

Forcing ars.reconfig()can lead torollbacksituations and other difficult to recover from situations. Exercise caution when using this option.这里遇到rollback情况,可能会导致fatal状态,那么replica set就等于失效了,可以拷贝local数据文件,或者所有数据文件进行重建

举例:<wbr style="line-height:23px"></wbr>
var c = rs.conf();
c.members[2].priority = 0; 0~100数字越大优先级越高,0不会被切换为primary节点,这里也可以写成包含小数的值
rs.reconfig(c);
配置完以后,可以通过一下命令查看一下配置是否修改好:
rs.conf()
rs.status()
为了防止脑裂,可以加入仲裁节点,使用以下命令:
rs.addArb("<hostname>:<:port>"); ==rs.add(hostspec,arbiterOnly)
示例:
rs.add('mongodb3.example.net:27017',true)
当然将一个从节点转换为仲裁节点:
(1)首先关闭从节点
(2)rs.remove("<hostname><:port>")删除该从节点,rs.conf()验证一下
(3)创建新的数据目录,因为仲裁节点是不存放生产数据的
(4)通过rs.addArb("<hostname>:<:port>")加入,rs.conf()验证一下
那么就可以看到:仲裁节点的属性为"arbiterOnly" : true
{
"_id" : "xxx",
"version" : 14,
"members" : [
{
"_id" : 1,
"host" : "xxx.xxx.xxx.xxx:5281"
},
{
"_id" : 2,
"host" : "xxx.xxx.xxx.xxx:5281",
"arbiterOnly" : true
},
{
"_id" : 3,
"host" : "xxx.xxx.xxx.xxx:5281"
}
]
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值