原理:
副本集羣每2秒發一次心跳,若是10秒事後沒收到則標記爲不可達。
MongoDB在主數據庫上應用數據庫操做,而後在主數據庫的操做日誌中記錄該操做。而後,輔助成員將這些操做複製並應用到異步過程當中。全部副本集成員在local.oplog.rs集合中都包含操做日誌的副本 ,默認大小爲磁盤的5%,但不記錄查詢操做。oplogSize能夠在/etc/mongdb.conf中指定大小數據庫
replication:
oplogSizeMB: <int>
replSetName: <string>
secondaryIndexPrefetch: <string>
enableMajorityReadConcern: <boolean>異步
mongdb副本集中的成員:
primary: 負責寫入數據
seconary: 負責同步主上的數據,提供讀服務
仲裁節點: 不保有數據,不參與選主,只進行選主投票ide
副本集選舉經過優先級,範圍0-1000,0爲仲裁節點,不參與選舉。默認是1fetch
如何設置?ui
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") }
}code
修改id值爲2的分片優先級爲3,當primary,則id值爲2的成爲prmary
config.members[2].priority=3進程
故意停掉27020進程
kill -9 82609get
查看id值爲2的主機是否成爲primary
進入27022的mongdb:同步
MongoDB Enterprise shard-rs:PRIMARY> use adminswitched to db adminMongoDB Enterprise shard-rs:PRIMARY> db.isMaster(){"hosts" : ["worker2:27020","worker2:27021","worker2:27022"],"setName" : "shard-rs","setVersion" : 4,"ismaster" : true, # 成爲primary"secondary" : false,