副本集操作

官方文档:https://docs.mongodb.com/v3.2/reference/method/js-replication/

1 rs.add()
{
_id: <int>,
host: <string>, // required
arbiterOnly: <boolean>,
buildIndexes: <boolean>,
hidden: <boolean>,
priority: <number>,
tags: <document>,
slaveDelay: <int>,
votes: <number>
}

Add a Secondary to a New Replica Set //添加一个新节点到新的副本集
rs.add( { host: "mongodbd4.example.net:27017" } )
rs.add( "mongodbd4.example.net:27017" )

Add a Secondary to an Existing Replica Set//添加一个新节点到已经存在的副本集
rs.add( { host: "mongodbd4.example.net:27017", priority: 0, votes: 0 } )
rs.status()

Reconfigure the replica set to update the votes and priority of the new member //配置副本集的投票属性
var cfg = rs.conf();
cfg.members[n].priority = 1; // Substitute the correct array index for the new member
cfg.members[n].votes = 1; // Substitute the correct array index for the new member
rs.reconfig(cfg)

WARNING //使用rs.reconfig()命令后,当前的主节点会down掉并发生选举
The rs.reconfig() shell method can force the current primary to step down, which causes an election. When the primary steps down,
the mongod closes all client connections. While this typically takes 10-20 seconds, try to make these changes during scheduled maintenance periods.
Avoid reconfiguring replica sets that contain members of different MongoDB versions as validation rules may differ across MongoDB versions.

Add a Priority 0 Member to a Replica Set //加入新节点,优先级为0
rs.add( { host: "mongodbd4.example.net:27017", priority: 0 } )
Add an Arbiter to a Replica Set //加入仲裁节点
rs.add( { host: "10.15.7.114:28003", arbiterOnly: true } )
添加备份节点
hidden(成员用于支持专用功能):这样设置后此机器在读写中都不可见,并且不会被选举为Primary,但是可以投票,一般用于备份数据
MyMongo:PRIMARY> rs.remove("10.15.7.114:28005"); #删除原来的第二个仲裁节点
MyMongo:PRIMARY> rs.add({host:"10.15.7.114:28005","priority":0,"hidden":true}) #设置为备份节点

添加延迟节点
Delayed(成员用于支持专用功能):可以指定一个时间延迟从primary节点同步数据。主要用于处理误删除数据马上同步到从节点导致的不一致问题
MyMongo:PRIMARY> rs.add({host:"10.15.7.114:28005","priority":0,"hidden":true,"slaveDelay":60})

Secondary-Only:不能成为primary节点,只能作为secondary副本节点,防止一些性能不高的节点成为主节点。
Non-Voting:没有选举权的secondary节点,纯粹的备份数据节点

2 rs.addArb()
rs.addArb(host) Adds a new arbiter to an existing replica set
WARNING//一般来说,避免在一个副本集中部署多个仲裁节点
In general, avoid deploying more than one arbiter per replica set

3 rs.conf()
//查看副本集配置信息
Returns a document that contains the current replica set configuration.
rs.config()
rs.config() is an alias of rs.conf().

4 rs.freeze()
rs.freeze(seconds)
//是当前副本集在指定时间内不能成为主节点(阻止选举)
Makes the current replica set member ineligible to become primary for the period specified

5 rs.help()
//返回副本集的一些函数
Returns a basic help text for all of the replication related shell functions.
MyMongo: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.slaveOk() allow queries on secondary nodes

rs.printReplicationInfo() check oplog size and time range
rs.printSlaveReplicationInfo() check replica set members and replication lag
db.isMaster() check who is primary

reconfiguration helpers disconnect from the database so the shell will display
an error, even if the command succeeds.

6 rs.initiate()
rs.initiate(configuration) //初始化副本集
Initiates a replica set. Optionally, the method can take an argument in the form of a document that holds the configuration of a replica set.
Eg
rs.initiate(
{
_id: "myReplSet",
version: 1,
members: [
{ _id: 0, host : "mongodb0.example.net:27017" },
{ _id: 1, host : "mongodb1.example.net:27017" },
{ _id: 2, host : "mongodb2.example.net:27017" }
]
}
)

