1、簡介
成都創(chuàng)新互聯(lián)是一家專業(yè)提供臨沂企業(yè)網(wǎng)站建設(shè),專注與成都做網(wǎng)站、成都網(wǎng)站建設(shè)、H5響應(yīng)式網(wǎng)站、小程序制作等業(yè)務(wù)。10年已為臨沂眾多企業(yè)、政府機構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)絡(luò)公司優(yōu)惠進行中。MongoDB復制是將數(shù)據(jù)同步在多個服務(wù)器的過程,類似MySQL Replication是一種異步復制;
復制提供了數(shù)據(jù)的冗余備份,并在多個服務(wù)器上存儲數(shù)據(jù)副本,提高了數(shù)據(jù)的可用性, 并可以保證數(shù)據(jù)的安全性。
2、MongoDB復制特點
(1) mongodb的節(jié)點有三種角色:primary,seconfary,arbiter,
primary負責處理客戶端請求,讀寫數(shù)據(jù);
secondary通過oplog復制主節(jié)點上的數(shù)據(jù),也可讀數(shù)據(jù);
arbiter節(jié)點不存儲數(shù)據(jù),僅投票功能,所以不需要單獨的硬件配置;
(2)如果primary不可以用,secondary會提升為新的primary; 由參數(shù)electionTimeoutMillis=10s控制
3、實驗環(huán)境
MongoDB-3.0.6版本
一臺物理機器上三個MongoDB實例,分別占用三個節(jié)點
172.16.101.54 27017
172.16.101.54 27018
172.16.101.54 27019
4、創(chuàng)建數(shù)據(jù)和日志目錄
[root@sht-sgmhadoopcm-01 mongodb]# mkdir -p /usr/local/mongodb/data/{db27017,db27018,db27019}
[root@sht-sgmhadoopcm-01 mongodb]# mkdir -p /usr/local/mongodb/log
5、配置文件/etc/mongod.conf
三個節(jié)點分別創(chuàng)建配置文件,文件內(nèi)容區(qū)別是節(jié)點不同,其他都相同,參數(shù)replSetname用來指定副本集名稱為rs0
[root@sht-sgmhadoopcm-01 mongodb]# ll /etc/mongod2701*.conf
-rw-r--r-- 1 root root 405 Nov 2 20:21 /etc/mongod27017.conf
-rw-r--r-- 1 root root 405 Nov 2 20:13 /etc/mongod27018.conf
-rw-r--r-- 1 root root 405 Nov 2 20:13 /etc/mongod27019.conf
mongodb配置文件每個冒號后面要有一個空格,否則會報錯
[root@sht-sgmhadoopcm-01 mongodb]# cat /etc/mongod27017.conf systemLog: destination: file path: "/usr/local/mongodb/log/mongod27017.log" logAppend: true storage: dbPath: /usr/local/mongodb/data/db27017 journal: enabled: true processManagement: fork: true pidFilePath: /usr/local/mongodb/data/db27017/mongod27017.pid net: port: 27017 bindIp: 0.0.0.0 setParameter: enableLocalhostAuthBypass: false replication: replSetName: rs06、啟動MongoDB
分別啟動三個節(jié)點的MongoDB服務(wù),并指定配置文件
[root@sht-sgmhadoopcm-01 mongodb]# bin/mongod --config /etc/mongod27017.conf
[root@sht-sgmhadoopcm-01 mongodb]# bin/mongod --config /etc/mongod27018.conf
[root@sht-sgmhadoopcm-01 mongodb]# bin/mongod --config /etc/mongod27019.conf
7、初始化副本集
登錄27017節(jié)點
[root@sht-sgmhadoopcm-01 mongodb]# bin/mongo 172.16.101.54:27017/admin
> rs.initiate()
{
"info2" : "no configuration explicitly specified -- making one",
"me" : "sht-sgmhadoopcm-01:27017",
"ok" : 1
}
添加成員到副本集,priority大的節(jié)點提成為主的可能性就大
rs0:PRIMARY> rs.add('172.16.101.54:27018')
{ "ok" : 1 }
rs0:PRIMARY> rs.add('172.16.101.54:27019',priority:5)
{ "ok" : 1 }
查看副本集配置
rs0:PRIMARY> rs.conf() { "_id" : "rs0", "version" : 3, "members" : [ { "_id" : 0, "host" : "sht-sgmhadoopcm-01:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : 0, "votes" : 1 }, { "_id" : 1, "host" : "172.16.101.54:27018", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : 0, "votes" : 1 }, { "_id" : 2, "host" : "172.16.101.54:27019", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : 0, "votes" : 1 } ], "settings" : { "chainingAllowed" : true, "heartbeatTimeoutSecs" : 10, "getLastErrorModes" : { }, "getLastErrorDefaults" : { "w" : 1, "wtimeout" : 0 } } }查看各個角色狀態(tài)
rs0:PRIMARY> rs.status() { "set" : "rs0", "date" : ISODate("2018-11-02T12:27:44.570Z"), "myState" : 1, "members" : [ { "_id" : 0, "name" : "sht-sgmhadoopcm-01:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 626, "optime" : Timestamp(1541161589, 1), "optimeDate" : ISODate("2018-11-02T12:26:29Z"), "electionTime" : Timestamp(1541161465, 2), "electionDate" : ISODate("2018-11-02T12:24:25Z"), "configVersion" : 3, "self" : true }, { "_id" : 1, "name" : "172.16.101.54:27018", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 161, "optime" : Timestamp(1541161589, 1), "optimeDate" : ISODate("2018-11-02T12:26:29Z"), "lastHeartbeat" : ISODate("2018-11-02T12:27:43.430Z"), "lastHeartbeatRecv" : ISODate("2018-11-02T12:27:43.311Z"), "pingMs" : 0, "syncingTo" : "sht-sgmhadoopcm-01:27017", "configVersion" : 3 }, { "_id" : 2, "name" : "172.16.101.54:27019", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 75, "optime" : Timestamp(1541161589, 1), "optimeDate" : ISODate("2018-11-02T12:26:29Z"), "lastHeartbeat" : ISODate("2018-11-02T12:27:43.430Z"), "lastHeartbeatRecv" : ISODate("2018-11-02T12:27:43.430Z"), "pingMs" : 0, "lastHeartbeatMessage" : "could not find member to sync from", "configVersion" : 3 } ], "ok" : 1 }8、數(shù)據(jù)同步測試
向27017節(jié)點插入數(shù)據(jù)
[root@sht-sgmhadoopcm-01 mongodb]# bin/mongo 172.16.101.54:27017/admin
rs0:PRIMARY> db.user.insert({"username":"test","age":12,"sex":"m"})
WriteResult({ "nInserted" : 1 })
rs0:PRIMARY> db.user.find()
{ "_id" : ObjectId("5bdc65ff048de313da6c425a"), "username" : "test", "age" : 12, "sex" : "m" }
數(shù)據(jù)成功的同步到27018和27019節(jié)點
[root@sht-sgmhadoopcm-01 mongodb]# bin/mongo 172.16.101.54:27018/admin
rs0:SECONDARY> db.getMongo().setReadPref('secondaryPreferred')
rs0:SECONDARY> db.user.find()
{ "_id" : ObjectId("5bdc65ff048de313da6c425a"), "username" : "test", "age" : 12, "sex" : "m" }
[root@sht-sgmhadoopcm-01 mongodb]# bin/mongo 172.16.101.54:27019/admin
rs0:SECONDARY> db.getMongo().setReadPref('secondaryPreferred')
rs0:SECONDARY> db.user.find()
{ "_id" : ObjectId("5bdc65ff048de313da6c425a"), "username" : "test", "age" : 12, "sex" : "m" }
9、AutoFailover測試
kill當前的primary27017節(jié)點
[root@sht-sgmhadoopcm-01 mongodb]# ps -ef|grep mongod
root 5703 1 0 20:17 ? 00:00:52 bin/mongod --config /etc/mongod27017.conf
root 5747 1 0 20:17 ? 00:00:51 bin/mongod --config /etc/mongod27018.conf
root 5979 1 0 20:20 ? 00:00:52 bin/mongod --config /etc/mongod27019.conf
[root@sht-sgmhadoopcm-01 mongodb]# kill -9 5703
可以看到27019變成primary,此時27017不可用
[root@sht-sgmhadoopcm-01 mongodb]# bin/mongo 172.16.101.54:27018/admin
rs0:SECONDARY> rs.status() { "set" : "rs0", "date" : ISODate("2018-11-02T15:10:18.746Z"), "myState" : 2, "members" : [ { "_id" : 0, "name" : "sht-sgmhadoopcm-01:27017", "health" : 0, "state" : 8, "stateStr" : "(not reachable/healthy)", "uptime" : 0, "optime" : Timestamp(0, 0), "optimeDate" : ISODate("1970-01-01T00:00:00Z"), "lastHeartbeat" : ISODate("2018-11-02T15:10:18.631Z"), "lastHeartbeatRecv" : ISODate("2018-11-02T15:09:28.583Z"), "pingMs" : 0, "lastHeartbeatMessage" : "Failed attempt to connect to sht-sgmhadoopcm-01:27017; couldn't connect to server sht-sgmhadoopcm-01:27017 (172.16.101.54), connection attempt failed", "configVersion" : -1 }, { "_id" : 1, "name" : "172.16.101.54:27018", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 10349, "optime" : Timestamp(1541170688, 2), "optimeDate" : ISODate("2018-11-02T14:58:08Z"), "configVersion" : 3, "self" : true }, { "_id" : 2, "name" : "172.16.101.54:27019", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 9827, "optime" : Timestamp(1541170688, 2), "optimeDate" : ISODate("2018-11-02T14:58:08Z"), "lastHeartbeat" : ISODate("2018-11-02T15:10:18.611Z"), "lastHeartbeatRecv" : ISODate("2018-11-02T15:10:18.592Z"), "pingMs" : 0, "electionTime" : Timestamp(1541171370, 1), "electionDate" : ISODate("2018-11-02T15:09:30Z"), "configVersion" : 3 } ], "ok" : 1 }重新啟動27017節(jié)點,會自動變成了27019的備庫,說明auto failover成功了
[root@sht-sgmhadoopcm-01 mongodb]# bin/mongod --config /etc/mongod27017.conf
rs0:PRIMARY> rs.status() { "set" : "rs0", "date" : ISODate("2018-11-02T15:13:27.035Z"), "myState" : 1, "members" : [ { "_id" : 0, "name" : "sht-sgmhadoopcm-01:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 42, "optime" : Timestamp(1541170688, 2), "optimeDate" : ISODate("2018-11-02T14:58:08Z"), "lastHeartbeat" : ISODate("2018-11-02T15:13:26.764Z"), "lastHeartbeatRecv" : ISODate("2018-11-02T15:13:25.055Z"), "pingMs" : 0, "configVersion" : 3 }, { "_id" : 1, "name" : "172.16.101.54:27018", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 10017, "optime" : Timestamp(1541170688, 2), "optimeDate" : ISODate("2018-11-02T14:58:08Z"), "lastHeartbeat" : ISODate("2018-11-02T15:13:26.662Z"), "lastHeartbeatRecv" : ISODate("2018-11-02T15:13:26.678Z"), "pingMs" : 0, "lastHeartbeatMessage" : "could not find member to sync from", "configVersion" : 3 }, { "_id" : 2, "name" : "172.16.101.54:27019", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 10349, "optime" : Timestamp(1541170688, 2), "optimeDate" : ISODate("2018-11-02T14:58:08Z"), "electionTime" : Timestamp(1541171370, 1), "electionDate" : ISODate("2018-11-02T15:09:30Z"), "configVersion" : 3, "self" : true } ], "ok" : 1 }FAQ
[root@sht-sgmhadoopcm-01 mongodb]# bin/mongod --config /etc/mongod27017.conf &
[1] 4885
[root@sht-sgmhadoopcm-01 mongodb]# Error parsing YAML config file: yaml-cpp: error at line 8, column 11: illegal map value
try 'bin/mongod --help' for more information
[1]+ Exit 2 bin/mongod --config /etc/mongod27017.conf
原因是配置文件/etc/mongod.conf冒號后面沒有跟空格。
參考鏈接
MongoDB Replication
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
網(wǎng)頁名稱:MongoDBReplicaSet副本集-創(chuàng)新互聯(lián)
文章網(wǎng)址:http://www.rwnh.cn/article14/ccjcde.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、網(wǎng)站設(shè)計公司、Google、品牌網(wǎng)站制作、網(wǎng)站導航、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容