为了账号安全,请及时绑定邮箱和手机立即绑定

MongoDB 维护Replica Set笔记

标签:
MongoDB

在每个MongoDB(版本 3.2.9) Instance中,都有一个本地数据库(local),用于存储 Replication 进程的信息和本地数据。local 数据库的特性是:位于local数据库中的数据和集合不会被 Replication 进程复制到其他MongoDB instance上。如果实例上有些collection 和 data不计划被复制到其他MongoDB Instance,可以将这些collection 和 data 存储在local 数据库中。

MongoDB shell提供一个全局变量rs,是数据库命令的包装器(wrapper),用于维护Replica Set。

一,Replica Set的配置

1,查看Replica Set的配置信息

MongoDB 将Replica Set的配置信息存储在local.system.replset 集合中,在同一个Replica Set中所有成员local.system.replset是相同的,不能直接修改该集合,必须通过rs.initiate()来初始化,通过 rs.reconfig()来重新配置,对Replica Set 增加或删除成员都会相应的修改Replica Set的配置信息。

rs.config()use localdb.system.replset.find()

配置信息重要信息主要有两部分:Replica Set的 id 值 和 member 数组。


{
_id: "replica set name",
members: [
    {
      _id: <int>,
      host: "host:port",
      arbiterOnly: <boolean>,
      buildIndexes: <boolean>,
      hidden: <boolean>,
      priority: <number>,
      slaveDelay: <int>,
      votes: <number>
    },
    ...
  ],
...
}


成员的配置文档:

priority:表示一个成员被选举为Primary节点的优先级,默认值是1,取值范围是从0到100,将priority设置为0有特殊含义:Priority为0的成员永远不能成为Primary 节点。Replica Set中,Priority最高的成员,会优先被选举为Primary 节点,只要其满足条件。

hidden:将成员配置为隐藏成员,要求Priority 为0。Client不会向隐藏成员发送请求,因此隐藏成员不会收到Client的Request。

slaveDelay:单位是秒,将Secondary 成员配置为延迟备份节点,要求Priority 为0,表示该成员比Primary 成员滞后指定的时间,才能将Primary上进行的写操作同步到本地。为了数据读取的一致性,应将延迟备份节点的hidden设置为true,避免用户读取到明显滞后的数据。Delayed members maintain a copy of the data that reflects the state of the data at some time in the past.

votes:有效值是0或1,默认值是1,如果votes是1,表示该成员(voting member)有权限选举Primary 成员。在一个Replica Set中,最多有7个成员,其votes 属性的值是1。

arbiterOnly:表示该成员是仲裁者,arbiter的唯一作用是就是参与选举,其votes属性是1,arbiter不保存数据,也不会为client提供服务。

buildIndexes:表示实在在成员上创建Index,该属性不能修改,只能在增加成员时设置该属性。如果一个成员仅仅作为备份,不接收Client的请求,将该成员设置为不创建index,能够提高数据同步的效率。

2,重新配置Replica Set

对Replica Set重新配置,必须连接到Primary 节点;如果Replica Set中没有一个节点被选举为Primary,那么,可以使用force option(rs.reconfig(config,{force:true})),在Secondary 节点上强制对Replica Set进行重新配置。

The force parameter allows a reconfiguration command to be issued to a non-primary node. If set as { force: true }, this forces 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.

示例,在primary 节点中,重新配置成员的优先级属性(priority)。

cfg = rs.conf()cfg.members[0].priority = 1cfg.members[1].priority = 1cfg.members[2].priority = 5rs.reconfig(cfg)

3,增加成员

3.1,该使用默认的配置增加成员

--增加一个成员,用于存储数据
rs.add("host:port")

--增加一个arbiter,用于选举
rs.add("host:port",true)

3.2,使用配置文档增加成员

示例,为Replica Set增加一个延迟备份的隐藏节点,滞后Primary节点1hour,该节点不参与投票,也不创建index,仅仅作为数据备份。

rs.add( { _id:4, host: "host:port", priority: 0, hidden:true, slaveDelay:3600, votes:0, buildIndexes:true, arbiterOnly:false } )

4,删除成员

rs.remove("host")

5,对replica set重新配置,能够增加成员,删除成员,并能同时修改成员的属性

二,对Replica Set的成员进行操作

1,冻结当前成员,使当前成员在指定的时间内没有资格成为Primary,即当前成员在一定时间内保持Secondary身份

Makes the current replica set member ineligible to become primary for the period specified.

rs.freeze(seconds)

2,强制 Primary 节点退化为Secondary节点

rs.stepDown()使当前Primary节点退化为Secondary 节点,并激发选举Primary的事件。该函数使当前的Primary 节点在指定的时间内,不能成为Primary节点。在一定的时间内,如果有 Secondary 节点满足条件,那么该Secondary节点被选举为Primary 节点;如果没有 Secondary 节点满足条件,那么原Primary节点参与选举。stepDown函数,使Primary节点退化为Secondary,并在一段时间内不能参与选举。

