MongoDB 複製集

來源:互聯網
上載者:User

標籤:rpo   啟動參數   match   mem   檔案夾   atm   red   writer   需要   

MongoDB 複製集技能目標
  • 理解MongoDB複製集概念
  • 學會部署MongoDB複製集
  • 理解MongoDB選舉過程
  • 學會MongoDB複製集管理和維護複製集概述複製集的優勢如下
  • 讓資料更安全
  • 提高資料可用性(24*7)
  • 災難恢複
  • 無停機維護(如:索引重建,備份,容錯移轉)
  • 讀縮放(額外的副本讀取)
  • 複本集對應用程式是透明的
複製集的工作原理

MongoDB的複製集至少需要兩個節點。其中一個是主節點(primary),負責處理客戶請求,其餘都是從節點(Secondary),負責複製主節點上的資料

MongoDB各個節點搭配方式為:一主一從或一主多從,主節點記錄其上的所有操作到oplog中,從節點定期輪詢主節點擷取操作,然後對資料副本執行這些操作從而保證從節點的資料與主節點資料是一致的

MongoDB複製結構複製集特點如下
  • N個節點的群集
  • 任何節點可作為主節點
  • 所有寫入操作都在主節點上
  • 自動故上裝一
  • 自動回復
MongoDB複製集部署首先建立資料檔案夾個記錄檔夾
# 資料存放檔案夾[[email protected] ~] cd /data/mongodb/[[email protected] mongodb]# lsmongodb1  mongodb2  mongodb3  mongodb4# 日誌存放檔案夾[[email protected] ~]# cd /data/logs/[[email protected] logs]# touch mongodb{2,3,4}.log [[email protected] logs]# lsmongodb1.log  mongodb2.log  mongodb3.log  mongodb4.log[[email protected] logs]# chmod 777 ./*.log #賦予當前檔案夾以.log結尾做大許可權[[email protected] logs]# lsmongodb1.log  mongodb2.log  mongodb3.log  mongodb4.log[[email protected] logs]# 
複製etc/mongod.conf設定檔並開啟複製集#replication:去掉#號在下一行加入replSetName: kgcrs 複製集名稱為kgcrs
[[email protected] logs]# cp -p /etc/mongod.conf /etc/mongod1.conf  cp:是否覆蓋"/etc/mongod1.conf"? y[[email protected] logs]# vim /etc/mongod1.conf #修改設定檔裡面的資料檔案位置,記錄檔位置和連接埠號碼#複製出三個設定檔並修改檔案位置,連接埠號碼[[email protected] logs]# cp -p /etc/mongod1.conf /etc/mongod2.conf [[email protected] logs]# cp -p /etc/mongod1.conf /etc/mongod3.conf [[email protected] logs]# cp -p /etc/mongod1.conf /etc/mongod4.conf [[email protected] logs]# vim /etc/mongod2.conf [[email protected] logs]# vim /etc/mongod3.conf [[email protected] logs]# vim /etc/mongod4.conf 
開啟節點
#開啟節點[[email protected] logs]# mongod -f /etc/mongod1.conf about to fork child process, waiting until server is ready for connections.forked process: 25849child process started successfully, parent exiting[[email protected] logs]# mongod -f /etc/mongod2.conf about to fork child process, waiting until server is ready for connections.forked process: 25781child process started successfully, parent [[email protected] logs]# mongod -f /etc/mongod3.conf about to fork child process, waiting until server is ready for connections.forked process: 25431child process started successfully, parent exiting[[email protected] logs]# mongod -f /etc/mongod4.conf about to fork child process, waiting until server is ready for connections.forked process: 25851child process started successfully, parent exiting
配置三個節點的複製集
> cfg={"_id":"kgcrs","members":[{"_id":0,"host":"127.0.0.1:27017"},{"_id":1,"host":"127.0.0.1:27018"},{"_id":2,"host":"127.0.0.1:27019"},{"_id":3,"host":"127.0.0.1:27020"},{"_id":4,"host":"127.0.0.1:27021"}]}{    "_id" : "kgcrs",    "members" : [        {            "_id" : 0,            "host" : "127.0.0.1:27017"        },        {            "_id" : 1,            "host" : "127.0.0.1:27018"        },        {            "_id" : 2,            "host" : "127.0.0.1:27019"        },        {            "_id" : 3,            "host" : "127.0.0.1:27020"        },        {            "_id" : 4,            "host" : "127.0.0.1:27021"        }    ]}#初始化配置時保證從節點沒有資料> rs.initiate(cfg) #添加節點kgcrs:PRIMARY> rs.add("IP地址:連接埠號碼")#刪除節點kgcrs:PRIMARY> rs.remove("IP地址:連接埠號碼)
查看複製集(rs複製集的簡稱)
> rs.status()kgcrs:PRIMARY> rs.status(){    "set" : "kgcrs",    "date" : ISODate("2018-07-17T06:03:15.378Z"),    "myState" : 1,    "term" : NumberLong(1),    "syncingTo" : "",    "syncSourceHost" : "",    "syncSourceId" : -1,    "heartbeatIntervalMillis" : NumberLong(2000),    "optimes" : {        "lastCommittedOpTime" : {            "ts" : Timestamp(1531807392, 1),            "t" : NumberLong(1)        },        "readConcernMajorityOpTime" : {            "ts" : Timestamp(1531807392, 1),            "t" : NumberLong(1)        },        "appliedOpTime" : {            "ts" : Timestamp(1531807392, 1),            "t" : NumberLong(1)        },        "durableOpTime" : {            "ts" : Timestamp(1531807392, 1),            "t" : NumberLong(1)        }    },    "members" : [        {            "_id" : 0,            "name" : "127.0.0.1:27017",            "health" : 1,            "state" : 1,            "stateStr" : "PRIMARY",            "uptime" : 1603,            "optime" : {                "ts" : Timestamp(1531807392, 1),                "t" : NumberLong(1)            },            "optimeDate" : ISODate("2018-07-17T06:03:12Z"),            "syncingTo" : "",            "syncSourceHost" : "",            "syncSourceId" : -1,            "infoMessage" : "",            "electionTime" : Timestamp(1531807251, 1),            "electionDate" : ISODate("2018-07-17T06:00:51Z"),            "configVersion" : 1,            "self" : true,            "lastHeartbeatMessage" : ""        },        {            "_id" : 1,            "name" : "127.0.0.1:27018",            "health" : 1,            "state" : 2,            "stateStr" : "SECONDARY",            "uptime" : 154,            "optime" : {                "ts" : Timestamp(1531807392, 1),                "t" : NumberLong(1)            },            "optimeDurable" : {                "ts" : Timestamp(1531807392, 1),                "t" : NumberLong(1)            },            "optimeDate" : ISODate("2018-07-17T06:03:12Z"),            "optimeDurableDate" : ISODate("2018-07-17T06:03:12Z"),            "lastHeartbeat" : ISODate("2018-07-17T06:03:13.741Z"),            "lastHeartbeatRecv" : ISODate("2018-07-17T06:03:14.290Z"),            "pingMs" : NumberLong(0),            "lastHeartbeatMessage" : "",            "syncingTo" : "127.0.0.1:27017",            "syncSourceHost" : "127.0.0.1:27017",            "syncSourceId" : 0,            "infoMessage" : "",            "configVersion" : 1        },        {            "_id" : 2,            "name" : "127.0.0.1:27019",            "health" : 1,            "state" : 2,            "stateStr" : "SECONDARY",            "uptime" : 154,            "optime" : {                "ts" : Timestamp(1531807392, 1),                "t" : NumberLong(1)            },            "optimeDurable" : {                "ts" : Timestamp(1531807392, 1),                "t" : NumberLong(1)            },            "optimeDate" : ISODate("2018-07-17T06:03:12Z"),            "optimeDurableDate" : ISODate("2018-07-17T06:03:12Z"),            "lastHeartbeat" : ISODate("2018-07-17T06:03:13.741Z"),            "lastHeartbeatRecv" : ISODate("2018-07-17T06:03:14.245Z"),            "pingMs" : NumberLong(0),            "lastHeartbeatMessage" : "",            "syncingTo" : "127.0.0.1:27017",            "syncSourceHost" : "127.0.0.1:27017",            "syncSourceId" : 0,            "infoMessage" : "",            "configVersion" : 1        },        {            "_id" : 3,            "name" : "127.0.0.1:27020",            "health" : 1,            "state" : 2,            "stateStr" : "SECONDARY",            "uptime" : 154,            "optime" : {                "ts" : Timestamp(1531807392, 1),                "t" : NumberLong(1)            },            "optimeDurable" : {                "ts" : Timestamp(1531807392, 1),                "t" : NumberLong(1)            },            "optimeDate" : ISODate("2018-07-17T06:03:12Z"),            "optimeDurableDate" : ISODate("2018-07-17T06:03:12Z"),            "lastHeartbeat" : ISODate("2018-07-17T06:03:13.741Z"),            "lastHeartbeatRecv" : ISODate("2018-07-17T06:03:14.291Z"),            "pingMs" : NumberLong(0),            "lastHeartbeatMessage" : "",            "syncingTo" : "127.0.0.1:27017",            "syncSourceHost" : "127.0.0.1:27017",            "syncSourceId" : 0,            "infoMessage" : "",            "configVersion" : 1        },        {            "_id" : 4,            "name" : "127.0.0.1:27021",            "health" : 1,            "state" : 2,            "stateStr" : "SECONDARY",            "uptime" : 154,            "optime" : {                "ts" : Timestamp(1531807392, 1),                "t" : NumberLong(1)            },            "optimeDurable" : {                "ts" : Timestamp(1531807392, 1),                "t" : NumberLong(1)            },            "optimeDate" : ISODate("2018-07-17T06:03:12Z"),            "optimeDurableDate" : ISODate("2018-07-17T06:03:12Z"),            "lastHeartbeat" : ISODate("2018-07-17T06:03:13.741Z"),            "lastHeartbeatRecv" : ISODate("2018-07-17T06:03:14.142Z"),            "pingMs" : NumberLong(0),            "lastHeartbeatMessage" : "",            "syncingTo" : "127.0.0.1:27017",            "syncSourceHost" : "127.0.0.1:27017",            "syncSourceId" : 0,            "infoMessage" : "",            "configVersion" : 1        }    ],    "ok" : 1,    "operationTime" : Timestamp(1531807392, 1),    "$clusterTime" : {        "clusterTime" : Timestamp(1531807392, 1),        "signature" : {            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),            "keyId" : NumberLong(0)        }    }}
容錯移轉切換kill掉一個進程把主節點down掉
# down掉mongodb[[email protected] logs]# ps aux | grep mongoroot     27647  0.3  2.8 1634180 53980 ?       Sl   13:36   0:06 mongod -f /etc/mongod.confroot     27683  0.3  2.7 1482744 52596 ?       Sl   13:36   0:06 mongod -f /etc/mongod1.confroot     27715  0.2  2.8 1466104 52796 ?       Sl   13:36   0:04 mongod -f /etc/mongod2.confroot     27747  0.2  2.7 1474360 52364 ?       Sl   13:36   0:04 mongod -f /etc/mongod3.confroot     27779  0.2  2.8 1465280 52936 ?       Sl   13:36   0:04 mongod -f /etc/mongod4.confroot     28523  0.0  0.0 112676   984 pts/1    R+   14:05   0:00 grep --color=auto mongo[[email protected] logs]# kill -9 27647[[email protected] logs]# ps aux | grep mongoroot     27683  0.3  2.8 1490940 53304 ?       Sl   13:36   0:06 mongod -f /etc/mongod1.confroot     27715  0.2  2.8 1490692 53420 ?       Sl   13:36   0:05 mongod -f /etc/mongod2.confroot     27747  0.2  2.8 1618796 53596 ?       Sl   13:36   0:05 mongod -f /etc/mongod3.confroot     27779  0.2  2.8 1489868 53252 ?       Sl   13:36   0:05 mongod -f /etc/mongod4.confroot     28566  0.0  0.0 112676   980 pts/1    R+   14:06   0:00 grep --color=auto mongo

#自動切換(原本為27017為主伺服器down掉之後自動切換為了27020)

kgcrs:SECONDARY> rs.isMaster() #查看主從{    "hosts" : [        "127.0.0.1:27017",        "127.0.0.1:27018",        "127.0.0.1:27019",        "127.0.0.1:27020",        "127.0.0.1:27021"    ],    "setName" : "kgcrs",    "setVersion" : 1,    "ismaster" : false,    "secondary" : true,    "primary" : "127.0.0.1:27020",     "me" : "127.0.0.1:27017",
手動切換
kgcrs:PRIMARY> rs.freeze(30)     //暫停30s不參與選舉{    "ok" : 0,    "errmsg" : "cannot freeze node when primary or running for election. state: Primary",    "code" : 95,    "codeName" : "NotSecondary",    "operationTime" : Timestamp(1531808896, 1),    "$clusterTime" : {        "clusterTime" : Timestamp(1531808896, 1),        "signature" : {            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),            "keyId" : NumberLong(0)        }    }}#//交出主節點位置,維持從節點狀態不少於60秒,等待30秒使主節點和從節點日誌同步kgcrs:PRIMARY> rs.stepDown(60,30)  2018-07-17T14:28:43.302+0800 E QUERY    [thread1] Error: error doing query: failed: network error while attempting to run command ‘replSetStepDown‘ on host ‘127.0.0.1:27020‘  :[email protected]/mongo/shell/db.js:168:1[email protected]/mongo/shell/db.js:186:16[email protected]/mongo/shell/utils.js:1341:12@(shell):1:12018-07-17T14:28:43.305+0800 I NETWORK  [thread1] trying reconnect to 127.0.0.1:27020 (127.0.0.1) failed2018-07-17T14:28:43.306+0800 I NETWORK  [thread1] reconnect 127.0.0.1:27020 (127.0.0.1) ok#切換為了27018kgcrs:SECONDARY> rs.isMaster(){    "hosts" : [        "127.0.0.1:27017",        "127.0.0.1:27018",        "127.0.0.1:27019",        "127.0.0.1:27020",        "127.0.0.1:27021"    ],    "setName" : "kgcrs",    "setVersion" : 1,    "ismaster" : false,    "secondary" : true,    "primary" : "127.0.0.1:27018",    "me" : "127.0.0.1:27020",
嘗試建立資料庫寫入資料(mongodb的增刪改查操作)
 #增kgcrs:PRIMARY> use kgcswitched to db kgckgcrs:PRIMARY> db.t1.insert({"id":1,"name":"zhangsan"}) WriteResult({ "nInserted" : 1 })kgcrs:PRIMARY> db.t2.insert({"id":2,"name":"zhangsan"})WriteResult({ "nInserted" : 1 })                         kgcrs:PRIMARY> show collectionst1t2#查kgcrs:PRIMARY> db.t1.insert({"id":2,"name":"lisi"})   WriteResult({ "nInserted" : 1 })kgcrs:PRIMARY> db.t1.find(){ "_id" : ObjectId("5b4da41868504a94462710e1"), "id" : 1, "name" : "zhangsan" }{ "_id" : ObjectId("5b4da5a468504a94462710e3"), "id" : 2, "name" : "lisi" }#改kgcrs:PRIMARY> db.t1.update({"id":1},{$set:{"name":"tom"}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })kgcrs:PRIMARY> db.t1.find()                         { "_id" : ObjectId("5b4da41868504a94462710e1"), "id" : 1, "name" : "tom" }{ "_id" : ObjectId("5b4da5a468504a94462710e3"), "id" : 2, "name" : "lisi" }#刪kgcrs:PRIMARY> db.t1.remove({"id":2})WriteResult({ "nRemoved" : 1 })kgcrs:PRIMARY> db.t1.find(){ "_id" : ObjectId("5b4da41868504a94462710e1"), "id" : 1, "name" : "tom" }
上面所做的增刪改查的操作在local的資料庫裡哦我們可以從看看一下
kgcrs:PRIMARY> show dbsadmin    0.000GBconfig   0.000GBkgc      0.000GBlocal    0.000GBschool   0.000GBschool2  0.000GBschool8  0.000GBkgcrs:PRIMARY> use localswitched to db localkgcrs:PRIMARY> show tablesmeoplog.rs                  #做複製集之後所有操作的記錄replset.electionreplset.minvalidreplset.oplogTruncateAfterPointstartup_logsystem.replsetsystem.rollback.idkgcrs:PRIMARY> db.oplog.rs.find(){ "ts" : Timestamp(1531814965, 1), "t" : NumberLong(3), "h" : NumberLong("8639784432240761376"), "v" : 2, "op" : "n", "ns" : "", "wall" : ISODate("2018-07-17T08:09:25.013Z"), "o" : { "msg" : "periodic noop" } }{ "ts" : Timestamp(1531814975, 1), "t" : NumberLong(3), "h" : NumberLong("6221196488842671080"), "v" : 2, "op" : "n", "ns" : "", "wall" : ISODate("2018-07-17T08:09:35.014Z"), "o" : { "msg" : "periodic noop" } }{ "ts" : Timestamp(1531814985, 1), "t" : NumberLong(3), "h" : NumberLong("-8535865731309768612"), "v" : 2, "op" : "n", "ns" : "", "wall" : ISODate("2018-07-17T08:09:45.013Z"), "o" : { "msg" : "periodic noop" } }{ "ts" : Timestamp(1531814995, 1), "t" : NumberLong(3), "h" : NumberLong("4999394607928512799"), "v" : 2, "op" : "n", "ns" : "", "wall" : ISODate("2018-07-17T08:09:55.024Z"), "o" : { "msg" : "periodic noop" } }{ "ts" : Timestamp(1531815005, 1), "t" : NumberLong(3), "h" : NumberLong("-5991841109696910698"), "v" : 2, "op" : "n", "ns" : "", "wall" : ISODate("2018-07-17T08:10:05.024Z"), "o" : { "msg" : "periodic noop" } }{ "ts" : Timestamp(1531815015, 1), "t" : NumberLong(3), "h" : NumberLong("-8100024743592064147"), "v" : 2, "op" : "n", "ns" : "", "wall" : ISODate("2018-07-17T08:10:15.014Z"), "o" : { "msg" : "periodic noop" } }{ "ts" : Timestamp(1531815025, 1), "t" : NumberLong(3), "h" : NumberLong("4558143489540169854"), "v" : 2, "op" : "n", "ns" : "", "wall" : ISODate("2018-07-17T08:10:25.025Z"), "o" : { "msg" : "periodic noo
選舉複製1:標準節點(在標準節點裡面選舉主節點)2:仲裁節點(能夠選擇誰作為主節點,自己不參與選舉,仲裁節點上沒有資料)3:被動節點(不會被選舉為主節點)

passives 被動節點 arbiters 仲裁節點

-------------允許從節點讀取資料-----------
[[email protected] logs]# mongo --port 27018kgcrs:SECONDARY> show dbskgcrs:SECONDARY> rs.slaveOk() #允許預設從節點讀取資料
-------------查看複製狀態資訊------------
kgcrs:SECONDARY> rs.help()kgcrs:SECONDARY> rs.printReplicationInfo()configured oplog size:   990MBlog length start to end: 1544secs (0.43hrs)oplog first event time:  Mon Jul 16 2018 05:49:12 GMT+0800 (CST)oplog last event time:   Mon Jul 16 2018 06:14:56 GMT+0800 (CST)now:                     Mon Jul 16 2018 06:14:59 GMT+0800 (CST)kgcrs:SECONDARY> rs.printSlaveReplicationInfo()source: 192.168.235.200:27018    syncedTo: Mon Jul 16 2018 06:16:16 GMT+0800 (CST)    0 secs (0 hrs) behind the primary source: 192.168.235.200:27019    syncedTo: Mon Jul 16 2018 06:16:16 GMT+0800 (CST)    0 secs (0 hrs) behind the primary #你會發現仲裁節點並不具備資料複製
--------------更改oplog大小---------------
kgcrs:SECONDARY> use localkgcrs:SECONDARY> db.oplog.rs.stats()        "ns" : "local.oplog.rs",    "size" : 20292,    "count" : 178,    "avgObjSize" : 114,    "storageSize" : 45056,        ...kgcrs:SECONDARY> rs.printReplicationInfo()configured oplog size:   990MBlog length start to end: 2024secs (0.56hrs)oplog first event time:  Mon Jul 16 2018 05:49:12 GMT+0800 (CST)oplog last event time:   Mon Jul 16 2018 06:22:56 GMT+0800 (CST)now:                     Mon Jul 16 2018 06:23:05 GMT+0800 (CST)[[email protected] logs]# mongo --port 27018kgcrs:SECONDARY> use adminkgcrs:SECONDARY> db.shutdownServer()登出replication:相關啟動參數,並修改port連接埠號碼27028[[email protected] logs]# mongod -f /etc/mongod2.conf   #單一實例模式啟動全備當前節點的所有oplog記錄[[email protected] logs]# mongodump --port 27028 --db local --collection ‘oplog.rs‘[[email protected] logs]# mongo --port 27028> use local> db.oplog.rs.drop()> db.runCommand( { create: "oplog.rs", capped: true, size: (2 * 1024 * 1024 * 1024) } )> use admin> db.shutdownServer()net:  port: 27018replication:    replSetName: kgcrs    oplogSizeMB: 2048[[email protected] logs]# mongod -f /etc/mongod2.conf[[email protected] logs]# mongo --port 27018kgcrs:PRIMARY> rs.stepDown()   #有效產生選舉
-----------------部署認證複製-------------
kgcrs:PRIMARY> use adminkgcrs:PRIMARY> db.createUser({"user":"root","pwd":"123","roles":["root"]})[[email protected] bin]# vim /etc/mongod.confsecurity:   keyFile: /usr/bin/kgcrskey1   clusterAuthMode: keyFile[[email protected] bin]# vim /etc/mongod2.conf [[email protected] bin]# vim /etc/mongod3.conf [[email protected] bin]# vim /etc/mongod4.conf [[email protected] ~]# cd /usr/bin/[[email protected] bin]# echo "kgcrs key"> kgcrskey1[[email protected] bin]# echo "kgcrs key"> kgcrskey2[[email protected] bin]# echo "kgcrs key"> kgcrskey3[[email protected] bin]# echo "kgcrs key"> kgcrskey4[[email protected] bin]# chmod 600 kgcrskey{1..4}
四個執行個體依次進行重啟
進入primarykgcrs:PRIMARY> show dbs   #無法查看資料庫kgcrs:PRIMARY> rs.status()   #無法查看複製集kgcrs:PRIMARY> use admin    #身份登入驗證kgcrs:PRIMARY> db.auth("root","123")kgcrs:PRIMARY> rs.status()  #可以查看資料庫kgcrs:PRIMARY> show dbs    #可以查看複製集

MongoDB 複製集

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.