Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

3.4-MongoDB replica set building

2025-03-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

* MongoDB 副本集(repl set)介绍

早期版本使用master-slave,一主一从和MySQL类似,但slave在此架构中为只读,当主库宕机后,从库不能自动切换为主

目前已经淘汰master-slave模式,改为副本集,这种模式下有一个主(primary),和多个从(secondary),只读。支持给它们设置权重,当主宕掉后,权重最高的从切换为主

在此架构中还可以建立一个仲裁(arbiter)的角色,它只负责裁决,而不存储数据

再此架构中读写数据都是在主上,要想实现负载均衡的目的需要手动指定读库的目标server

* MongoDB副本集搭建

三台机器: 172.7.15.111(primary) 172.7.15.112(secondary) 172.7.15.101(secondary)

编辑三台机器的配置文件,增加:

replication:

##oplog大小 #类似mysql的二进制日志

oplogSizeMB: 20 #注意,此处开头有2个空格

##复制集名称

replSetName: aminglinux #注意,此处开头有2个空格

分别重启三台机器

* MongoDB副本集搭建

连接主,在主上运行命令mongo

>use admin

>config={_id:"aminglinux",members:[{_id:0,host:"172.7.15.111:27017"},{_id:1,host:"172.7.15.112:27017"},{_id:2,host:"172.7.15.101:27017"}]} #config类似变量

>rs.initiate(config) #初始化,如果查看状态有了成员,下面的加载就不用执行了。

>rs.add("172.7.15.112") #加载成员

>rs.add("172.7.15.101")

rs.status() //查看状态

如果两个从上的状态为"stateStr" : "STARTUP", 则需要进行如下操作

> var config={_id:"aminglinux",members:[{_id:0,host:"172.7.15.111:27017"},{_id:1,host:"172.7.15.112:27017"},{_id:2,host:"172.7.15.101:27017"}]}

>rs.reconfig(config)

此时再次查看rs.status()会发现从的状态变为SECONDARY

课后答疑:

问:1、实验中,仲裁者是谁,是不是不要仲裁者也行?

2、实验中,哪个参数是配置优先级的,如果没有,他是怎么判断将哪台机器作为primary的?

3、config={_id:"mongo1",members:[{_id:0,host:"10.10.10.60:27017"},{_id:1,host:"10.10.10.50:27017"},{_id:2,host:"10.10.10.70:27017"}]} ,这个命令里的配置生效后,有没办法进行修改?

答:1 不设置仲裁也没有关系

2 参考文档,http://www.apelearn.com/bbs/thread-9287-1-1.html

3 对config 重新赋值。

建议多使用搜索引擎尝试搜一下

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report