mongoDB-3.x Replica Set集群
官方文档:
参考文档:http://blog.csdn.net/huaishu/article/details/9253769
oplogSizeMB:
5120
replSetName:
rs0
secondaryIndexPrefetch: all
enableMajorityReadConcern: false
操作后,成功加入了一台ARBITER,与此同时,我重启过之前的PRIMARY 192.168.192.10,可以看到,选举后192.168.192.20提升成了PRIMARY, 相应的192.168.192.10则转变为了SECONDARY
常用操作
简言之,replication提供数据冗余高可用,还相应提升了读(多节点分摊)性能
模型:
The minimum requirements for a replica set are:
A primary,
a secondary,
and an arbiter.
Most deployments, however, will keep three members that store data:
A primary and
two secondary
members.
Changed
in version 3.0.0:
有点类似,redis中的Master(Primary),Slave(Secondary),Redis-Sentinel(Arbiter)
角色:
Primary: Replica集群中唯一支持写入的成员,有且仅有1个Primary,支持读和选举
Secondary: 通过异步oplog并应用操作来同步Primary数据,参与选举,至少有1个Secondary
You can configure a secondary member for a specific purpose. You can configure a secondary to:
- Prevent it
from becoming a primary in an election, which allows it to reside
in a secondary data center or to serve as a cold standby.
See
Priority 0 Replica Set Members. - Prevent
applications from reading from it, which allows it to run
applications that require separation from normal traffic.
See
Hidden Replica Set Members. - Keep a
running “historical” snapshot for use in recovery from certain
errors, such as unintentionally deleted databases.
See
Delayed Replica Set Members.
注意:
Priority 0 的Secondary禁止通过选举提升为Primary
Hidden Replica的Secondary禁止被客户端读取
Delayed
Replica的Secondary延时同步,也称“在线备份”,可避免人为失误,在一段时间内可用于恢复(“rolling
backup” or a running “historical” snapshot of the data
set)
Arbiter:
不存储数据副本,消耗资源少,参与选举,不应和Primary,Secondary在同一主机上
安装mongoDB
集群所有节点先安装上mongoDB
配置集群
A.3节点Replica
一.Primary with Two
Secondary
部署参考
测试节点
Primary: 192.168.192.10
Secondary: 192.168.192.21
Secondary: 192.168.192.22
1.配置文件(所有节点)
replication:
或 命令行参数
--oplogSize
5120 --replSet rs0
2.配置Replica Set
进入mongo shell
rsconfig = {_id: 'rs0', members: [
{_id: 0, host:
'192.168.192.10:27017',priority:1},
{_id: 1, host: '192.168.192.20:27017'},
{_id: 2, host: '192.168.192.21:27017'}]
}
rs.initiate(rsconfig)
rs.conf()
rs.status()
health: 1正常,0异常
state: 1代表PRIMARY,可读写,2代表SECONDARY,只读
后期加节点,可以在primary节点mongo
shell下通过rs.add()来添加,如再加一台
rs.add("192.168.192.22:27017")
补充, 还可以通过如下函数来初始化
db.runCommand(
{"replSetInitiate":{"_id":"rs0","members":[
{"_id":0,"host":"192.168.192.10:27017"},
{"_id":1,"host":"192.168.192.20:27017"},
{"_id":2,"host":"192.168.192.21:27017","shardOnly":true}]}
})
示例1.Priority 0 Replica
在primary节点mongo shell下执行
cfg = rs.conf()
cfg.members[2].priority =
0
rs.reconfig(cfg)
rs.conf()
priority值0-1000,默认为1
在primary节点mongo shell下执行
cfg = rs.conf()
cfg.members[2].priority =
0
cfg.members[2].hidden =
true
rs.reconfig(cfg)
rs.conf()
示例3.Delayed
Replica
在primary节点mongo shell下执行
cfg = rs.conf()
cfg.members[2].priority =
0
cfg.members[2].hidden =
true
cfg.members[2].slaveDelay =
3600
rs.reconfig(cfg)
rs.conf()
可以延时"快照或备份"1个小时前的数据副本,延时的时间一定要大于恢复窗口所需时间,可以按实际调整
二.Primary with a Secondary and an
Arbiter
添加一个Arbiter
rs.addArb("192.168.192.11:27017")
rs.status()
操作后,成功加入了一台ARBITER,与此同时,我重启过之前的PRIMARY 192.168.192.10,可以看到,选举后192.168.192.20提升成了PRIMARY, 相应的192.168.192.10则转变为了SECONDARY
Read Preference
Read Preference Mode | Description |
---|---|
primary |
Default mode. All operations read from the current replica
set |
primaryPreferred |
In most situations, operations read from
the |
secondary |
All operations read from the |
secondaryPreferred |
In most situations, operations read from |
nearest |
Operations read from member of the |
在primary节点mongo shell下执行
conf = rs.conf()
conf.members[0].tags = { "dc":
"east", "use": "production" }
conf.members[1].tags = { "dc":
"east", "use": "reporting" }
conf.members[2].tags = { "dc":
"east", "use": "backup" }
rs.reconfig(conf)
rs.conf()
db.collection.find().readPref({mode:
'secondaryPreferred',tags: [{'dc': 'east'}]})
客户端
db.getMongo().setReadPref('nearest')
还可以继续细分
client1:
db.getMongo().setReadPref('nearest',[{ "dc":
"east", "use": "production" },{ "dc": "east",
"use": "reporting" },{}])
client2:
db.getMongo().setReadPref('nearest',[{ "dc":
"east", "use": "reporting" },{ "dc": "east",
"use": "production" },{}])
示例:python客户端,pymongo
import pymongo
client = pymongo.MongoReplicaSetClient('xxxhost: yyyport',
replicaSet='my_set',
readPreference='secondaryPreferred')
db.runCommand( { addshard : "example.net:34008",
maxSize : 125 } )
常用操作
打开mongo
shell
1.查看节点状态
rs.isMaster()
show dbs
use local
db.system.replset.find()
2.副本oplog查询
show collections
db.oplog.rs.find()
字段描述
ts:某个操作的时间戳
op:操作类型:如下:
i:insert
d:delete
u:update
ns:命名空间,也就是操作的collection name
o:doucment的内容
3.master oplog meta data
db.printReplicationInfo()
字段说明
configured oplog size:配置的oplog文件大小。
log length start to end:oplog日志的启用时间段。
oplog first event time:第一个事务日志的产生时间。
oplog last event time:最后一个事务日志的产生时间。
now:现在的时间值。
4.查看slave状态
db.printSlaveReplicationInfo()
5.插入,查询
use os
db.os.insert({name:"gentoo", type:"linux"})
db.os.insert({name:"ubuntu", type:"linux"})
PRIMARY
提示:
默认SECONDARY不可读,启用方法
在SECONDARY上执行
db.getMongo().setSlaveOk()
rs.slaveOk()
但每次进入mongo shell都要执行,可以在用户home目录下建个mongo shell环境变量
vi ~/.mongorc.js
增加一行rs.slaveOk();