•副本集在mongodb中是是一組 mongod保持相同的數據集過程,副本集提供冗餘和高可用性,而且是全部生產部署的基礎。
•複製提供冗餘並增長數據可用性,在不用數據庫服務器上具備多個數據副本是,複製能夠提供一個級別的單一數據庫服務器丟失的容錯能力。
•副本集能夠支撐更高的讀操做,由於客戶端能夠向不一樣的服務器發送讀取操做,能夠配置在不一樣的數據中心用做遭難恢復或者報告,備份。
副本集成員最多50個,只有7個成員能夠參與選舉投票,多中心容災能力,自動恢復,滾動式升級服務linux
線上環境常見的架構爲副本集,能夠理解爲一主多從。mongodb
三臺機器同樣配置2核16G內存 存儲盤100G數據庫
"host" : "10.1.1.159:27020"
"host" : "10.1.1.77:27020"
"host" : "10.1.1.178:27020服務器
[root@10-1-1-159 ~]# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.2.1.tgz
[root@10-1-1-159 ~]# tar -zxvf mongodb-linux-x86_64-rhel70-4.2.1.tgz -C /data/
[root@10-1-1-159 ~]# mkdir /data/mongodb/{data,logs,pid,conf} -p
配置文件:架構
[root@10-1-1-159 ~]# cat /data/mongodb/conf/mongodb.conf systemLog: destination: file logAppend: true path: /data/mongodb/logs/mongod.log storage: dbPath: /data/mongodb/data journal: enabled: true directoryPerDB: true wiredTiger: engineConfig: cacheSizeGB: 8 #若是一臺機器啓動一個實例這個能夠註釋選擇默認,若是一臺機器啓動多個實例,須要設置內存大小,避免互相搶佔內存 directoryForIndexes: true processManagement: fork: true pidFilePath: /data/mongodb/pid/mongod.pid net: port: 27020 bindIp: 10.1.1.159,localhost #修改成本機IP地址 maxIncomingConnections: 5000 #security: #keyFile: /data/mongodb/conf/keyfile #authorization: enabled replication: # oplogSizeMB: 1024 replSetName: rs02
[root@10-1-1-159 ~]# scp -r /data/ root@10.1.1.77:/data/
[root@10-1-1-159 ~]# scp -r /data/ root@10.1.1.178:/data/app
目錄結構:curl
[root@10-1-1-178 data]# tree mongodb mongodb ├── conf │ └── mongodb.conf ├── data ├── logs └── pid
groupadd mongod
useradd -g mongod mongod
yum install -y libcurl openssl glibc
cd /data
ln -s mongodb-linux-x86_64-rhel70-4.2.1 mongodb-4.2
chown -R mongod.mongod /data
sudo -u mongod /data/mongodb4.2.1/bin/mongod -f /data/mongodb/conf/mongodb.confide
配置複製集:
#副本集名稱rs02和配置文件中replSetName保持一致測試
config = { _id:"rs02", members:[
{_id:0,host:"10.1.1.159:27010",priority:90},
{_id:1,host:"10.1.1.77:27010",priority:90},
{_id:2,host:"10.1.1.178:27010",arbiterOnly:true}
]
}url
#初始化
rs.initiate(config);
[root@10-1-1-159 ~]# /data/mongodb3.6.9/bin/mongo 10.1.1.159:27020 > use admin switched to db admin > config = { _id:"rs02", members:[ ... {_id:0,host:"10.1.1.159:27020",priority:90}, ... {_id:1,host:"10.1.1.77:27020",priority:90}, ... {_id:2,host:"10.1.1.178:27020",arbiterOnly:true} ... ] ... } { "_id" : "rs02", "members" : [ { "_id" : 0, "host" : "10.1.1.159:27020", "priority" : 90 }, { "_id" : 1, "host" : "10.1.1.77:27020", "priority" : 90 }, { "_id" : 2, "host" : "10.1.1.178:27020", "arbiterOnly" : true } ] } > > rs.initiate(config); 初始化副本集########eeeerrrr { "ok" : 1, "operationTime" : Timestamp(1583907929, 1), "$clusterTime" : { "clusterTime" : Timestamp(1583907929, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } }
rs02:PRIMARY> rs.status() { "set" : "rs02", "date" : ISODate("2020-03-13T07:11:09.427Z"), "myState" : 1, "term" : NumberLong(1), "syncingTo" : "", "syncSourceHost" : "", "syncSourceId" : -1, "heartbeatIntervalMillis" : NumberLong(2000), "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1584083465, 1), "t" : NumberLong(1) }, "readConcernMajorityOpTime" : { "ts" : Timestamp(1584083465, 1), "t" : NumberLong(1) }, "appliedOpTime" : { "ts" : Timestamp(1584083465, 1), "t" : NumberLong(1) }, "durableOpTime" : { "ts" : Timestamp(1584083465, 1), "t" : NumberLong(1) } }, "members" : [ { "_id" : 0, "name" : "10.1.1.159:27020", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", #主節點 "uptime" : 185477, "optime" : { "ts" : Timestamp(1584083465, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2020-03-13T07:11:05Z"), "syncingTo" : "", "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "", "electionTime" : Timestamp(1583907939, 1), "electionDate" : ISODate("2020-03-11T06:25:39Z"), "configVersion" : 1, "self" : true, "lastHeartbeatMessage" : "" }, { "_id" : 1, "name" : "10.1.1.77:27020", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", #從節點 "uptime" : 175540, "optime" : { "ts" : Timestamp(1584083465, 1), "t" : NumberLong(1) }, "optimeDurable" : { "ts" : Timestamp(1584083465, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2020-03-13T07:11:05Z"), "optimeDurableDate" : ISODate("2020-03-13T07:11:05Z"), "lastHeartbeat" : ISODate("2020-03-13T07:11:08.712Z"), "lastHeartbeatRecv" : ISODate("2020-03-13T07:11:08.711Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncingTo" : "10.1.1.159:27020", "syncSourceHost" : "10.1.1.159:27020", "syncSourceId" : 0, "infoMessage" : "", "configVersion" : 1 }, { "_id" : 2, "name" : "10.1.1.178:27020", "health" : 1, "state" : 7, "stateStr" : "ARBITER", #仲裁節點 "uptime" : 175540, "lastHeartbeat" : ISODate("2020-03-13T07:11:08.712Z"), "lastHeartbeatRecv" : ISODate("2020-03-13T07:11:08.711Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncingTo" : "", "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "", "configVersion" : 1 } ], "ok" : 1, "operationTime" : Timestamp(1584083465, 1), "$clusterTime" : { "clusterTime" : Timestamp(1584083465, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } } rs02:PRIMARY>
10.1.1.178:27020 ARBITER 仲裁節點
10.1.1.77:27020 SECONDARY 從節點
10.1.1.159:27020 PRIMARY 主節點
咱們插入一些數據查,而後將主節點停掉,
仲裁節點的日誌
咱們能夠看到,當節點10.1.1.159宕機之後,從新選舉了:Member 10.1.1.77:27010 is now in state PRIMARY
2020-03-18T14:34:53.636+0800 I NETWORK [conn9] end connection 10.1.1.159:49160 (1 connection now open) 2020-03-18T14:34:54.465+0800 I CONNPOOL [Replication] dropping unhealthy pooled connection to 10.1.1.159:27010 2020-03-18T14:34:54.465+0800 I CONNPOOL [Replication] after drop, pool was empty, going to spawn some connections 2020-03-18T14:34:54.465+0800 I ASIO [Replication] Connecting to 10.1.1.159:27010 ...... 2020-03-18T14:35:02.473+0800 I ASIO [Replication] Failed to connect to 10.1.1.159:27010 - HostUnreachable: Error connecting to 10.1.1.159:27010 :: caused by :: Connection refused 2020-03-18T14:35:02.473+0800 I CONNPOOL [Replication] Dropping all pooled connections to 10.1.1.159:27010 due to HostUnreachable: Error connecting to 10.1.1.159:27010 :: caused by :: Connection refused 2020-03-18T14:35:02.473+0800 I REPL_HB [replexec-8] Error in heartbeat (requestId: 662) to 10.1.1.159:27010, response status: HostUnreachable: Error connecting to 10.1.1.159:27010 :: caused by :: Connection refused 2020-03-18T14:35:04.463+0800 I REPL [replexec-5] Member 10.1.1.77:27010 is now in state PRIMARY 2020-03-18T14:35:04.473+0800 I ASIO [Replication] Connecting to 10.1.1.159:27010 2020-03-18T14:35:04.473+0800 I ASIO [Replication] Failed to connect to 10.1.1.159:27010 - HostUnreachable: Error connecting to 10.1.1.159:27010 :: caused by :: Connection refused 2020-03-18T14:35:04.473+0800 I CONNPOOL [Replication] Dropping all pooled connections to 10.1.1.159:27010 due to HostUnreachable: Error connecting to 10.1.1.159:27010 :: caused by :: Connection refused
架構也就變成了下圖:
目前副本集搭建完成,也測試了當一個節點出現問題之後(至少三個節點),並不會影響服務正常讀寫。線上環境咱們須要開啓認證,下一章咱們開始添加用戶,開啓認證受權。