7 rs.printReplicationInfo()
//打印副本集的信息,这里为oplog日志
The following example is a sample output from the rs.printReplicationInfo() method run on the primary:
MyMongo:PRIMARY> rs.printReplicationInfo()
configured oplog size: 1024MB
log length start to end: 14939secs (4.15hrs)
oplog first event time: Thu Oct 11 2018 03:45:19 GMT+0800 (CST)
oplog last event time: Thu Oct 11 2018 07:54:18 GMT+0800 (CST)
now: Thu Oct 11 2018 07:54:23 GMT+0800 (CST)
MyMongo:PRIMARY> db.getReplicationInfo
//在slave上运行,命令为db.printSlaveReplicationInfo()
If run on a slave of a master-slave replication, the method calls db.printSlaveReplicationInfo().

8 rs.printSlaveReplicationInfo()
//返回slave与primary的延时
Returns a formatted report of the status of a replica set from the perspective of the secondary member of the set.
MyMongo:SECONDARY> rs.printSlaveReplicationInfo()
source: 10.15.7.114:28002
syncedTo: Thu Oct 11 2018 08:00:08 GMT+0800 (CST)
0 secs (0 hrs) behind the primary

9 rs.reconfig()
rs.reconfig(configuration, force) //{ force: true }
//重新配置已经存在的副本集,重写副本集配置文件,必须在primary节点上运行
Reconfigures an existing replica set, overwriting the existing replica set configuration. To run the method, you must connect to the primary of the replica set.
//重新配置副本集,必须先rs.conf(),在运行新的副本集配置,然后rs.reconfig()
To reconfigure an existing replica set, first retrieve the current configuration with rs.conf(), modify the configuration
document as needed, and then pass the modified document to rs.reconfig().

WARNING//避免在同一个副本集中,出现不同mongodb的版本
Avoid reconfiguring replica sets that contain members of different MongoDB versions as validation rules may differ across MongoDB versions.
//在执行命令rs.reconfig(),primary库会set down,进行重新选举
{ force: true }
WARNING//在执行rs.reconfig()加上force=ture参数,可导致提交的写操作回滚,要小心使用
Using rs.reconfig() with { force: true } can lead to rollback of committed writes. Exercise caution when using this option
Member Priority and Votes
Changed in version 3.2.
Members with priority greater than 0 cannot have 0 votes.
Non-voting members must have priority of 0.
##修改1为主节点,进行重新选举
cfg = rs.conf();
cfg.members[1].priority = 2;
rs.reconfig(cfg);

10 rs.remove()
rs.remove(hostname) //移除副本集成员
MyMongo:PRIMARY> rs.remove("10.15.7.114:28003");

11 rs.slaveOk()
MyMongo:SECONDARY> db.getMongo().setSlaveOk()
//允许在secondry节点上进行读取操作
This allows the current connection to allow read operations to run on secondary members.

12 rs.status()
//返回副本集的当前状态信息
This output reflects the current status of the replica set, using data derived from the heartbeat packets sent by the other members of the replica set.

13 rs.stepDown()
rs.stepDown(stepDownSecs, secondaryCatchUpPeriodSecs)
//此操作将会把primary节点变为secondry,并落后新成为primary节点%秒
Triggers the primary of the replica set to become a secondary. This in turn triggers an election for primary.
The method steps down the primary for a specified number of seconds; during this period, the stepdown member is ineligible from becoming primary.
主节点降为secondary
MyMongo:PRIMARY> use admin
MyMongo:PRIMARY> rs.stepDown(60)#单位为 秒
secondaryCatchUpPeriodSecs //Optional. The number of seconds that mongod will wait for an electable secondary to catch up to the primary.

14 rs.syncFrom()
//临时指定成员要同步的目标
which allows administrators to temporarily override the default sync target for the current member.
Specify the name of the member you want to replicate from in the form of [hostname]:[port].
rs.syncFrom("myHost:27017");

PRIMARY> rs.status().members[1].MyMongo

