標籤:初始化 mongodb複製 alt show base comm 行資料 query process
MongoDB 複製集
複製集的優勢
mongodb各個節點常見的搭配方式為:一主一從、一主多從。
主節點記錄在其上的所有操作oplog,從節點定期輪詢主節點擷取這些操作,然後對自己的資料副本執行這些操作,從而保證從節點的資料與主節點一致。
MongoDB複製結構圖如下所示:
以上結構圖中,用戶端從主節點讀取資料,在用戶端寫入資料到主節點時, 主節點與從節點進行資料互動保障資料的一致性。
複製集集特徵:
- N 個節點的叢集
- 任何節點可作為主節點
- 所有寫入操作都在主節點上
- 自動容錯移轉
- 自動回復
安裝 MongoDB (tar安裝) :
tar -zxvf mongodb-linux-x86_64-3.2.1.tgz -C /usr/local/cd /usr/local/mv mongodb-linux-x86_64-3.2.1/ mongodb #重新命名mkdir -p /data/mongodb/mongodb{1,2,3,4} #建立資料目錄mkdir -p /data/logstouch /data/logs/mongodb{1,2,3,4}.log #建立記錄檔cd /data/logs/chmod 777 *.log #賦予許可權cd /usr/local/mongodb/binvim mongodb1.confport=27017dbpath=/data/mongodb/mongodb1logpath=/data/logs/mongodb1.loglogappend=truefork=truemaxConns=5000storageEngine=mmapv1replSet=test #複製集名稱------------------------------------------------------------------------------------#下面是yum安裝時修改、添加的replication: #去注釋 replSetName: test #添加複製集名稱
ln -s /usr/local/mongodb/bin/mongod /usr/bin/ #把mongodb常用的命令添加到系統命令中ln -s /usr/local/mongodb/bin/mongo /usr/bin/
[[email protected] bin]# mongo> cfg={"_id":"test","members":[{"_id":0,"host":"192.168.217.129:27017"},{"_id":1,"host":"192.168.217.129:27018"},{"_id":2,"host":"192.168.217.129:27019"}]}#配置複製集 ,注意複製集的名稱要一致{ "_id" : "test", "members" : [ { "_id" : 0, "host" : "192.168.217.129:27017" }, { "_id" : 1, "host" : "192.168.217.129:27018" }, { "_id" : 2, "host" : "192.168.217.129:27019" } ]}> rs.initiate(cfg) #初始化配置時保證從節點沒有資料{ "ok" : 1 }test:PRIMARY> rs.status() #查看複製集的完整狀態{ "set" : "test", ....... { "_id" : 0, "name" : "192.168.217.129:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", #27017 連接埠作為主節點 "uptime" : 1234, "optime" : { "ts" : Timestamp(1531961046, 1), "t" : NumberLong(1) }, ...... }, { "_id" : 1, "name" : "192.168.217.129:27018", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", #從節點 "uptime" : 49, "optime" : { "ts" : Timestamp(1531961046, 1), "t" : NumberLong(1) }, ...... }, { "_id" : 2, "name" : "192.168.217.129:27019", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", #從節點 "uptime" : 49, "optime" : { "ts" : Timestamp(1531961046, 1), "t" : NumberLong(1) }, ....... } ], "ok" : 1}
test:PRIMARY> rs.add("192.168.217.129:27020") #添加節點{ "ok" : 1 }test:PRIMARY> rs.status() ........ "_id" : 3, "name" : "192.168.217.129:27020", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", ........test:PRIMARY> rs.remove("192.168.217.129:27020") #刪除節點{ "ok" : 1 }test:PRIMARY> rs.status().........
[[email protected] bin]# mongod -f mongodb1.conf --shutdown #關閉主節點連接埠killing process with pid: 3552[[email protected] bin]# mongo --port 27018 test:SECONDARY> rs.status(){ "set" : "test", ....... { "_id" : 0, "name" : "192.168.217.129:27017", "health" : 0, "state" : 8, "stateStr" : "(not reachable/healthy)", "uptime" : 0, "optime" : { ....... { "_id" : 1, "name" : "192.168.217.129:27018", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 1811, ....... { "_id" : 2, "name" : "192.168.217.129:27019", #自動切換主節點 "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 712, ...... } ], "ok" : 1}
[[email protected] bin]# mongo --port 27019 test:PRIMARY> rs.freeze(30) #暫停30s不參與選舉{ "ok" : 1 }test:PRIMARY> rs.stepDown(60,30) #交出主節點位置,維持從節點狀態不少於60秒,等待30秒使主節點和從節點日誌同步test:SECONDARY> rs.status()......
test:SECONDARY> show dbs #2018-07-19T09:04:34.898+0800 E QUERY [thread1] Error: listDatabases failed:{ "ok" : 0, "errmsg" : "not master and slaveOk=false", "code" : 13435 } :[email protected]/mongo/shell/utils.js:23:13[email protected]/mongo/shell/mongo.js:53:1[email protected]/mongo/shell/utils.js:700:19[email protected]/mongo/shell/utils.js:594:15@(shellhelp2):1:1test:SECONDARY> rs.slaveOk() #允許預設從節點讀取資料test:SECONDARY> show dbslocal 1.078GB
test:PRIMARY> use localswitched to db localtest:PRIMARY> rs.printReplicationInfo() #查看記錄檔能夠使用的大小 預設oplog大小會佔用64位執行個體5%的可用磁碟空間configured oplog size: 95.37109375MBlog length start to end: 1103secs (0.31hrs)oplog first event time: Thu Jul 19 2018 08:43:55 GMT+0800 (CST)oplog last event time: Thu Jul 19 2018 09:02:18 GMT+0800 (CST)now: Thu Jul 19 2018 09:20:08 GMT+0800 (CST)test:PRIMARY> db.runCommand({"convertToCapped":"oplog.rs","size":10000000000}) #修改 單位:B{ "ok" : 1 }test:PRIMARY> rs.printReplicationInfo()configured oplog size: 9536.746032714844MBlog length start to end: 1103secs (0.31hrs)oplog first event time: Thu Jul 19 2018 08:43:55 GMT+0800 (CST)oplog last event time: Thu Jul 19 2018 09:02:18 GMT+0800 (CST)now: Thu Jul 19 2018 09:20:24 GMT+0800 (CST)test:PRIMARY>
大資料 MongoDB 複製集管理