mongodb replica sets reconfig and conver a Secondary to an Arbiter
  TEZNKK3IfmPf 2023年11月12日 30 0
replica set由于需求可能会调整节点的优先级,或者仲裁节点
那么先看一下语法:
rs.reconfig(configuration[force])
Parameters:
  • configuration – A document that 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 to rollback situations.

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

rs.reconfig() provides a wrapper around the “replSetReconfig” database command.

rs.reconfig() overwrites the existing replica set configuration. Retrieve the current configuration object with rs.conf(), modify the configuration as needed and then use rs.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 a rs.reconfig() can lead to rollback situations and other difficult to recover from situations. Exercise caution when using this option.这里遇到rollback情况,可能会导致fatal状态,那么replica set就等于失效了,可以拷贝local数据文件,或者所有数据文件进行重建

举例:
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(hostspecarbiterOnly)
示例:
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"
                }
        ]
}
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月12日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   2023年11月14日   62   0   0 mongodb
  TEZNKK3IfmPf   2023年11月15日   50   0   0 shiromongodb
  TEZNKK3IfmPf   2023年11月15日   31   0   0 mongodbjava
  TEZNKK3IfmPf   2024年03月29日   60   0   0 mongodb
  TEZNKK3IfmPf   2023年11月15日   29   0   0 mongodb
TEZNKK3IfmPf