堡壘機
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都是兩票,則按照誰資料新,誰當主節點。