这样就可以看到27017 是主节点,其他的不是 。
然后rs 是一个mongo 命令的封装:
mydb:PRIMARY> rs.help() rs.status(){ replSetGetStatus : 1 } checks repl set status rs.initiate(){ replSetInitiate : null } initiates set with default settings rs.initiate(cfg){ replSetInitiate : cfg } initiates set with configuration cfg rs.conf()get the current configuration object from local.system.replset rs.reconfig(cfg)updates the configuration of a running replica set with cfg (disconnects) rs.add(hostportstr)add a new member to the set with default attributes (disconnects) rs.add(membercfgobj)add a new member to the set with extra attributes (disconnects) rs.addArb(hostportstr)add a new member which is arbiterOnly:true (disconnects) rs.stepDown([stepdownSecs, catchUpSecs])step down as primary (disconnects) rs.syncFrom(hostportstr)make a secondary sync from the given member rs.freeze(secs)make a node ineligible to become primary for the time specified rs.remove(hostportstr)remove a host from the replica set (disconnects) rs.secondaryOk()allow queries on secondary nodes rs.printReplicationInfo()check oplog size and time range rs.printSecondaryReplicationInfo()check replica set members and replication lag db.isMaster()check who is primary db.hello()check who is primary reconfiguration helpers disconnect from the database so the shell will display an error, even if the command succeeds.
可以看下哪些是自己想要的 。
简单的实验非主节点是否可以写入数据:
![mongodb基础整理篇————副本概念篇[外篇]](http://shimg.jingyanzongjie.com/230725/2355254959-0.png)
文章插图
结果是不行的 。
主节点写入是否从节点会同步:
连接27017 判断是否是主节点:
![mongodb基础整理篇————副本概念篇[外篇]](http://shimg.jingyanzongjie.com/230725/2355252645-1.png)
文章插图
那么开始连接一个从节点,并从节点上读取数据,和mysql 一样 。
![mongodb基础整理篇————副本概念篇[外篇]](http://shimg.jingyanzongjie.com/230725/23552532G-2.png)
文章插图
可以看到不让我们进行访问
这里面是一个这样的机制:
因为从节点落后于主节点,那么默认情况下是不让从从节点上读取数据 。
这和mysql 是不一样的 。
那么是否能修改配置来从从节点上读取数据,这也是可以的 。
![mongodb基础整理篇————副本概念篇[外篇]](http://shimg.jingyanzongjie.com/230725/2355252V8-3.png)
文章插图
可以修改客户端的连接配置,可以来进行完成, 然后就可以访问从节点数据了 。
下面来实验,如果主节点没了,那么是否这个副本集是否就没法使用了?
db.adminCommand({"shutdown":1});
使用上面这个命令进行关闭 。![mongodb基础整理篇————副本概念篇[外篇]](http://shimg.jingyanzongjie.com/230725/2355254649-4.png)
文章插图
可以看到的确关闭了 。
那么随便进入一个节点看下 。
进入27018 查看 。
![mongodb基础整理篇————副本概念篇[外篇]](http://shimg.jingyanzongjie.com/230725/2355252O4-5.png)
文章插图
那么主节点就变成了27018了 。
用rs.status() 查看27017的状况:
![mongodb基础整理篇————副本概念篇[外篇]](https://www.huyubaike.com//bbsmax.ikafan.com/static/L3Byb3h5L2h0dHBzL2ltZzIwMjIuY25ibG9ncy5jb20vYmxvZy8xMjg5Nzk0LzIwMjIxMC8xMjg5Nzk0LTIwMjIxMDE2MTE1MTUyNTA2LTgwMjIyMDkwOS5wbmc=.jpg)
文章插图
已经不健康了,那么再次启动27017一下 。
![mongodb基础整理篇————副本概念篇[外篇]](http://shimg.jingyanzongjie.com/230725/235525D29-7.png)
文章插图
27017 就变成从节点了 。
这个时候要增加副本集怎么办:
可以使用rs.add({"localhost":"27020"})
如果要删除怎么办:
可以使用rs.remove({"localhost":"27020"})
如果进行修改怎么办:
var config = rs.config()config.members[0].host="localhost":"27017"rs.reconfig(config)
这样就从新加载了 。
副本集的一些理论知识前面知道了,副本集3个情况下,如果一个没了,还是可以运行的 。
经验总结扩展阅读
- CentOS 8.2 对k8s基础环境配置
- 没有物质基础的爱情,哪些星座配对走不远
- python基础--简单数据类型预览
- python基础-较复杂数据类型预览
- 分布式存储系统之Ceph集群RadosGW基础使用
- python的基本运用
- 零基础怎样玩好露娜(玩好露娜的核心诀窍)
- 一 网络安全主动进攻之DNS基础和ettercap实现DNS流量劫持
- 钉钉企业版怎么收费
- SQL基础语句入门