在每一个MongoDB(版本 3.2.9) Instance中,都有一个本地数据库(local),用于存储 Replication 进程的信息和本地数据。local 数据库的特性是:位于local数据库中的数据和集合不会被 Replication 进程复制到其余MongoDB instance上。若是实例上有些collection 和 data不计划被复制到其余MongoDB Instance,能够将这些collection 和 data 存储在local 数据库中。node
MongoDB shell提供一个全局变量rs,是数据库命令的包装器(wrapper),用于维护Replica Set。mongodb
一,Replica Set的配置shell
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 local db.system.replset.find()
配置信息重要信息主要有两部分:Replica Set的 id 值 和 member 数组。app
{ _id: "replica set name", members: [ { _id: <int>, host: "host:port", arbiterOnly: <boolean>, buildIndexes: <boolean>, hidden: <boolean>, priority: <number>, slaveDelay: <int>, votes: <number> }, ... ], ... }
成员的配置文档:async
priority:表示一个成员被选举为Primary节点的优先级,默认值是1,取值范围是从0到100,将priority设置为0有特殊含义:Priority为0的成员永远不能成为Primary 节点。Replica Set中,Priority最高的成员,会优先被选举为Primary 节点,只要其知足条件。函数
hidden:将成员配置为隐藏成员,要求Priority 为0。Client不会向隐藏成员发送请求,所以隐藏成员不会收到Client的Request。ui
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.this
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,即当前成员在必定时间内保持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的大小
修改的过程主要分为三步:
详细过程是:
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 每次启动时的开机日志
参考文档: