mongodb基礎整理篇————副本概念篇[外篇]

前言副本集整理 。開始逐步把mongodb博客補齊了 。
正文什么是副本集副本集是一組服務器,其中一個是用于處理寫入操作的主節點,還有多個用于保存主節點的數據副本的從節點 。
如果主節點崩潰了 , 則從節點會從其中選取出一個新的主節點 。
作用起到一個熱備份 和 容災的作用,如果出現不可預料的事故,比如主節點磁盤損壞,那么可以故障轉移,其他節點將會提到主節點進行寫入 。
實驗現在一臺機器上演示 。
步驟一創建對應的目錄:
數據:
mkdir -p ~/data/rs{1,2,3}日志:
mkdir -p ~/logs/rs{1,2,3}步驟二啟動3個mongod:
mongod --replSet mydb --dbpath ~/data/rs1 --logpath ~/logs/rs1/log --port 27017 --smallfiles --oplogSize=200 &mongod --replSet mydb --dbpath ~/data/rs2 --logpath ~/logs/rs2/log --port 27018 --smallfiles --oplogSize=200 &mongod --replSet mydb --dbpath ~/data/rs3 --logpath ~/logs/rs3/log --port 27019 --smallfiles --oplogSize=200 &步驟三把副本連接到一起,副本集配置傳遞:
mongo --port 27017初始化副本集配置:
rsconf={ "_id" : "mydb", "members" : [{"_id" : 0,"host" : "localhost:27017"},{"_id" : 1,"host" : "localhost:27018"},{"_id" : 2,"host" : "localhost:27019"} ]}rs.initiate(rsconf)這個配置文檔就是副本集的配置 。在localhost:27017 上運行的成員會解析配置并將消息發送給其他成員,提醒他們存在新的配置 。一旦所有成員都加載了配置,他們就會選擇一個主節點并開始處理讀寫操作 。
注意:
不能在不停機的情況下將單機服務器轉換為副本集,以重新啟動并初始化該副本集 。因此,即使一開始只有一臺服務器 , 你也希望將其配置為一個單成員的副本集 。這樣,如果以后想添加更多成員 , 則可以在不停止運行的情況下進行添加 。然后使用rs.status()查看副本集的狀態
{ "set" : "mydb", "date" : ISODate("2022-10-16T02:51:57.670Z"), "myState" : 1, "term" : NumberLong(1), "syncingTo" : "", "syncSourceHost" : "", "syncSourceId" : -1, "heartbeatIntervalMillis" : NumberLong(2000), "optimes" : {"lastCommittedOpTime" : {"ts" : Timestamp(1665888717, 1),"t" : NumberLong(1)},"readConcernMajorityOpTime" : {"ts" : Timestamp(1665888717, 1),"t" : NumberLong(1)},"appliedOpTime" : {"ts" : Timestamp(1665888717, 1),"t" : NumberLong(1)},"durableOpTime" : {"ts" : Timestamp(1665888717, 1),"t" : NumberLong(1)} }, "members" : [{"_id" : 0,"name" : "localhost:27017","health" : 1,"state" : 1,"stateStr" : "PRIMARY","uptime" : 955,"optime" : {"ts" : Timestamp(1665888717, 1),"t" : NumberLong(1)},"optimeDate" : ISODate("2022-10-16T02:51:57Z"),"syncingTo" : "","syncSourceHost" : "","syncSourceId" : -1,"infoMessage" : "","electionTime" : Timestamp(1665888285, 1),"electionDate" : ISODate("2022-10-16T02:44:45Z"),"configVersion" : 1,"self" : true,"lastHeartbeatMessage" : ""},{"_id" : 1,"name" : "localhost:27018","health" : 1,"state" : 2,"stateStr" : "SECONDARY","uptime" : 443,"optime" : {"ts" : Timestamp(1665888707, 1),"t" : NumberLong(1)},"optimeDurable" : {"ts" : Timestamp(1665888707, 1),"t" : NumberLong(1)},"optimeDate" : ISODate("2022-10-16T02:51:47Z"),"optimeDurableDate" : ISODate("2022-10-16T02:51:47Z"),"lastHeartbeat" : ISODate("2022-10-16T02:51:55.983Z"),"lastHeartbeatRecv" : ISODate("2022-10-16T02:51:57.128Z"),"pingMs" : NumberLong(0),"lastHeartbeatMessage" : "","syncingTo" : "localhost:27017","syncSourceHost" : "localhost:27017","syncSourceId" : 0,"infoMessage" : "","configVersion" : 1},{"_id" : 2,"name" : "localhost:27019","health" : 1,"state" : 2,"stateStr" : "SECONDARY","uptime" : 443,"optime" : {"ts" : Timestamp(1665888707, 1),"t" : NumberLong(1)},"optimeDurable" : {"ts" : Timestamp(1665888707, 1),"t" : NumberLong(1)},"optimeDate" : ISODate("2022-10-16T02:51:47Z"),"optimeDurableDate" : ISODate("2022-10-16T02:51:47Z"),"lastHeartbeat" : ISODate("2022-10-16T02:51:56.020Z"),"lastHeartbeatRecv" : ISODate("2022-10-16T02:51:57.065Z"),"pingMs" : NumberLong(0),"lastHeartbeatMessage" : "","syncingTo" : "localhost:27017","syncSourceHost" : "localhost:27017","syncSourceId" : 0,"infoMessage" : "","configVersion" : 1} ], "ok" : 1, "operationTime" : Timestamp(1665888717, 1), "$clusterTime" : {"clusterTime" : Timestamp(1665888717, 1),"signature" : {"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),"keyId" : NumberLong(0)} }}這樣就可以看到27017 是主節點,其他的不是 。
然后rs 是一個mongo 命令的封裝:
mydb:PRIMARY> rs.help() rs.status(){ replSetGetStatus : 1 } checks repl set status rs.initiate(){ replSetInitiate : null } initiates set with default settings rs.initiate(cfg){ replSetInitiate : cfg } initiates set with configuration cfg rs.conf()get the current configuration object from local.system.replset rs.reconfig(cfg)updates the configuration of a running replica set with cfg (disconnects) rs.add(hostportstr)add a new member to the set with default attributes (disconnects) rs.add(membercfgobj)add a new member to the set with extra attributes (disconnects) rs.addArb(hostportstr)add a new member which is arbiterOnly:true (disconnects) rs.stepDown([stepdownSecs, catchUpSecs])step down as primary (disconnects) rs.syncFrom(hostportstr)make a secondary sync from the given member rs.freeze(secs)make a node ineligible to become primary for the time specified rs.remove(hostportstr)remove a host from the replica set (disconnects) rs.secondaryOk()allow queries on secondary nodes rs.printReplicationInfo()check oplog size and time range rs.printSecondaryReplicationInfo()check replica set members and replication lag db.isMaster()check who is primary db.hello()check who is primary reconfiguration helpers disconnect from the database so the shell will display an error, even if the command succeeds.

推薦閱讀