Forces the primary of the replica set to become a secondary, triggering 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.

rs.stepDown(stepDownSecs, secondaryCatchUpPeriodSecs)

3,强制当前成员从指定成员同步数据

rs.syncFrom("host:port");

4,使当前的Secondary 节点能够读取数据

默认情况下,Secondary 节点是不能读取数据的

rs.slaveOk()

三,查看Replica Set的状态

set字段:Replica Set的name

stateStr:成员状态的描述信息

name:该成员的host 和 端口

syncTo:该成员从哪个成员同步数据,可以使用rs.syncFrom()强制同步的Path,从指定的 Target 成员同步数据。


{
    "set" : "rs0",
    "myState" : 1,
    "heartbeatIntervalMillis" : NumberLong(2000),
    "members" : [ 
        {
            "_id" : 0,
            "name" : "cia-sh-05:40004",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 240973,
            "optime" : {
                "ts" : Timestamp(1473336939, 1),
                "t" : NumberLong(5)
            },
            "optimeDate" : ISODate("2016-09-08T12:15:39.000Z"),
            "lastHeartbeat" : ISODate("2016-09-10T04:39:55.041Z"),
            "lastHeartbeatRecv" : ISODate("2016-09-10T04:39:56.356Z"),
            "pingMs" : NumberLong(0),
            "syncingTo" : "cia-sh-06:40001"
        }, .....
    ]
}


三,Replica Set的操作日志

MongoDB的Replication实际上是基于操作日志(operation log)的复制。Replication进程的整个过程是:Replication 将Primary节点中执行的写操作记录到oplog集合中,Secondary成员读取Primary 成员的oplog集合,重做(redo)oplog中记录的写操作,最终,Replica Set中的各个成员达到数据的一致性。

oplog集合中记录的操作是基于单个doc的,也就是说,如果一条命令只影响一个doc,那么Replication向oplog集合中插入一个操作命令;如果一个命令影响多个doc,那么Replication将该命令拆分成多个等效的操作命令,每个操作命令只会影响一个doc,最终向oplog集合中插入的是多个操作命令。

1,oplog 集合

oplog集合是一个特殊的固定集合,存储的是Primary节点的操作日志,每个Replica Set的成员都一个oplog的副本:local.oplog.rs,该集合存储在每个成员的local数据库中。Replica Set中的每个成员都有一个oplog集合,用于存储当前节点的操作记录,其他成员能够从任何一个成员的oplog中同步数据。

The oplog (operations log) is a special capped collection that keeps a rolling record of all operations that modify the data stored in your databases. MongoDB applies database operations on the primary and then records the operations on the primary’s oplog. The secondary members then copy and apply these operations in an asynchronous process. All replica set members contain a copy of the oplog, in the local.oplog.rs collection, which allows them to maintain the current state of the database.

2,oplog的大小

oplog集合是一个固定集合,其大小是固定的,在第一次开始Replica Set的成员时,MongoDB创建默认大小的oplog。在MongoDB 3.2.9 版本中,MongoDB 默认的存储引擎是WiredTiger,一般情况下,oplog的默认大小是数据文件所在disk 空闲空间(disk free space)的5%,最小不会低于990 MB,最大不会超过50 GB。

3,修改oplog的大小

修改的过程主要分为三步:

  • 以单机模式重启mongod

  • 启动之后,重新创建oplog,并保留最后一个记录作为种子

  • 以复制集方式重启mongod

详细过程是:

step1:以单机模式重启mongod

对于Primary成员,首先调用stepDown函数,强制Primary成员转变为Secondary成员

rs.stepDown()

对于secondary成员,调用shutdownServer()函数,关闭mongod

use admin db.shutdownServer()

启动mongod实例,不要使用replset参数

mongod --port 37017 --dbpath /srv/mongodb

step2:创建新的oplog

有备无患,备份oplog文件

mongodump --db local --collection 'oplog.rs' --port 37017

将oplog中最后一条有效记录保存到temp 集合中,作为新oplog的seed

use local

db.temp.drop()
db.temp.save( db.oplog.rs.find( { }, { ts: 1, h: 1 } ).sort( {$natural : -1} ).limit(1).next() )

db.oplog.rs.drop()

重建新的oplog集合,并将temp集合中一条记录保存到oplog中,size的单位是Byte

db.runCommand( { create: "oplog.rs", capped: true, size: (2 * 1024 * 1024 * 1024) } )db.oplog.rs.save( db.temp.findOne() )

step3:以复制集模式启动 mongod,replset参数必须制定正确的Replica Set的名字

db.shutdownServer()mongod --replSet rs0 --dbpath /srv/mongodb

三,查看mongod 的开机日志

在local.startup_log 集合中,存储mongod 每次启动时的开机日志。

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消