Mongodb 副本集的节点详细操作的更多相关文章

  1. MongoDB副本集--Secondary节点实例恢复

    场景描述 MongoDB副本集中有一台Secondary节点出现RECOVERING的状态 状态如下: arps:RECOVERING> rs.status() { "set" ...

  2. mongodb副本集仲裁节点搭建

    服务器准备: 主节点192.168.100.106 从节点192.168.100.107 仲裁节点192.168.100.108 三台服务器: 关闭防火墙 service iptables stop ...

  3. MongoDB副本集的常用操作及原理

    本文是对MongoDB副本集常用操作的一个汇总,同时也穿插着介绍了操作背后的原理及注意点. 结合之前的文章:MongoDB副本集的搭建,大家可以在较短的时间内熟悉MongoDB的搭建和管理. 下面的操 ...

  4. MongoDB 副本集的常用操作及原理

    本文是对MongoDB副本集常用操作的一个汇总,同时也穿插着介绍了操作背后的原理及注意点. 结合之前的文章:MongoDB副本集的搭建,大家可以在较短的时间内熟悉MongoDB的搭建和管理. 下面的操 ...

  5. 创建mongodb副本集操作实例

    一:概念 相关概念及图片引用自这里 mongodb副本集: 副本集是一组服务器,其中一个是主服务器,用于处理客户请求:还有多个备份服务器,用于保存主服务器的数据副本.如果主服务器崩溃了,备份服务器自动 ...

  6. MongoDB副本集(一主一备+仲裁)环境部署-运维操作记录

    MongoDB复制集是一个带有故障转移的主从集群.是从现有的主从模式演变而来,增加了自动故障转移和节点成员自动恢复.MongoDB复制集模式中没有固定的主结点,在启动后,多个服务节点间将自动选举产生一 ...

  7. MongoDB 副本集的用户和权限一般操作步骤

    步骤总结: 在主节点上添加超管用户,副本集会自动同步 按照仲裁者.副本节点.主节点的先后顺序关闭所有节点服务 创建副本集认证的key文件,复制到每个服务所在目录 修改每个服务的配置文件,增加参数 启动 ...

  8. nodejs+mongoose操作mongodb副本集实例

    继上一篇设置mongodb副本集之后,开始使用nodejs访问mongodb副本集: 1:创建项目     express 项目名称 2:npm install mongoose    安装mongo ...

  9. mongodb副本集(选举,节点设置,读写分离设置)

    1.相对于传统主从模式的优势 传统的主从模式,需要手工指定集群中的Master.如果Master发生故障,一般都是人工介入,指定新的Master.这个过程对于应用一般不是透明的,往往伴随着应用重新修改 ...

随机推荐

  1. 通过join方法顺序执行多个线程

    方法一:直接用多线程之间的通讯去解决 package com.toov5.test; import javax.imageio.ImageTypeSpecifier; class Res1{ char ...

  2. 阿里云环境搭建CDN内容分发

    1.创建CDN CNAME 指向CDN云厂商地址 2.使用域名转向到CDN云厂商地址 请求过来 通过cnd 分发到不同的服务器  如果有缓存的话 直接走了 CDN也可以实现安全功能,比如CDN实现防止 ...

  3. 算法总结之 数组的partition调整 三个值的升序

    给定一个数组arr, 其中只可能有 0,1,2三个值,请实现arr排序 另一种问法: 有一个数组,只有红 蓝 黄 球,请事先红球全放在数组的左边,蓝球放中间,黄球放右边 另一种问法: 有一个数组,再给 ...

  4. java进阶之-Maven,svn,git,maven合拼多个项目

    git的使用介绍(写很容易懂得哦) maven合拼多个项目(写得很好哦) MAVEN作用:统一开发规范与工具:统一管理jar包 1.下载MAVEN  下载绿色版的面安装 2.环境配置 eclipse想 ...

  5. HBase-存储-概览

    概览 HBase主要处理两种文件:一种是预写日志(Write-Ahead Log,WAL),另一种是实际的数据文件.这两种文件主要由HRegionServer管理.在某些情况下,HMaster也可以进 ...

  6. keystone cache

    http://docs.openstack.org/juno/config-reference/content/section_keystone.conf.html http://docs.opens ...

  7. 使用struts2的iterator标签出现的错误

    错误如下所示: 代码如下所示: <body> <s:debug></s:debug> 获取list的值第一种方式 <!-- 3 获取值栈list集合数据 -- ...

  8. Redis缓存集群方案

    由于单台Redis服务器的内存管理能力有限,使用过大内存的Redis又会使得服务器的性能急剧下降,一旦服务器发生故障将会影响更大范围业务,而Redis 3.0 beta1支持的集群功能还不适合生产环境 ...

  9. Word 2010怎么自动添加文献引用

    1.将光标移至在需要添加引用的地方,比如我下图中在这段文字最后添加一个引用(为了方便说明)   2.(2010版本) 3.点击上面的“引用”,然后点击蓝圈里面的小图标,出现下面对话框,并设置成如图,点 ...

  10. spring: 使用Aspectj代理EnabelAspectjAutoProxy

    使用JavaConfig的话,可以在配置类的类级别上通过使用EnableAspectJ-AutoProxy注解启用自动代理功能. package ch2.test; import org.aspect ...