MongoDB復(fù)制是將數(shù)據(jù)同步到多個服務(wù)器的過程;
創(chuàng)新互聯(lián)公司專注于清豐網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供清豐營銷型網(wǎng)站建設(shè),清豐網(wǎng)站制作、清豐網(wǎng)頁設(shè)計、清豐網(wǎng)站官網(wǎng)定制、成都小程序開發(fā)服務(wù),打造清豐網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供清豐網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
復(fù)制集提供了數(shù)據(jù)的冗余備份并提高了數(shù)據(jù)的可用性,通??梢员WC數(shù)據(jù)的安全性;
復(fù)制集還允許您從硬件故障和服務(wù)中斷中恢復(fù)數(shù)據(jù)。
- mongodb的復(fù)制集至少需要兩個節(jié)點。其中一個是主節(jié)點,負責處理客戶端請求,其余的都是從節(jié)點,負責復(fù)制主節(jié)點上的數(shù)據(jù)。
- mongodb各個節(jié)點常見的搭配方式為:一主一從、一主多從。
- 主節(jié)點記錄在其上的所有操作oplog,從節(jié)點定期輪詢主節(jié)點獲取這些操作,然后對自己的數(shù)據(jù)副本執(zhí)行這些操作,從而保證從節(jié)點的數(shù)據(jù)與主節(jié)點一致。
(以上圖片引用網(wǎng)址:http://www.runoob.com/mongodb/mongodb-replication.html )
以上結(jié)構(gòu)圖中,客戶端從主節(jié)點讀取數(shù)據(jù),在客戶端寫入數(shù)據(jù)到主節(jié)點時,主節(jié)點與從節(jié)點進行數(shù)據(jù)交互保障數(shù)據(jù)的一致性。
- N 個節(jié)點的集群
- 任何節(jié)點可作為主節(jié)點
- 所有寫入操作都在主節(jié)點上
- 自動故障轉(zhuǎn)移
- 自動恢復(fù)
在同一臺服務(wù)器上創(chuàng)建MongoDB的多實例(4個實例)來做MongoDB主從的實驗。
至于如何安裝MongoDB?請參考之前博文 Linux 平臺安裝MongoDB 4.0(最新版)
#創(chuàng)建各實例的數(shù)據(jù)目錄
mkdir -p /data/mongodb/mongodb{1,2,3,4}
#創(chuàng)建實例配置目錄
mkdir -p /data/conf/
#創(chuàng)建實例的日志目錄
mkdir -p /data/logs/
#創(chuàng)建各實例的日志文件
touch /data/logs/mongodb{1,2,3,4}.log
#賦予日志文件權(quán)限777
chmod 777 /data/logs/*.log
vim /data/mongodb/mongodb1.conf
#mongod.conf
#for documentation of all options, see:
#http://docs.mongodb.org/manual/reference/configuration-options/
#where to write logging data.
systemLog:
destination: file
logAppend: true
path: /data/logs/mongodb1.log //mongodb1的日志文件路徑
#Where and how to store data.
storage:
dbPath: /data/mongodb/mongodb1/ //mongodb1的數(shù)據(jù)文件路徑
journal:
enabled: true
#engine:
#mmapv1:
#wiredTiger:
#how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /data/mongodb/mongodb1/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
#network interfaces
net:
port: 27017 //mongodb1的進程號
bindIp: 0.0.0.0 # Listen to local interface only, comment to listen on all interfaces.
#security:
#operationProfiling:
replication: //刪除“#”,開啟復(fù)制集功能
replSetName: test-rc //名稱為test-rc
#sharding:
##Enterprise-Only Options
#auditLog:
#snmp:
#復(fù)制默認實例的配置文件
cp -p /data/mongodb/mongodb1.conf /data/conf/mongodb2.conf
cp -p /data/mongodb/mongodb1.conf /data/conf/mongodb3.conf
cp -p /data/mongodb/mongodb1.conf /data/conf/mongodb4.conf
cat /data/conf/mongodb2.conf
#mongod.conf
#for documentation of all options, see:
#http://docs.mongodb.org/manual/reference/configuration-options/
#where to write logging data.
systemLog:
destination: file
logAppend: true
path: /data/logs/mongodb2.log //mongodb2的日志文件路徑
#Where and how to store data.
storage:
dbPath: /data/mongodb/mongodb2/ //mongodb2的數(shù)據(jù)文件路徑
journal:
enabled: true
#engine:
#mmapv1:
#wiredTiger:
#how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /data/mongodb/mongodb1/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
#network interfaces
net:
port: 27018 //mongodb2的進程號
bindIp: 0.0.0.0 # Listen to local interface only, comment to listen on all interfaces.
#security:
#operationProfiling:
replication: //刪除“#”,開啟復(fù)制集功能
replSetName: test-rc #名稱為test-rc
#sharding:
##Enterprise-Only Options
#auditLog:
#snmp:
cat /data/conf/mongodb3.conf
#mongod.conf
#for documentation of all options, see:
#http://docs.mongodb.org/manual/reference/configuration-options/
#where to write logging data.
systemLog:
destination: file
logAppend: true
path: /data/logs/mongodb3.log //mongodb3的日志文件路徑
#Where and how to store data.
storage:
dbPath: /data/mongodb/mongodb3/ //mongodb3的數(shù)據(jù)文件路徑
journal:
enabled: true
#engine:
#mmapv1:
#wiredTiger:
#how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /data/mongodb/mongodb1/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
#network interfaces
net:
port: 27019 //mongodb3的進程號
bindIp: 0.0.0.0 # Listen to local interface only, comment to listen on all interfaces.
#security:
#operationProfiling:
replication: //刪除“#”,開啟復(fù)制集功能
replSetName: test-rc #名稱為test-rc
#sharding:
##Enterprise-Only Options
#auditLog:
#snmp:
cat /data/conf/mongodb4.conf
#mongod.conf
#for documentation of all options, see:
#http://docs.mongodb.org/manual/reference/configuration-options/
#where to write logging data.
systemLog:
destination: file
logAppend: true
path: /data/logs/mongodb4.log //mongodb4的日志文件路徑
#Where and how to store data.
storage:
dbPath: /data/mongodb/mongodb4/ //mongodb4的數(shù)據(jù)文件路徑
journal:
enabled: true
#engine:
#mmapv1:
#wiredTiger:
#how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /data/mongodb/mongodb1/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
#network interfaces
net:
port: 27020 //mongodb4的進程號
bindIp: 0.0.0.0 # Listen to local interface only, comment to listen on all interfaces.
#security:
#operationProfiling:
replication: //刪除“#”,開啟復(fù)制集功能
replSetName: test-rc //名稱為test-rc
#sharding:
##Enterprise-Only Options
#auditLog:
#snmp:
for i in 1 2 3 4
do
mongod -f /data/conf/mongodb$i.conf
done
[root@localhost conf]# netstat -tunlp | grep mongod
mongo
> rs.status()
> cfg={"_id":"test-rc","members":[{"_id":0,"host":"192.168.100.100:27017"},{"_id":1,"host":"192.168.100.100:27018"},{"_id":2,"host":"192.168.100.100:27019"}]}
> rs.initiate(cfg)
test-rc:PRIMARY> rs.status()
{
"set" : "test-rc",
"date" : ISODate("2018-07-14T04:46:58.710Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1531543618, 1),
"t" : NumberLong(1)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1531543618, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1531543618, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1531543618, 1),
"t" : NumberLong(1)
}
},
"lastStableCheckpointTimestamp" : Timestamp(1531543608, 1),
"members" : [
{
"_id" : 0,
"name" : "192.168.100.100:27017",
"health" : 1, //健康狀態(tài)
"state" : 1, //1:為主節(jié)點 ; 2:為從節(jié)點
"stateStr" : "PRIMARY", //主節(jié)點
"uptime" : 2886,
"optime" : {
"ts" : Timestamp(1531543618, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-07-14T04:46:58Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1531543426, 1),
"electionDate" : ISODate("2018-07-14T04:43:46Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "192.168.100.100:27018",
"health" : 1, //健康狀態(tài)
"state" : 2, //1:為主節(jié)點 ; 2:為從節(jié)點
"stateStr" : "SECONDARY", //從節(jié)點
"uptime" : 202,
"optime" : {
"ts" : Timestamp(1531543608, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1531543608, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-07-14T04:46:48Z"),
"optimeDurableDate" : ISODate("2018-07-14T04:46:48Z"),
"lastHeartbeat" : ISODate("2018-07-14T04:46:56.765Z"),
"lastHeartbeatRecv" : ISODate("2018-07-14T04:46:57.395Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.100.100:27017",
"syncSourceHost" : "192.168.100.100:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "192.168.100.100:27019",
"health" : 1, //健康狀態(tài)
"state" : 2, //1:為主節(jié)點 ; 2:為從節(jié)點
"stateStr" : "SECONDARY", //從節(jié)點
"uptime" : 202,
"optime" : {
"ts" : Timestamp(1531543608, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1531543608, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-07-14T04:46:48Z"),
"optimeDurableDate" : ISODate("2018-07-14T04:46:48Z"),
"lastHeartbeat" : ISODate("2018-07-14T04:46:56.769Z"),
"lastHeartbeatRecv" : ISODate("2018-07-14T04:46:57.441Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.100.100:27017",
"syncSourceHost" : "192.168.100.100:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1531543618, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1531543618, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
特別提醒:如以上可知道主節(jié)點在192.168.100.100:27017節(jié)點上
注意:嚴禁在從庫做任何修改操作
test-rc:PRIMARY> rs.add("192.168.100.100:27020")
test-rc:PRIMARY> rs.status()
test-rc:PRIMARY> rs.remove("192.168.100.100:27018")
test-rc:PRIMARY> rs.status()
發(fā)現(xiàn)192.168.100.100:27018節(jié)點已經(jīng)沒有相關(guān)信息了
test-rc:PRIMARY> exit
netstat -tunlp | grep mongod
可以查看到共有4個實例的進程信息
kill -9 48211
mongo --port 27019
test-rc:PRIMARY> rs.status()
{
"_id" : 2,
"name" : "192.168.100.100:27019",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1547,
"optime" : {
"ts" : Timestamp(1531544567, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2018-07-14T05:02:47Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1531544345, 1),
"electionDate" : ISODate("2018-07-14T04:59:05Z"),
"configVersion" : 3,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 3,
"name" : "192.168.100.100:27020",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 700,
"optime" : {
"ts" : Timestamp(1531544567, 1),
"t" : NumberLong(2)
},
"optimeDurable" : {
"ts" : Timestamp(1531544567, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2018-07-14T05:02:47Z"),
"optimeDurableDate" : ISODate("2018-07-14T05:02:47Z"),
"lastHeartbeat" : ISODate("2018-07-14T05:02:56.150Z"),
"lastHeartbeatRecv" : ISODate("2018-07-14T05:02:56.289Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.100.100:27019",
"syncSourceHost" : "192.168.100.100:27019",
"syncSourceId" : 2,
"infoMessage" : "",
"configVersion" : 3
}
特別提醒:主節(jié)點已經(jīng)到192.168.100.100:27019節(jié)點上,說明主節(jié)點已經(jīng)自動切換了
test-rc:PRIMARY> rs.freeze(30)
{
"operationTime" : Timestamp(1531544867, 1),
"ok" : 0,
"errmsg" : "cannot freeze node when primary or running for election. state: Primary",
"code" : 95,
"codeName" : "NotSecondary",
"$clusterTime" : {
"clusterTime" : Timestamp(1531544867, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
test-rc:PRIMARY> rs.stepDown(60,30)
> test-rc:PRIMARY> rs.stepDown(60,30)
2018-07-14T01:08:07.326-0400 E QUERY [js] Error: error doing query: failed: network error while attempting to run command 'replSetStepDown' on host '127.0.0.1:27019' :
DB.prototype.runCommand@src/mongo/shell/db.js:168:1
DB.prototype.adminCommand@src/mongo/shell/db.js:186:16
rs.stepDown@src/mongo/shell/utils.js:1398:12
@(shell):1:1
2018-07-14T01:08:07.328-0400 I NETWORK [js] trying reconnect to 127.0.0.1:27019 failed
2018-07-14T01:08:07.329-0400 I NETWORK [js] reconnect 127.0.0.1:27019 ok
> test-rc:PRIMARY> rs.status()
{
"set" : "test-rc",
"date" : ISODate("2018-07-14T05:10:31.161Z"),
"myState" : 2,
"term" : NumberLong(3),
"syncingTo" : "192.168.100.100:27020",
"syncSourceHost" : "192.168.100.100:27020",
"syncSourceId" : 3,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1531545028, 1),
"t" : NumberLong(3)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1531545028, 1),
"t" : NumberLong(3)
},
"appliedOpTime" : {
"ts" : Timestamp(1531545028, 1),
"t" : NumberLong(3)
},
"durableOpTime" : {
"ts" : Timestamp(1531545028, 1),
"t" : NumberLong(3)
}
},
"lastStableCheckpointTimestamp" : Timestamp(1531545018, 1),
"members" : [
{
"_id" : 0,
"name" : "192.168.100.100:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 70,
"optime" : {
"ts" : Timestamp(1531545028, 1),
"t" : NumberLong(3)
},
"optimeDate" : ISODate("2018-07-14T05:10:28Z"),
"syncingTo" : "192.168.100.100:27020",
"syncSourceHost" : "192.168.100.100:27020",
"syncSourceId" : 3,
"infoMessage" : "",
"configVersion" : 3,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 2,
"name" : "192.168.100.100:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 68,
"optime" : {
"ts" : Timestamp(1531545028, 1),
"t" : NumberLong(3)
},
"optimeDurable" : {
"ts" : Timestamp(1531545028, 1),
"t" : NumberLong(3)
},
"optimeDate" : ISODate("2018-07-14T05:10:28Z"),
"optimeDurableDate" : ISODate("2018-07-14T05:10:28Z"),
"lastHeartbeat" : ISODate("2018-07-14T05:10:30.079Z"),
"lastHeartbeatRecv" : ISODate("2018-07-14T05:10:31.094Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.100.100:27020",
"syncSourceHost" : "192.168.100.100:27020",
"syncSourceId" : 3,
"infoMessage" : "",
"configVersion" : 3
},
{
"_id" : 3,
"name" : "192.168.100.100:27020",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 68,
"optime" : {
"ts" : Timestamp(1531545028, 1),
"t" : NumberLong(3)
},
"optimeDurable" : {
"ts" : Timestamp(1531545028, 1),
"t" : NumberLong(3)
},
"optimeDate" : ISODate("2018-07-14T05:10:28Z"),
"optimeDurableDate" : ISODate("2018-07-14T05:10:28Z"),
"lastHeartbeat" : ISODate("2018-07-14T05:10:30.079Z"),
"lastHeartbeatRecv" : ISODate("2018-07-14T05:10:29.561Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1531544897, 1),
"electionDate" : ISODate("2018-07-14T05:08:17Z"),
"configVersion" : 3
}
],
"ok" : 1,
"operationTime" : Timestamp(1531545028, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1531545028, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
特別提醒:此刻主節(jié)點已經(jīng)在192.168.100.100:27020節(jié)點上
另外有需要云服務(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)用場景需求。
標題名稱:MongoDB復(fù)制集(即主從復(fù)制)-創(chuàng)新互聯(lián)
本文地址:http://www.muchs.cn/article8/dsogop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、App設(shè)計、微信公眾號、網(wǎng)站維護、定制網(wǎng)站、動態(tài)網(wǎng)站
聲明:本網(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)容