MongoDB Replica Sets
簡介
MongoDB中的副本集(Replica Set)是一組維護相同數據集的mongod服務。副本集可提供冗余和高可用性,是所有生產部署的基礎。也可以說,副本集類似於有自動故障修復功能的主從集群。通俗的講就使用多台機器進行同一數據的異步同步,從而使多台機器擁有同一數據的多個副本,並且當主庫宕機時再不需要用戶干預的情況下自動切換其他備份服務器做主庫。而且還可以利用副本服務器做只讀服務器,實現讀寫分離,提高負載。
1 冗余和數據可用性
復制提供冗余並提高數據可用性。通過在不同數據庫服務器上提供多個數據副本,復制可提供一定級別的容錯功能,以防止丟失單個數據庫服務器。
在某些情況下,復制可以提供增加的讀取性能,因為客戶端可以將讀取操作發送到不同的服務上,在不同數據中心維護數據副本可以增加分布式應用程序的數據位置和可用性。
2 MongoDB中的復制
副本集是一組維護相同數據集的
mongod實例。副本集包含多個數據承載節點和可選的一個仲裁節點。在承載數據的節點中,一個且僅一個成員視為主節點,而其他節點被視為要(從)節點。主節點接收所有的操作。副本集只能有一個主要能夠確認具有{w:"most"}寫入關注的寫入;雖然在某些情況下,另一個
mongod實例可能暫時認為自己也是主要的。主要記錄其操作日志中的數據集的所有更改,即oplog。3 主從復制和副本集區別
主從集群和副本集最大的區別就是副本集沒有固定的“主節點”;整個集群會選出一個“主節點”,當其掛掉后,又在剩下的從節點中選中其他節點為“主節點”,副本集總有一個活躍點(主、primary)和一個或多個備份節點(從、secondary)
副本集的三個角色
副本集有兩種類型三種角色
兩種類型:
- 主節點(Primary)類型:數據操作的主要連接帶你,可讀寫。
- 次要(輔助、從)節點(Secondaries)類型:數據冗余備份節點,可以讀或選舉。
三種角色:
主要成員(Primary):主要接收所有寫操作。就是主節點。
副本成員(Replicate):從節點通過復制操作以維護相同的數據集,即備份數據,不可泄操作,但可以讀操作(但需要配置)。是默認的一種從節點類型。
仲裁者(Arbiter):不保留任何數據的副本,只具有投票選舉作用。當然也可以將仲裁服務器維護為副本集的一部分,即副本成員同事也可以是仲裁者。也是一種從節點類型。
搭建副本集集群
一主一副本一仲裁

