1.原理:
副本集群每2秒发一次心跳,如果10秒过后没收到则标记为不可达。
MongoDB在主数据库上应用数据库操作,然后在主数据库的操作日志中记录该操作。然后,辅助成员将这些操作复制并应用到异步过程中。所有副本集成员在local.oplog.rs集合中都包含操作日志的副本 ,默认大小为磁盘的5%,但不记录查询操作。oplogSize可以在/etc/mongdb.conf中指定大小

replication:
   oplogSizeMB: 
   replSetName: 
   secondaryIndexPrefetch: 
   enableMajorityReadConcern: 

2.mongdb副本集中的成员角色:
primary: 负责写入数据
seconary: 负责同步主上的数据,提供读服务
仲裁节点: 不保有数据,不参与选主,只进行选主投票

副本集选举通过优先级,范围0-1000,0为仲裁节点,不参与选举。默认是1

3.如何设置?

MongoDB Enterprise shard-rs:PRIMARY> config=rs.conf()
MongoDB Enterprise shard-rs:PRIMARY> config
{
        "_id" : "shard-rs",
        "version" : 3,
        "protocolVersion" : NumberLong(1),
        "writeConcernMajorityJournalDefault" : true,
        "members" : [
                {
                        "_id" : 0,
                        "host" : "worker2:27020",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 1,
                        "host" : "worker2:27021",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 2,
                        "host" : "worker2:27022",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                }
        ],
        "settings" : {
                "chainingAllowed" : true,
                "heartbeatIntervalMillis" : 2000,
                "heartbeatTimeoutSecs" : 10,
                "electionTimeoutMillis" : 10000,
                "catchUpTimeoutMillis" : -1,
                "catchUpTakeoverDelayMillis" : 30000,
                "getLastErrorModes" : {

                },
                "getLastErrorDefaults" : {
                        "w" : 1,
                        "wtimeout" : 0
                },
                "replicaSetId" : ObjectId("5dd8bf958b8ea486b84c23b2")
        }
}

修改id值为2的分片优先级为3,当primary,则id值为2的成为prmary
config.members[2].priority=3

故意停掉27020进程
kill -9 82609

查看id值为2的主机是否成为primary
进入27022的mongdb:

MongoDB Enterprise shard-rs:PRIMARY> use admin
switched to db admin
MongoDB Enterprise shard-rs:PRIMARY> db.isMaster()
{
        "hosts" : [
                "worker2:27020",
                "worker2:27021",
                "worker2:27022"
        ],
        "setName" : "shard-rs",
        "setVersion" : 4,
        "ismaster" : true,   # 成为primary
        "secondary" : false,