MongoDB Replication 副本集搭建

版本:針對(duì)MongoDB 5.0

官方文檔:https://docs.mongodb.com/manual/tutorial/deploy-replica-set/#overview

第一步:準(zhǔn)備三臺(tái)服務(wù)器 (副本集應(yīng)該總是有奇數(shù)個(gè)成員。確保選舉的順利進(jìn)行)

192.168.223.133 PRIMARY
192.168.223.134 SECONDARY
192.168.223.135 SECONDARY

# 分別修改主機(jī)名
192.168.223.133 hostnamectl set-hostname mongodb01.node
192.168.223.134 hostnamectl set-hostname mongodb02.node
192.168.223.135 hostnamectl set-hostname mongodb03.node

# 分別配置/etc/hosts
vim /etc/hosts
    ## 在hosts文件中增加下面的配置
    192.168.223.133 mongodb01.node
    192.168.223.134 mongodb02.node
    192.168.223.135 mongodb03.node

第二步:安裝MongoDB,增加副本集參數(shù)配置

# 安裝mongodb
sudo yum install -y mongodb-org

# 設(shè)置防火墻
firewall-cmd --zone=public --add-port=27017/tcp --permanent
firewall-cmd --reload

# 修改配置文件/etc/mongod.conf 
# mongod.conf
...
# 修改bindIp
net:
  port: 27017
  bindIp: localhost,mongodb01.node # 綁定本機(jī)的IP或者主機(jī)名

# 副本集配置
replication:
  oplogSizeMB: 102 # 對(duì)于64位系統(tǒng),oplog通常是可用磁盤空間的5%。
  replSetName: rs0
   
# 啟動(dòng)/重新啟動(dòng)mongodb 
systemctl start mongod
systemctl restart mongod

第三步:副本集初始化以及配置

# 隨便連接一個(gè)mongodb實(shí)例
mongo mongodb://mongodb01.node:27017/test

# 使用rs.help()可以查看副本集所有的操作方法
> rs.help()

# 初始化副本集 此處需要注意第一個(gè)"_id"表示副本集的名稱,就是配置文件中配置的”replSetName“
> rs.initiate({_id:'rs0',members:[
    {_id:1,host:'mongodb01.node:27017'},
    {_id:3,host:'mongodb02.node:27017'},
    {_id:2,host:'mongodb03.node:27017'}
]})

# 當(dāng)然也可以選擇先配置一個(gè)節(jié)點(diǎn) 然后加入使用rs.add()加入其他節(jié)點(diǎn), 如下:
> rs.initiate({_id:'rs0',members:[{_id:1,host:'mongodb01.node:27017'}]})
rs0:PRIMARY> rs.add('mongodb02.node:27017')
rs0:PRIMARY> rs.add('mongodb03.node:27017')

# 初始化以后會(huì)看到客戶端shell變成了”rs0:PRIMARY“