這里在一台機器上面進行搭建,通過多端口方式實現
創建主節點
1.創建數據存儲和日志存放目錄
# mkdir -p /data/replica_sets/myrs_27017/log
# mkdir -p /data/replica_sets/myrs_27017/data/db
2.新建或修改配置文件
# vim /data/replica_sets/myrs_27017/mongod.conf
systemLog:
#日志輸出的目標指定為文件
destination: file
#日志存放目錄
path: "/data/replica_sets/myrs_27017/log/mongod.log"
#當mongos或mongod實例重新啟動時,mongos或mongod會將新條目附加到現有日志文件的末尾
logAppend: true
storage:
#mongod實例存儲其數據的目錄
dbPath: "/data/replica_sets/myrs_27017/data/db"
journal:
#啟用或禁用持久性日志已確保數據文件保持有效和可恢復
enabled: true
processManagement:
#啟用在后台運行mongos或mongod進程的守護進程模式
fork: true
#指定用於保存mongos或mongod進程的進程ID的文件位置
pidFilePath: "/data/replica_sets/myrs_27017/log/mongod.pid"
net:
#服務實例綁定IP
bindIp: 127.0.0.1,10.10.10.11
#服務實例綁定端口
port: 27017
replication:
#副本集的名稱
replSetName: myrs
3.啟動節點服務
# mongod -f /data/replica_sets/myrs_27017/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 23945
child process started successfully, parent exiting
創建副本節點
1.創建數據存儲和日志存放目錄
# mkdir -p /data/replica_sets/myrs_27018/log
# mkdir -p /data/replica_sets/myrs_27018/data/db
2.新建或修改配置文件
# vim /data/replica_sets/myrs_27018/mongod.conf
systemLog:
#日志輸出的目標指定為文件
destination: file
#日志存放目錄
path: "/data/replica_sets/myrs_27018/log/mongod.log"
#當mongos或mongod實例重新啟動時,mongos或mongod會將新條目附加到現有日志文件的末尾
logAppend: true
storage:
#mongod實例存儲其數據的目錄
dbPath: "/data/replica_sets/myrs_27018/data/db"
journal:
#啟用或禁用持久性日志已確保數據文件保持有效和可恢復
enabled: true
processManagement:
#啟用在后台運行mongos或mongod進程的守護進程模式
fork: true
#指定用於保存mongos或mongod進程的進程ID的文件位置
pidFilePath: "/data/replica_sets/myrs_27018/log/mongod.pid"
net:
#服務實例綁定IP
bindIp: 127.0.0.1,10.10.10.11
#服務實例綁定端口
port: 27018
replication:
#副本集的名稱
replSetName: myrs
3.啟動節點服務
# mongod -f /data/replica_sets/myrs_27018/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 24174
child process started successfully, parent exiting
創建仲裁節點
1.創建數據存儲和日志存放目錄
# mkdir -p /data/replica_sets/myrs_27019/log
# mkdir -p /data/replica_sets/myrs_27019/data/db
2.新建或修改配置文件
# vim /data/replica_sets/myrs_27019/mongod.conf
systemLog:
#日志輸出的目標指定為文件
destination: file
#日志存放目錄
path: "/data/replica_sets/myrs_27019/log/mongod.log"
#當mongos或mongod實例重新啟動時,mongos或mongod會將新條目附加到現有日志文件的末尾
logAppend: true
storage:
#mongod實例存儲其數據的目錄
dbPath: "/data/replica_sets/myrs_27019/data/db"
journal:
#啟用或禁用持久性日志已確保數據文件保持有效和可恢復
enabled: true
processManagement:
#啟用在后台運行mongos或mongod進程的守護進程模式
fork: true
#指定用於保存mongos或mongod進程的進程ID的文件位置
pidFilePath: "/data/replica_sets/myrs_27019/log/mongod.pid"
net:
#服務實例綁定IP
bindIp: 127.0.0.1,10.10.10.11
#服務實例綁定端口
port: 27019
replication:
#副本集的名稱
replSetName: myrs
3.啟動節點服務
# mongod -f /data/replica_sets/myrs_27019/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 24441
child process started successfully, parent exiting
初始化配置副本集
1.使用客戶端命令連接任意一個節點,初始化主節點(這里選擇27017)
# mongo --host=10.10.10.11 --port=27017
備注:連接上之后,很多命令無法使用,比如show dbs等,必須初始化副本集才行
2.初始化新的副本集(命令:rs.initiate())
> rs.initiate()
{
"info2" : "no configuration specified. Using a default configuration for the set",
"me" : "10.10.10.11:27017",
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1604913568, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1604913568, 1)
}
myrs:SECONDARY>
myrs:PRIMARY>
備注:
1)"ok"的值為1,說明創建成功。
2)命令行提示符發生變化,變成了一個從節點角色,此時默認不能讀寫。稍等片刻,回車,變成主節點。
查看副本集的配置
返回包含當前副本集配置的文檔。
語法:
rs.conf(configuration)備注:
rs.config()是該方法的別名。configuration:可選,如果沒有配置,則使用默認主節點配置。
示例:在27017上執行副本集中當前節點的默認節點配置
myrs:PRIMARY> rs.conf()
{
"_id" : "myrs",
"version" : 1,
"term" : 1,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"members" : [
{
"_id" : 0,
"host" : "10.10.10.11:27017",
"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("5fa909a07e34d2b6fa2e54a1")
}
}
說明:
1)"_id" : "myrs":副本集的配置數據存儲的主鍵值,默認就是副本集的名字
2)"members":副本集成員數組,此時只有一個:"host" : "10.10.10.11:27017",該成員不是仲裁節點:"arbiterOnly" : false,優先級(權重值):"priority" : 1,
3)"settings":副本集的參數配置
提示:副本集配置的查看命令,本質是查詢的是system.replset的表中的數據:
myrs:PRIMARY> use local
switched to db local
myrs:PRIMARY> show collections
oplog.rs
replset.election
replset.initialSyncId
replset.minvalid
replset.oplogTruncateAfterPoint
startup_log
system.replset
system.rollback.id
myrs:PRIMARY> db.system.replset.find()
{ "_id" : "myrs", "version" : 1, "term" : 1, "protocolVersion" : NumberLong(1), "writeConcernMajorityJournalDefault" : true, "members" : [ { "_id" : 0, "host" : "10.10.10.11:27017", "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("5fa909a07e34d2b6fa2e54a1") } }
查看副本集的狀態
說明:返回包含狀態信息的文檔,次輸出結果從副本集的其他成員發送的心跳包中獲得的數據反映副本集的當前狀態
語法:
rs.status()
在27017上查看副本集狀態
myrs:PRIMARY> rs.status()
{
"set" : "myrs",
"date" : ISODate("2020-11-10T01:15:18.661Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 1,
"writeMajorityCount" : 1,
"votingMembersCount" : 1,
"writableVotingMembersCount" : 1,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1604970915, 1),
"t" : NumberLong(1)
},
"lastCommittedWallTime" : ISODate("2020-11-10T01:15:15.402Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1604970915, 1),
"t" : NumberLong(1)
},
"readConcernMajorityWallTime" : ISODate("2020-11-10T01:15:15.402Z"),
"appliedOpTime" : {
"ts" : Timestamp(1604970915, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1604970915, 1),
"t" : NumberLong(1)
},
"lastAppliedWallTime" : ISODate("2020-11-10T01:15:15.402Z"),
"lastDurableWallTime" : ISODate("2020-11-10T01:15:15.402Z")
},
"lastStableRecoveryTimestamp" : Timestamp(1604970895, 1),
"electionCandidateMetrics" : {
"lastElectionReason" : "electionTimeout",
"lastElectionDate" : ISODate("2020-11-09T09:19:28.377Z"),
"electionTerm" : NumberLong(1),
"lastCommittedOpTimeAtElection" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"lastSeenOpTimeAtElection" : {
"ts" : Timestamp(1604913568, 1),
"t" : NumberLong(-1)
},
"numVotesNeeded" : 1,
"priorityAtElection" : 1,
"electionTimeoutMillis" : NumberLong(10000),
"newTermStartDate" : ISODate("2020-11-09T09:19:28.401Z"),
"wMajorityWriteAvailabilityDate" : ISODate("2020-11-09T09:19:28.420Z")
},
"members" : [
{
"_id" : 0,
"name" : "10.10.10.11:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 57734,
"optime" : {
"ts" : Timestamp(1604970915, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-11-10T01:15:15Z"),
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1604913568, 2),
"electionDate" : ISODate("2020-11-09T09:19:28Z"),
"configVersion" : 1,
"configTerm" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
}
],
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1604970915, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1604970915, 1)
}
說明:
1)"set" : "myrs":副本集的名字
2)"myState" : 1:說明狀態正常
3)"members":副本集成員數組,此時只有一個:"name" : "10.10.10.11:27017",該成員的角色是"stateStr" : "PRIMARY",該節點是健康的:"health" : 1。
添加副本從節點
在主節點添加從節點,將其他成員加入到副本集
語法:
rs.add(host, arbiterOnly)
| Parameter | Type | Description |
|---|---|---|
host |
string or document | 要添加到副本集的新成員。指定為字符串或配置文檔:1)如果是一個字符串,則需要指定新成員的主機名和可選的端口;2)如果是一個文檔,需要指定在members數組中找到的副本集成員配置文檔。必須在成員配置文檔中指定主機字段。詳見下方文檔:“主機成員的配置文檔” |
arbiterOnly |
boolean | 可選的。僅在
|
主機成員的配置文檔:
{
_id: <int>,
host: <string>, // required
arbiterOnly: <boolean>,
buildIndexes: <boolean>,
hidden: <boolean>,
priority: <number>,
tags: <document>,
slaveDelay: <int>,
votes: <number>
}
將27018的副本節點添加到副本集中:
myrs:PRIMARY> rs.add("10.10.10.11:27018")
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1604975516, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1604975516, 1)
}
說明:
1)"ok" : 1:說明添加成功。
查看副本集狀態
myrs:PRIMARY> rs.status()
{
"set" : "myrs",
"date" : ISODate("2020-11-10T02:33:54.698Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 2,
"writeMajorityCount" : 2,
"votingMembersCount" : 2,
"writableVotingMembersCount" : 2,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1604975625, 1),
"t" : NumberLong(1)
},
"lastCommittedWallTime" : ISODate("2020-11-10T02:33:45.868Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1604975625, 1),
"t" : NumberLong(1)
},
"readConcernMajorityWallTime" : ISODate("2020-11-10T02:33:45.868Z"),
"appliedOpTime" : {
"ts" : Timestamp(1604975625, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1604975625, 1),
"t" : NumberLong(1)
},
"lastAppliedWallTime" : ISODate("2020-11-10T02:33:45.868Z"),
"lastDurableWallTime" : ISODate("2020-11-10T02:33:45.868Z")
},
"lastStableRecoveryTimestamp" : Timestamp(1604975575, 1),
"electionCandidateMetrics" : {
"lastElectionReason" : "electionTimeout",
"lastElectionDate" : ISODate("2020-11-09T09:19:28.377Z"),
"electionTerm" : NumberLong(1),
"lastCommittedOpTimeAtElection" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"lastSeenOpTimeAtElection" : {
"ts" : Timestamp(1604913568, 1),
"t" : NumberLong(-1)
},
"numVotesNeeded" : 1,
"priorityAtElection" : 1,
"electionTimeoutMillis" : NumberLong(10000),
"newTermStartDate" : ISODate("2020-11-09T09:19:28.401Z"),
"wMajorityWriteAvailabilityDate" : ISODate("2020-11-09T09:19:28.420Z")
},
"members" : [
{
"_id" : 0,
"name" : "10.10.10.11:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 62450,
"optime" : {
"ts" : Timestamp(1604975625, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-11-10T02:33:45Z"),
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1604913568, 2),
"electionDate" : ISODate("2020-11-09T09:19:28Z"),
"configVersion" : 2,
"configTerm" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "10.10.10.11:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 117,
"optime" : {
"ts" : Timestamp(1604975625, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1604975625, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-11-10T02:33:45Z"),
"optimeDurableDate" : ISODate("2020-11-10T02:33:45Z"),
"lastHeartbeat" : ISODate("2020-11-10T02:33:52.778Z"),
"lastHeartbeatRecv" : ISODate("2020-11-10T02:33:53.089Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncSourceHost" : "10.10.10.11:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 2,
"configTerm" : 1
}
],
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1604975625, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1604975625, 1)
}
說明:
1)"name" : "10.10.10.11:27018"是第二個節點的名字,其角色是"stateStr" : "SECONDARY"
添加仲裁節點
在主節點添加一個仲裁節點到副本集
語法:
rs.addArb(host)
將27019節點添加到副本集中作為仲裁節點:
myrs:PRIMARY> rs.addArb("10.10.10.11:27019")
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1604989664, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1604989664, 1)
}
說明:
1)"ok" : 1:說明添加成功
查看副本集狀態:
myrs:PRIMARY> rs.status()
{
"set" : "myrs",
"date" : ISODate("2020-11-10T06:28:31.102Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 2,
"writeMajorityCount" : 2,
"votingMembersCount" : 3,
"writableVotingMembersCount" : 2,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1604989707, 1),
"t" : NumberLong(1)
},
"lastCommittedWallTime" : ISODate("2020-11-10T06:28:27.292Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1604989707, 1),
"t" : NumberLong(1)
},
"readConcernMajorityWallTime" : ISODate("2020-11-10T06:28:27.292Z"),
"appliedOpTime" : {
"ts" : Timestamp(1604989707, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1604989707, 1),
"t" : NumberLong(1)
},
"lastAppliedWallTime" : ISODate("2020-11-10T06:28:27.292Z"),
"lastDurableWallTime" : ISODate("2020-11-10T06:28:27.292Z")
},
"lastStableRecoveryTimestamp" : Timestamp(1604989675, 1),
"electionCandidateMetrics" : {
"lastElectionReason" : "electionTimeout",
"lastElectionDate" : ISODate("2020-11-09T09:19:28.377Z"),
"electionTerm" : NumberLong(1),
"lastCommittedOpTimeAtElection" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"lastSeenOpTimeAtElection" : {
"ts" : Timestamp(1604913568, 1),
"t" : NumberLong(-1)
},
"numVotesNeeded" : 1,
"priorityAtElection" : 1,
"electionTimeoutMillis" : NumberLong(10000),
"newTermStartDate" : ISODate("2020-11-09T09:19:28.401Z"),
"wMajorityWriteAvailabilityDate" : ISODate("2020-11-09T09:19:28.420Z")
},
"members" : [
{
"_id" : 0,
"name" : "10.10.10.11:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 76527,
"optime" : {
"ts" : Timestamp(1604989707, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-11-10T06:28:27Z"),
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1604913568, 2),
"electionDate" : ISODate("2020-11-09T09:19:28Z"),
"configVersion" : 3,
"configTerm" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "10.10.10.11:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 14194,
"optime" : {
"ts" : Timestamp(1604989707, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1604989707, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-11-10T06:28:27Z"),
"optimeDurableDate" : ISODate("2020-11-10T06:28:27Z"),
"lastHeartbeat" : ISODate("2020-11-10T06:28:30.884Z"),
"lastHeartbeatRecv" : ISODate("2020-11-10T06:28:30.897Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncSourceHost" : "10.10.10.11:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 3,
"configTerm" : 1
},
{
"_id" : 2,
"name" : "10.10.10.11:27019",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 46,
"lastHeartbeat" : ISODate("2020-11-10T06:28:30.900Z"),
"lastHeartbeatRecv" : ISODate("2020-11-10T06:28:30.917Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 3,
"configTerm" : 1
}
],
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1604989707, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1604989707, 1)
}
說明:
1)"name" : "10.10.10.11:27019"是第二個節點的名字,其角色是"stateStr" : "ARBITER"
副本集的數據讀寫操作
測試三個不同角色的節點的數據讀寫情況。
1、測試主節點27017,寫入和讀數據:
# mongo --host=10.10.10.11 --port=27017
myrs:PRIMARY> use collectest
switched to db collectest
myrs:PRIMARY> db
collectest
myrs:PRIMARY> db.collectest.insert({"name":"張三","sex":"男","age":22,"userid":1001,"createdatetime":new Date()})
WriteResult({ "nInserted" : 1 })
myrs:PRIMARY>
myrs:PRIMARY> db.collectest.find()
{ "_id" : ObjectId("5faa3432f6e79c62c00e4d72"), "name" : "張三", "sex" : "男", "age" : 22, "userid" : 1001, "createdatetime" : ISODate("2020-11-10T06:33:22.459Z") }
myrs:PRIMARY>
說明:通過測試可以發現主節點是可以進行數據的讀和寫操作的。
2、測試從節點
# mongo --host=10.10.10.11 --port=27018
myrs:SECONDARY> show dbs
uncaught exception: Error: listDatabases failed:{
"topologyVersion" : {
"processId" : ObjectId("5fa909804c7cb6020458ce77"),
"counter" : NumberLong(5)
},
"operationTime" : Timestamp(1605085202, 1),
"ok" : 0,
"errmsg" : "not master and slaveOk=false",
"code" : 13435,
"codeName" : "NotMasterNoSlaveOk",
"$clusterTime" : {
"clusterTime" : Timestamp(1605085202, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs/<@src/mongo/shell/mongo.js:147:19
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:99:12
shellHelper.show@src/mongo/shell/utils.js:937:13
shellHelper@src/mongo/shell/utils.js:819:15
@(shellhelp2):1:1
測試發現,不能讀取集合的數據。當前從節點只是一個備份,不是奴隸節點,無法讀取數據,寫當然更不行。因為默認情況下,從節點是沒有讀寫權限的,可以增加讀的權限,但是需要進行設置。
設置讀操作權限:
rs.secondaryOk()
#或者
rs.secondaryOk(true)
示例,在27018上面設置從節點具備讀權限:
myrs:SECONDARY> rs.secondaryOk()
myrs:SECONDARY> show dbs
admin 0.000GB
collectest 0.000GB
config 0.000GB
local 0.000GB
myrs:SECONDARY> use collectest
switched to db collectest
myrs:SECONDARY> show collections
collectest
myrs:SECONDARY>
myrs:SECONDARY> db.collectest.find()
{ "_id" : ObjectId("5faa3432f6e79c62c00e4d72"), "name" : "張三", "sex" : "男", "age" : 22, "userid" : 1001, "createdatetime" : ISODate("2020-11-10T06:33:22.459Z") }
這里便實現了讀寫分離,讓主節點插入數據,讓從來讀取數據。
如果要取消從節點的讀權限:
myrs:SECONDARY> rs.secondaryOk(false)
myrs:SECONDARY> show dbs
uncaught exception: Error: listDatabases failed:{
"topologyVersion" : {
"processId" : ObjectId("5fa909804c7cb6020458ce77"),
"counter" : NumberLong(5)
},
"operationTime" : Timestamp(1605086322, 1),
"ok" : 0,
"errmsg" : "not master and slaveOk=false",
"code" : 13435,
"codeName" : "NotMasterNoSlaveOk",
"$clusterTime" : {
"clusterTime" : Timestamp(1605086322, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs/<@src/mongo/shell/mongo.js:147:19
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:99:12
shellHelper.show@src/mongo/shell/utils.js:937:13
shellHelper@src/mongo/shell/utils.js:819:15
@(shellhelp2):1:1
3、測試仲裁節點,仲裁節點不會放任何業務數據,可以登陸查看
# mongo --host=10.10.10.11 --port=27019
myrs:ARBITER> show dbs
uncaught exception: Error: listDatabases failed:{
"topologyVersion" : {
"processId" : ObjectId("5fa90b59bea51b269ba3eb82"),
"counter" : NumberLong(3)
},
"ok" : 0,
"errmsg" : "node is not in primary or recovering state",
"code" : 13436,
"codeName" : "NotMasterOrSecondary"
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs/<@src/mongo/shell/mongo.js:147:19
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:99:12
shellHelper.show@src/mongo/shell/utils.js:937:13
shellHelper@src/mongo/shell/utils.js:819:15
@(shellhelp2):1:1
測試發現,仲裁節點不能進行數據的查看。
主節點的選舉原則
MongoDB在副本集中,會自動進行主節點的選舉,主節點選舉的觸發條件:1)主節點故障
2)主節點網絡不可達(默認心跳信息為10秒)
3)人工干預(
rs.stepDown(600))一旦觸發選舉,就要根據一定規則來選主節點。
選舉規則是根據票數來決定誰獲勝:
- 票數最高,且獲得了“大多數”成員的投票支持的節點獲勝
- 若票數相同,且都獲得了“大多數”成員的投票支持的,數據新的節點獲勝。數據的新舊是通過操作日志
oplog來對比的。在獲得票數的時候,優先級(
priority)參數影響重大。可以通過設置優先級(priority)來設置額外票數。優先級即權重,取值為0-1000,相當於可額外增加0-1000的票數,優先級的值越大,就越有可能獲得多數成員的投票(votes)數。指定較高的值可使成員更有資格成為主要成員,更低的值可使成員更不符合條件。
默認情況下,優先級都為1
myrs:SECONDARY> rs.conf()
{
"_id" : "myrs",
"version" : 3,
"term" : 3,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"members" : [
{
"_id" : 0,
"host" : "10.10.10.11:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "10.10.10.11:27018",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "10.10.10.11:27019",
"arbiterOnly" : true,
"buildIndexes" : true,
"hidden" : false,
"priority" : 0,
"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("5fa909a07e34d2b6fa2e54a1")
}
}
可以通過上面的配置信息看出,主節點和副本節點的優先級各位1,即,默認可以認為都已經有了一票。單選舉節點,優先級是0,(注意:官方說了,選舉節點的優先級必須是0,不能是別的值。即不具備選舉權,單具有投票權)
故障測試
副本節點故障測試
關閉27018副本節點:
# ps -ef |grep mongo
root 23945 1 0 Nov10 ? 00:22:10 mongod -f /data/replica_sets/myrs_27017/mongod.conf
root 24174 1 0 Nov10 ? 00:22:33 mongod -f /data/replica_sets/myrs_27018/mongod.conf
root 24441 1 0 Nov10 ? 00:15:15 mongod -f /data/replica_sets/myrs_27019/mongod.conf
# kill -2 24174
進入主節點27017寫入數據
# mongo --host=10.10.10.11 --port=27017
---
myrs:PRIMARY> use collectest
switched to db collectest
myrs:PRIMARY> db.collectest.insert({"name":"小白", "sex":"女", "age":20, "userid":1002, "createdatetime":new Date()})
WriteResult({ "nInserted" : 1 })
啟動27018副本節點,並驗證數據
# mongod -f /data/replica_sets/myrs_27018/mongod.conf
# mongo --host=10.10.10.11 --port=27018
myrs:SECONDARY> rs.secondaryOk()
myrs:SECONDARY> show dbs
admin 0.000GB
collectest 0.000GB
config 0.000GB
local 0.001GB
myrs:SECONDARY> use collectest
switched to db collectest
myrs:SECONDARY> db.collectest.find()
{ "_id" : ObjectId("5faa3432f6e79c62c00e4d72"), "name" : "張三", "sex" : "男", "age" : 22, "userid" : 1001, "createdatetime" : ISODate("2020-11-10T06:33:22.459Z") }
{ "_id" : ObjectId("5facec10cca53c48154d261c"), "name" : "小白", "sex" : "女", "age" : 20, "userid" : 1002, "createdatetime" : ISODate("2020-11-12T08:02:24.915Z") }
通過上面的測試驗證,可以發現,當副本節點故障時,主節點讀寫不受影響,並且在副本節點恢復后,主節點寫入的數據,會自動同步給從節點。
主節點故障測試
關閉主節點27017
# ps -ef |grep mongo
root 23945 1 0 Nov10 ? 00:22:34 mongod -f /data/replica_sets/myrs_27017/mongod.conf
root 24441 1 0 Nov10 ? 00:15:27 mongod -f /data/replica_sets/myrs_27019/mongod.conf
root 50521 1 1 16:04 ? 00:00:16 mongod -f /data/replica_sets/myrs_27018/mongod.conf
# kill -2 23945
登陸副本節點查看集群狀態
# mongo --host=10.10.10.11 --port=27018
myrs:PRIMARY>
myrs:PRIMARY> rs.status()
{
"set" : "myrs",
"date" : ISODate("2020-11-12T08:30:11.791Z"),
"myState" : 1,
"term" : NumberLong(7),
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 2,
"writeMajorityCount" : 2,
"votingMembersCount" : 3,
"writableVotingMembersCount" : 2,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1605169711, 1),
"t" : NumberLong(5)
},
"lastCommittedWallTime" : ISODate("2020-11-12T08:28:31.364Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1605169711, 1),
"t" : NumberLong(5)
},
"readConcernMajorityWallTime" : ISODate("2020-11-12T08:28:31.364Z"),
"appliedOpTime" : {
"ts" : Timestamp(1605169802, 1),
"t" : NumberLong(7)
},
"durableOpTime" : {
"ts" : Timestamp(1605169802, 1),
"t" : NumberLong(7)
},
"lastAppliedWallTime" : ISODate("2020-11-12T08:30:02.141Z"),
"lastDurableWallTime" : ISODate("2020-11-12T08:30:02.141Z")
},
"lastStableRecoveryTimestamp" : Timestamp(1605169711, 1),
"electionCandidateMetrics" : {
"lastElectionReason" : "electionTimeout",
"lastElectionDate" : ISODate("2020-11-12T08:28:42.106Z"),
"electionTerm" : NumberLong(7),
"lastCommittedOpTimeAtElection" : {
"ts" : Timestamp(1605169711, 1),
"t" : NumberLong(5)
},
"lastSeenOpTimeAtElection" : {
"ts" : Timestamp(1605169711, 1),
"t" : NumberLong(5)
},
"numVotesNeeded" : 2,
"priorityAtElection" : 1,
"electionTimeoutMillis" : NumberLong(10000),
"numCatchUpOps" : NumberLong(0),
"newTermStartDate" : ISODate("2020-11-12T08:28:42.125Z")
},
"members" : [
{
"_id" : 0,
"name" : "10.10.10.11:27017",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDurable" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2020-11-12T08:30:10.207Z"),
"lastHeartbeatRecv" : ISODate("2020-11-12T08:28:31.380Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "Error connecting to 10.10.10.11:27017 :: caused by :: Connection refused",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 3,
"configTerm" : 5
},
{
"_id" : 1,
"name" : "10.10.10.11:27018",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1537,
"optime" : {
"ts" : Timestamp(1605169802, 1),
"t" : NumberLong(7)
},
"optimeDate" : ISODate("2020-11-12T08:30:02Z"),
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1605169722, 1),
"electionDate" : ISODate("2020-11-12T08:28:42Z"),
"configVersion" : 3,
"configTerm" : 7,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 2,
"name" : "10.10.10.11:27019",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 1534,
"lastHeartbeat" : ISODate("2020-11-12T08:30:10.162Z"),
"lastHeartbeatRecv" : ISODate("2020-11-12T08:30:10.165Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 3,
"configTerm" : 7
}
],
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1605169802, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1605169802, 1)
}
通過上面查看之前的27018副本節點已經變為了主節點,接下來在副本節點中插入數據
myrs:PRIMARY> use collectest
switched to db collectest
myrs:PRIMARY> db.collectest.insert({"name":"小嘿", "sex":"女", "age":21, "userid":1002, "createdatetime":new Date()})
WriteResult({ "nInserted" : 1 })
此時,重新啟動27017節點,並查看狀態
# mongod -f /data/replica_sets/myrs_27017/mongod.conf
# mongo --host=10.10.10.11 --port=27017
myrs:SECONDARY>
myrs:SECONDARY> rs.secondaryOk()
myrs:SECONDARY> use collectest
switched to db collectest
myrs:SECONDARY> db.collectest.find()
{ "_id" : ObjectId("5faa3432f6e79c62c00e4d72"), "name" : "張三", "sex" : "男", "age" : 22, "userid" : 1001, "createdatetime" : ISODate("2020-11-10T06:33:22.459Z") }
{ "_id" : ObjectId("5facec10cca53c48154d261c"), "name" : "小白", "sex" : "女", "age" : 20, "userid" : 1002, "createdatetime" : ISODate("2020-11-12T08:02:24.915Z") }
{ "_id" : ObjectId("5facf32dfb5fe16aaf699d7d"), "name" : "小嘿", "sex" : "女", "age" : 21, "userid" : 1002, "createdatetime" : ISODate("2020-11-12T08:32:45.919Z") }
通過上面測試發現,此時27017節點已經變成了副本節點,並且配置查看權限后,可以對數據進行查看操作。
通過上面的測試,主從自動切換,故而試下了高可用。
仲裁節點和主節點故障
先關掉仲裁節點27019
關掉現在的主節點27018
登陸27017后,發現,27017仍然是從節點,副本集中沒有主節點了,導致此時,副本集只是讀狀態,無法寫入。
為啥不選舉了?因為27017的票數,沒有獲得大多數,即沒有大於等於2,它只有默認的一票(優先級是1)
如果要觸發選舉,隨便加入一個成員即可。
-
如果只加入27019仲裁節點成員,則主節點一定是27017,因為沒得選了,仲裁節點不參與選舉,但參與投票
-
如果只加入27018節點,會發起選舉。因為27017和27018都是兩票,則按照誰數據新,誰當主節點。

