MongoDB 维护Replica Set

Posted 悦光阴

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MongoDB 维护Replica Set相关的知识,希望对你有一定的参考价值。

在每个MongoDB 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 集合中,不能直接修改该集合,必须通过rs.initiate()来初始化,通过 rs.reconfig()来重新配置,对Replica Set 增加或删除成员都会相应的修改Replica Set的配置信息。

rs.config()

use local
db.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 = 1
cfg.members[1].priority = 1
cfg.members[2].priority = 5
rs.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。

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

rs.freeze(seconds)

2,谋划Primary 节点退位

rs.stepDown()触发选举Primary的事件,使当前的Primary 节点在指定的时间内,不能成为Primary 节点。如果有Secondary 节点满足条件,那么Primary 节点下课,其他节点被选举为Primary 节点;如果没有Secondary 节点满足条件,那么该节点继续作为Primary 节点。

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实际上是基于操作日志的复制,Primary节点将执行的写操作记录到oplog中,Replication将Primary 的oplog同步到其他成员中,其他成员重做(redo)oplog中记录的写操作,最终,Replica Set中的各个成员达到数据的一致性。

在local数据库中,集合 local.oplog.rs 是一个固定大小的集合,存储Primary的操作日志(oplog)。
local.oplog.rs is the capped collection that holds the oplog. You set its size at creation using the oplogSizeMB setting. To resize the oplog after replica set initiation, use the Change the Size of the Oplog procedure.

 

三,查看mongod 的开机日志

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

 

参考文档:

The local Database

Replica Set Configuration

以上是关于MongoDB 维护Replica Set的主要内容,如果未能解决你的问题,请参考以下文章

MongoDB副本集replica set --基础知识

mongodb replica set搭建

解锁MongoDB replica set核心姿势

Mongodb如何创建mongodb的replica set

Replica Set副本集方式的mongodb集群搭建

如何重命名MongoDB中的replica set