標籤:collect 多執行個體 服務 path eid commit 刪除節點 讀取資料 started
複製集概念:
複製集是額外的資料副本,是跨多個伺服器同步資料的過程,提供了冗餘並增加了資料的可用性,通過它可以對硬體故障和中斷的服務進行資料恢複
複製集工作原理:
MongoDB複製集最少需要兩個節點。主節點:負責處理用戶端的請求,從節點:負責複製主節點上的資料搭配方式:一主一從或一主多從註:用戶端在主節點寫入資料,在從節點讀取資料,主從進行資料互動,保證資料的一致性
MongoDB複製集部署(1)配置複製集
[[email protected] ~]# mkdir -p /data/moongodb/mongodb{2,3,4} //建立多執行個體[[email protected] ~]# cd /data/mongodb/[[email protected] mongodb]# mongo mongod2.log mongodb2 mongodb3 mongodb4[[email protected] mongodb]# mkdir logs[[email protected] mongodb]# lslogs mongo mongodb2 mongodb3 mongodb4[[email protected] mongodb]# touch logs/mongodb{2,3,4}.log[[email protected] mongodb]# cd logs/[[email protected] logs]# lsmongodb2.log mongodb3.log mongodb4.log[[email protected] logs]# chmod 777 *.log //賦予最大許可權[[email protected] logs]# lsmongodb2.log mongodb3.log mongodb4.log[[email protected] logs]# ll總用量 0-rwxrwxrwx. 1 root root 0 7月 17 08:59 mongodb2.log-rwxrwxrwx. 1 root root 0 7月 17 08:59 mongodb3.log-rwxrwxrwx. 1 root root 0 7月 17 08:59 mongodb4.log
(2)編輯4個MongoDB執行個體的設定檔
[[email protected] etc]# vim mongod.confreplication: //注釋此項 replSetName: root123 //添加複製集名稱(自取),注意前面空兩格 [[email protected] etc]# mongod -f /etc/mongod.conf --shutdown //關閉服務killing process with pid: 1084[[email protected] etc]# mongod -f /etc/mongod.conf //開啟服務about to fork child process, waiting until server is ready for connections.forked process: 11329child process started successfully, parent exiting註:修改設定檔後需重啟服務,檔案生效[[email protected] etc]# cp -p mongod.conf mongod2.conf cp:是否覆蓋"mongod2.conf"? y[[email protected] etc]# vim mongod2.confpath:/data/mongodb/logs/mongodb2.log //日誌存放位置dbpath:/data/mongodb/mongodb2 //資料存放位置port: //修改連接埠號碼,連接埠號碼不能相同[[email protected] etc]# cp -p mongod2.conf mongod3.conf [[email protected] etc]# cp -p mongod2.conf mongod4.conf [[email protected] etc]# vim mongod3.conf //改序號和連接埠號碼即可,其它和2相同配置[[email protected] etc]# vim mongod4.conf [[email protected] etc]# mongod -f /etc/mongod2.conf[[email protected] etc]# mongod -f /etc/mongod3.conf[[email protected] etc]# mongod -f /etc/mongod4.conf[[email protected] etc]# netstat -antp | grep mongod //四台執行個體都已開啟tcp 0 0 0.0.0.0:27019 0.0.0.0:* LISTEN 11599/mongod tcp 0 0 0.0.0.0:27020 0.0.0.0:* LISTEN 11627/mongod tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 11459/mongod tcp 0 0 0.0.0.0:27018 0.0.0.0:* LISTEN 10252/mongod
(3)配置三個節點的複製集
[[email protected] etc]# systemctl stop firewalld.service //關閉防火牆[[email protected] etc]# setenforce 0[[email protected] etc]#mongo> show dbs> cfg={"_id":"root123","members":[{"_id":0,"host":"192.168.200.184:27017"},{"_id":1,"host":"192.168.200.184:27018"},{"_id":2,"host":"192.168.200.184:27019"}]} //配置節點複製集{ "_id" : "root123", "members" : [ { "_id" : 0, "host" : "192.168.200.184:27017" }, { "_id" : 1, "host" : "192.168.200.184:27018" }, { "_id" : 2, "host" : "192.168.200.184:27019" } ]}> db.stats() { "db" : "test", "collections" : 0, "views" : 0, "objects" : 0, "avgObjSize" : 0, "dataSize" : 0, "storageSize" : 0, "numExtents" : 0, "indexes" : 0, "indexSize" : 0, "fileSize" : 0, "fsUsedSize" : 0, "fsTotalSize" : 0, "ok" : 1, //狀態ok=1,複製成功 "$clusterTime" : { "clusterTime" : Timestamp(0, 0), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } }}123:SECONDARY> rs.status() //查看複製集狀態{ "set" : "root123", "date" : ISODate("2018-07-17T03:24:03.253Z"), "myState" : 1, "term" : NumberLong(1), "syncingTo" : "", "syncSourceHost" : "", "syncSourceId" : -1, "heartbeatIntervalMillis" : NumberLong(2000), "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1531797840, 1), "t" : NumberLong(1) }, "readConcernMajorityOpTime" : { "ts" : Timestamp(1531797840, 1), "t" : NumberLong(1) }, "appliedOpTime" : { "ts" : Timestamp(1531797840, 1), "t" : NumberLong(1) }, "durableOpTime" : { "ts" : Timestamp(1531797840, 1), "t" : NumberLong(1) } }, "members" : [ { "_id" : 0, "name" : "192.168.200.184:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", //狀態1為主 "uptime" : 980, "optime" : { "ts" : Timestamp(1531797840, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2018-07-17T03:24:00Z"), "syncingTo" : "", "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "could not find member to sync from", "electionTime" : Timestamp(1531797808, 1), "electionDate" : ISODate("2018-07-17T03:23:28Z"), "configVersion" : 1, "self" : true, "lastHeartbeatMessage" : "" }, { "_id" : 1, "name" : "192.168.200.184:27018", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", //狀態2為從 "uptime" : 45, "optime" : { "ts" : Timestamp(1531797840, 1), "t" : NumberLong(1) }, "optimeDurable" : { "ts" : Timestamp(1531797840, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2018-07-17T03:24:00Z"), "optimeDurableDate" : ISODate("2018-07-17T03:24:00Z"), "lastHeartbeat" : ISODate("2018-07-17T03:24:02.633Z"), "lastHeartbeatRecv" : ISODate("2018-07-17T03:24:02.920Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncingTo" : "192.168.200.184:27017", "syncSourceHost" : "192.168.200.184:27017", "syncSourceId" : 0, "infoMessage" : "", "configVersion" : 1 }, { "_id" : 2, "name" : "192.168.200.184:27019", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", //狀態2為從 "uptime" : 45, "optime" : { "ts" : Timestamp(1531797840, 1), "t" : NumberLong(1) }, "optimeDurable" : { "ts" : Timestamp(1531797840, 1), "t" : NumberLong(1) }, "optimeDate" : ISODate("2018-07-17T03:24:00Z"), "optimeDurableDate" : ISODate("2018-07-17T03:24:00Z"), "lastHeartbeat" : ISODate("2018-07-17T03:24:02.633Z"), "lastHeartbeatRecv" : ISODate("2018-07-17T03:24:02.896Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncingTo" : "192.168.200.184:27017", "syncSourceHost" : "192.168.200.184:27017", "syncSourceId" : 0, "infoMessage" : "", "configVersion" : 1 } ], "ok" : 1, "operationTime" : Timestamp(1531797840, 1), "$clusterTime" : { "clusterTime" : Timestamp(1531797840, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } }}root123:PRIMARY> //最後顯示此狀態注:初始化配置時保證從節點沒有資料
(4)添加和刪除節點
root123:PRIMARY> rs.add("192.168.200.184:27020"){ "ok" : 1, "operationTime" : Timestamp(1531799035, 1), "$clusterTime" : { "clusterTime" : Timestamp(1531799035, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } }}root123:PRIMARY> rs.stats()"_id" : 3, "name" : "192.168.200.184:27020", //添加成功 "health" : 1, "state" : 2, "stateStr" : "SECONDARY", //也為從節點 "uptime" : 26, "optime" : { "ts" : Timestamp(1531799060, 1), "t" : NumberLong(1) },root123:PRIMARY> rs.remove("192.168.200.184:27020"){ "ok" : 1, //刪除成功 "operationTime" : Timestamp(1531799257, 1), "$clusterTime" : { "clusterTime" : Timestamp(1531799257, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } }}
(5)類比故障自動轉移
[[email protected] mongodb]# ps aux | grep mongodroot 12342 1.3 5.8 1465664 58768 ? Sl 11:07 0:38 mongod -f /etc/mongod3.confroot 12387 1.0 5.9 1442988 59124 ? Sl 11:07 0:29 mongod -f /etc/mongod4.confroot 12428 1.4 6.4 1582772 64516 ? Sl 11:07 0:40 mongod -f /etc/mongod.confroot 12667 1.5 6.2 1459800 62268 ? Sl 11:17 0:35 mongod -f /etc/mongod2.confroot 13655 0.0 0.0 112676 984 pts/0 S+ 11:55 0:00 grep --color=auto mongod[[email protected] mongodb]# kill -9 12428[[email protected] mongodb]# mongoroot123:SECONDARY> rs.status()"members" : [ { "_id" : 0, "name" : "192.168.200.184:27017", "health" : 0, //第一台健康值為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) },{ "_id" : 2, "name" : "192.168.200.184:27019", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", //27019跳為主節點 "uptime" : 2039, "optime" : { "ts" : Timestamp(1531799828, 1), "t" : NumberLong(2) }, "optimeDurable" : { "ts" : Timestamp(1531799828, 1), "t" : NumberLong(2) },
多學多練,快樂無限~!!!
每天一篇,深入學習MongoDB複製集