# 使用rs.status()查看副本集群的狀態(tài) 
# mongodb01.node:27017狀態(tài): "PRIMARY"
# mongodb02.node:27017和mongodb03.node:27017狀態(tài): "SECONDARY"
rs0:SECONDARY> rs.status()
{
        "set" : "rs0",
        "date" : ISODate("2021-12-15T07:33:26.513Z"),
        "myState" : 1,
        "term" : NumberLong(2),
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "majorityVoteCount" : 2,
        "writeMajorityCount" : 2,
        "votingMembersCount" : 3,
        "writableVotingMembersCount" : 3,
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1639553603, 1),
                        "t" : NumberLong(2)
                },
                "lastCommittedWallTime" : ISODate("2021-12-15T07:33:23.687Z"),
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1639553603, 1),
                        "t" : NumberLong(2)
                },
                "appliedOpTime" : {
                        "ts" : Timestamp(1639553603, 1),
                        "t" : NumberLong(2)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1639553603, 1),
                        "t" : NumberLong(2)
                },
                "lastAppliedWallTime" : ISODate("2021-12-15T07:33:23.687Z"),
                "lastDurableWallTime" : ISODate("2021-12-15T07:33:23.687Z")
        },
        "lastStableRecoveryTimestamp" : Timestamp(1639553549, 1),
        "electionCandidateMetrics" : {
                "lastElectionReason" : "stepUpRequestSkipDryRun",
                "lastElectionDate" : ISODate("2021-12-15T07:33:03.635Z"),
                "electionTerm" : NumberLong(2),
                "lastCommittedOpTimeAtElection" : {
                        "ts" : Timestamp(1639553575, 1),
                        "t" : NumberLong(1)
                },
                "lastSeenOpTimeAtElection" : {
                        "ts" : Timestamp(1639553575, 1),
                        "t" : NumberLong(1)
                },
                "numVotesNeeded" : 2,
                "priorityAtElection" : 1,
                "electionTimeoutMillis" : NumberLong(10000),
                "priorPrimaryMemberId" : 1,
                "numCatchUpOps" : NumberLong(0),
                "newTermStartDate" : ISODate("2021-12-15T07:33:03.671Z"),
                "wMajorityWriteAvailabilityDate" : ISODate("2021-12-15T07:33:03.699Z")
        },
        "electionParticipantMetrics" : {
                "votedForCandidate" : true,
                "electionTerm" : NumberLong(1),
                "lastVoteDate" : ISODate("2021-12-15T07:26:29.091Z"),
                "electionCandidateMemberId" : 1,
                "voteReason" : "",
                "lastAppliedOpTimeAtElection" : {
                        "ts" : Timestamp(1639553178, 1),
                        "t" : NumberLong(-1)
                },
                "maxAppliedOpTimeInSet" : {
                        "ts" : Timestamp(1639553178, 1),
                        "t" : NumberLong(-1)
                },
                "priorityAtElection" : 1
        },
        "members" : [
                {
                        "_id" : 1,
                        "name" : "mongodb01.node: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"),
                        "lastAppliedWallTime" : ISODate("2021-12-15T07:33:13.685Z"),
                        "lastDurableWallTime" : ISODate("2021-12-15T07:33:13.685Z"),
                        "lastHeartbeat" : ISODate("2021-12-15T07:33:25.726Z"),
                        "lastHeartbeatRecv" : ISODate("2021-12-15T07:33:18.704Z"),
                        "pingMs" : NumberLong(1),
                        "lastHeartbeatMessage" : "Error connecting to mongodb01.node:27017 (192.168.223.133:27017) :: caused by :: Connection refused",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "configVersion" : 1,
                        "configTerm" : 2
                },
                {
                        "_id" : 2,
                        "name" : "mongodb03.node:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 428,
                        "optime" : {
                                "ts" : Timestamp(1639553603, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1639553603, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDate" : ISODate("2021-12-15T07:33:23Z"),
                        "optimeDurableDate" : ISODate("2021-12-15T07:33:23Z"),
                        "lastAppliedWallTime" : ISODate("2021-12-15T07:33:23.687Z"),
                        "lastDurableWallTime" : ISODate("2021-12-15T07:33:23.687Z"),
                        "lastHeartbeat" : ISODate("2021-12-15T07:33:25.715Z"),
                        "lastHeartbeatRecv" : ISODate("2021-12-15T07:33:25.714Z"),
                        "pingMs" : NumberLong(1),
                        "lastHeartbeatMessage" : "",
                        "syncSourceHost" : "mongodb02.node:27017",
                        "syncSourceId" : 3,
                        "infoMessage" : "",
                        "configVersion" : 1,
                        "configTerm" : 2
                },
                {
                        "_id" : 3,
                        "name" : "mongodb02.node:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 632,
                        "optime" : {
                                "ts" : Timestamp(1639553603, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDate" : ISODate("2021-12-15T07:33:23Z"),
                        "lastAppliedWallTime" : ISODate("2021-12-15T07:33:23.687Z"),
                        "lastDurableWallTime" : ISODate("2021-12-15T07:33:23.687Z"),
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "electionTime" : Timestamp(1639553583, 1),
                        "electionDate" : ISODate("2021-12-15T07:33:03Z"),
                        "configVersion" : 1,
                        "configTerm" : 2,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                }
        ],
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1639553603, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1639553603, 1)
}

第四步:測(cè)試數(shù)據(jù)同步

## 主節(jié)點(diǎn)操作:
# 新建并切換數(shù)據(jù)庫(kù) my
rs0:PRIMARY> use my
# 新建集合并插入數(shù)據(jù) 
rs0:PRIMARY> db.col00.insert({name:"user01"})

## 從節(jié)點(diǎn)操作
# 從節(jié)點(diǎn)默認(rèn)不能查詢操作 需要執(zhí)行rs.secondaryOk()允許操作
rs0:SECONDARY> rs.secondaryOk()
# 查詢主節(jié)點(diǎn)創(chuàng)建的數(shù)據(jù) 如果可以查到說(shuō)明搭建成功
rs0:SECONDARY> db.col00.find()

第四步:測(cè)試風(fēng)險(xiǎn)轉(zhuǎn)移

## 將主節(jié)點(diǎn)mongodb01.node:27017實(shí)例關(guān)閉
systeml stop mongod

# 連接從節(jié)點(diǎn)mongodb02.node:27017查看集群狀態(tài) 發(fā)現(xiàn)mongodb02.node:27017成為了主節(jié)點(diǎn)
rs0:PRIMARY> rs.status()
{
        "set" : "rs0",
        "date" : ISODate("2021-12-13T07:05:56.087Z"),
        "myState" : 1,
        "term" : NumberLong(2),
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "majorityVoteCount" : 2,
        "writeMajorityCount" : 2,
        "votingMembersCount" : 3,
        "writableVotingMembersCount" : 3,
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1639379146, 1),
                        "t" : NumberLong(2)
                },
                "lastCommittedWallTime" : ISODate("2021-12-13T07:05:46.369Z"),
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1639379146, 1),
                        "t" : NumberLong(2)
                },
                "appliedOpTime" : {
                        "ts" : Timestamp(1639379146, 1),
                        "t" : NumberLong(2)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1639379146, 1),
                        "t" : NumberLong(2)
                },
                "lastAppliedWallTime" : ISODate("2021-12-13T07:05:46.369Z"),
                "lastDurableWallTime" : ISODate("2021-12-13T07:05:46.369Z")
        },
        "lastStableRecoveryTimestamp" : Timestamp(1639379146, 1),
        "electionCandidateMetrics" : {
                "lastElectionReason" : "stepUpRequestSkipDryRun",
                "lastElectionDate" : ISODate("2021-12-13T07:05:26.351Z"),
                "electionTerm" : NumberLong(2),
                "lastCommittedOpTimeAtElection" : {
                        "ts" : Timestamp(1639379123, 1),
                        "t" : NumberLong(1)
                },
                "lastSeenOpTimeAtElection" : {
                        "ts" : Timestamp(1639379123, 1),
                        "t" : NumberLong(1)
                },
                "numVotesNeeded" : 2,
                "priorityAtElection" : 1,
                "electionTimeoutMillis" : NumberLong(10000),
                "priorPrimaryMemberId" : 1,
                "numCatchUpOps" : NumberLong(0),
                "newTermStartDate" : ISODate("2021-12-13T07:05:26.365Z"),
                "wMajorityWriteAvailabilityDate" : ISODate("2021-12-13T07:05:26.372Z")
        },
        "members" : [
                {
                        "_id" : 1,
                        "name" : "192.168.223.133: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"),
                        "lastAppliedWallTime" : ISODate("2021-12-13T07:05:36.368Z"),
                        "lastDurableWallTime" : ISODate("2021-12-13T07:05:36.368Z"),
                        "lastHeartbeat" : ISODate("2021-12-13T07:05:54.436Z"),
                        "lastHeartbeatRecv" : ISODate("2021-12-13T07:05:41.406Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "Error connecting to 192.168.223.133:27017 :: caused by :: Connection refused",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "configVersion" : 11,
                        "configTerm" : 2
                },
                {
                        "_id" : 2,
                        "name" : "192.168.223.129:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 4548,
                        "optime" : {
                                "ts" : Timestamp(1639379146, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDate" : ISODate("2021-12-13T07:05:46Z"),
                        "lastAppliedWallTime" : ISODate("2021-12-13T07:05:46.369Z"),
                        "lastDurableWallTime" : ISODate("2021-12-13T07:05:46.369Z"),
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "electionTime" : Timestamp(1639379126, 1),
                        "electionDate" : ISODate("2021-12-13T07:05:26Z"),
                        "configVersion" : 11,
                        "configTerm" : 2,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                },
                {
                        "_id" : 3,
                        "name" : "192.168.223.130:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 1139,
                        "optime" : {
                                "ts" : Timestamp(1639379146, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1639379146, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDate" : ISODate("2021-12-13T07:05:46Z"),
                        "optimeDurableDate" : ISODate("2021-12-13T07:05:46Z"),
                        "lastAppliedWallTime" : ISODate("2021-12-13T07:05:46.369Z"),
                        "lastDurableWallTime" : ISODate("2021-12-13T07:05:46.369Z"),
                        "lastHeartbeat" : ISODate("2021-12-13T07:05:54.414Z"),
                        "lastHeartbeatRecv" : ISODate("2021-12-13T07:05:54.419Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncSourceHost" : "192.168.223.129:27017",
                        "syncSourceId" : 2,
                        "infoMessage" : "",
                        "configVersion" : 11,
                        "configTerm" : 2
                }
        ],
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1639379146, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        },
        "operationTime" : Timestamp(1639379146, 1)
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容