您好,登錄后才能下訂單哦!
復(fù)制的原理
復(fù)制時(shí)基于啊哦做日志 oplog , 相當(dāng)于 MySQL 中的二進(jìn)制日志,只記錄發(fā)生改變的記錄。復(fù)制是將主節(jié)點(diǎn)的oplog 日志同步并應(yīng)用到其他從節(jié)點(diǎn)的過(guò)程。
選舉的原理
節(jié)點(diǎn)類(lèi)型分為標(biāo)準(zhǔn)(host)節(jié)點(diǎn) 、被動(dòng)(passive)節(jié)點(diǎn)和仲裁(arbiter)節(jié)點(diǎn)。
(1)只有標(biāo)準(zhǔn)節(jié)點(diǎn)可能被選舉為活躍(primary)節(jié)點(diǎn),有選舉權(quán)。被動(dòng)節(jié)點(diǎn)有完整副本,不可能成為活躍節(jié)點(diǎn),有選舉權(quán)。仲裁節(jié)點(diǎn)不復(fù)制數(shù)據(jù),不可能成為活躍節(jié)點(diǎn),只有選舉權(quán)。
(2)標(biāo)準(zhǔn)節(jié)點(diǎn)與被動(dòng)節(jié)點(diǎn)的區(qū)別:primary 值高者是標(biāo)準(zhǔn)節(jié)點(diǎn),低者則為被動(dòng)節(jié)點(diǎn)。
(3)選舉規(guī)則是票數(shù)高者獲勝,primary 是優(yōu)先權(quán)為 0~1000 的值,相當(dāng)于額為增加 0~1000 的票數(shù)。選舉結(jié)果:票數(shù)高者獲勝;若票數(shù)相同,數(shù)據(jù)新者獲勝。
實(shí)驗(yàn)部署
搭建配置4個(gè) MongoDB 實(shí)例 mongodb 、mongpdb2 、mongodb3 、mongodb4 。 IP 地址:192.168.213.184
由于MongoDB 數(shù)據(jù)庫(kù)安裝好后,mongodb 為默認(rèn)第一個(gè)實(shí)例,因此只要再搭建2 ,3,4 實(shí)例即可
1. 配置復(fù)制集
(1)創(chuàng)建數(shù)據(jù)文件和日志文件存儲(chǔ)路徑
[root@localhost ~]# mkdir -p /data/mongodb/mongodb{2,3,4} #創(chuàng)建數(shù)據(jù)庫(kù)文件2,3,4
[root@localhost ~]# cd /data/mongodb/
[root@localhost mongodb]# ls
mongodb2 mongodb3 mongodb4
[root@localhost mongodb]# mkdir logs
[root@localhost mongodb]# ls
logs mongodb2 mongodb3 mongodb4
[root@localhost mongodb]# cd logs/
[root@localhost logs]# touch mongodb{2,3,4}.log #在 /data/mongodb/logs/ 目錄下的日志存儲(chǔ)路徑
[root@localhost logs]# chmod - R 777 /data/mongodb/logs/*.log #修改目錄權(quán)限
[root@localhost logs]# ls -l
總用量 0
-rwxrwxrwx. 1 root root 0 9月 13 18:43 mongodb2.log
-rwxrwxrwx. 1 root root 0 9月 13 18:43 mongodb3.log
-rwxrwxrwx. 1 root root 0 9月 13 18:43 mongodb4.log
(2)配置實(shí)例1 的配置文件,啟用復(fù)制集名稱(chēng)
[root@localhost logs]# vim /etc/mongod.conf
replication:
replSetName: abc #去掉注釋?zhuān)付◤?fù)制集名稱(chēng)(名稱(chēng)隨便定義)
創(chuàng)建多實(shí)例配置文件
[root@localhost logs]# cp -p /etc/mongod.conf /etc/mongod2.conf 復(fù)制生成實(shí)例2 的配置文件
[root@localhost logs]# vim /etc/mongod2.confsystemLog:
destination: file
logAppend: true
path: /data/mongodb/logs/mongodb2.log #日志文件位置storage:
dbPath: /data/mongodb/mongodb2 #數(shù)據(jù)文件位置
journal:
enabled: trueport: 27018 #端口號(hào),默認(rèn)端口號(hào)為27017,因此實(shí)例2 的端口號(hào)要與之區(qū)別,實(shí)例3和4 依次往后排
配置其他實(shí)例,與實(shí)例2修改一樣,注意端口號(hào)
[root@localhost logs]# cp -p /etc/mongod2.conf /etc/mongod3.conf
[root@localhost logs]# cp -p /etc/mongod2.conf /etc/mongod4.conf
(3)啟動(dòng)服務(wù)并查看端口是否開(kāi)啟
[root@localhost logs]# mongod -f /etc/mongod.conf –shutdown #先關(guān)閉實(shí)例1 的服務(wù),在開(kāi)啟
killing process with pid: 3997 #進(jìn)程號(hào)
[root@localhost logs]# mongod -f /etc/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 4867
child process started successfully, parent exiting
[root@localhost logs]# mongod -f /etc/mongod2.conf #啟動(dòng)實(shí)例2 的服務(wù)
about to fork child process, waiting until server is ready for connections.
forked process: 4899
child process started successfully, parent exiting
[root@localhost logs]# mongod -f /etc/mongod3.conf #啟動(dòng)實(shí)例3 的服務(wù)
about to fork child process, waiting until server is ready for connections.
forked process: 4927
child process started successfully, parent exiting
[root@localhost logs]# mongod -f /etc/mongod4.conf #啟動(dòng)實(shí)例4 的服務(wù)
about to fork child process, waiting until server is ready for connections.
forked process: 4955查看節(jié)點(diǎn)端口是否開(kāi)啟
[root@localhost logs]# netstat -ntap
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:27019 0.0.0.0:* LISTEN 4927/mongod
tcp 0 0 0.0.0.0:27020 0.0.0.0:* LISTEN 4955/mongod
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 4867/mongod
tcp 0 0 0.0.0.0:27018 0.0.0.0:* LISTEN 4899/mongod
(4)查看各實(shí)例能否進(jìn)入 mongodb 數(shù)據(jù)庫(kù)
[root@localhost logs]# mongo
MongoDB shell version v3.6.7
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.7[root@localhost logs]# mongo --port 27018
MongoDB shell version v3.6.7
connecting to: mongodb://127.0.0.1:27018/
MongoDB server version: 3.6.7
Server has startup warnings:[root@localhost logs]# mongo --port 27019
MongoDB shell version v3.6.7
connecting to: mongodb://127.0.0.1:27019/
MongoDB server version: 3.6.7
Server has startup warnings:[root@localhost logs]# mongo --port 27020
MongoDB shell version v3.6.7
connecting to: mongodb://127.0.0.1:27020/
MongoDB server version: 3.6.7
2 . 配置復(fù)制集的優(yōu)先級(jí)
(1)登錄默認(rèn)實(shí)例 mongo,重新配置4個(gè)節(jié)點(diǎn) MongoDB 復(fù)制集,設(shè)置兩個(gè)標(biāo)準(zhǔn)節(jié)點(diǎn),有個(gè)被動(dòng)節(jié)點(diǎn)和一個(gè)仲裁節(jié)點(diǎn),命令如下:
根據(jù)優(yōu)先級(jí)確定節(jié)點(diǎn): 優(yōu)先級(jí)為 100的為標(biāo)準(zhǔn)節(jié)點(diǎn),端口號(hào)為 27017和27018 ,優(yōu)先級(jí)為0 的為被動(dòng)節(jié)點(diǎn),端口號(hào)為27019;仲裁節(jié)點(diǎn)為27020
> cfg={"_id":"abc","members":[{"_id":0,"host":"192.168.213.184:27017","priority":100},{"_id":1,"host":"192.168.213.184:27018","priority":100},{"_id":2,"host":"192.168.213.184:27019","priority":0},{"_id":3,"host":"192.168.213.184:27020","arbiterOnly":true}]}
{
"_id" : "abc",
"members" : [
{
"_id" : 0,
"host" : "192.168.213.184:27017", #標(biāo)準(zhǔn)節(jié)點(diǎn),優(yōu)先級(jí)為100
"priority" : 100
},
{
"_id" : 1,
"host" : "192.168.213.184:27018", #標(biāo)準(zhǔn)節(jié)點(diǎn)
"priority" : 100
},
{
"_id" : 2,
"host" : "192.168.213.184:27019", #被動(dòng)節(jié)點(diǎn),優(yōu)先級(jí)為0
"priority" : 0
},
{
"_id" : 3,
"host" : "192.168.213.184:27020", #仲裁節(jié)點(diǎn)
"arbiterOnly" : true
}
]
}
> rs.initiate(cfg) # 初始化復(fù)制集
{
"ok" : 1,
"operationTime" : Timestamp(1536819122, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1536819122, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
(2)使用命令 rs.isMaster() 查看各節(jié)點(diǎn)身份
abc:PRIMARY> rs.isMaster()
{
"hosts" : [ #標(biāo)準(zhǔn)節(jié)點(diǎn)
"192.168.213.184:27017",
"192.168.213.184:27018"
],
"passives" : [ #被動(dòng)節(jié)點(diǎn)
"192.168.213.184:27019"
],
"arbiters" : [ #仲裁節(jié)點(diǎn)
"192.168.213.184:27020"
],
"setName" : "abc",
"setVersion" : 1,
"ismaster" : true,
"secondary" : false,
"primary" : "192.168.213.184:27017",
(3)此時(shí)默認(rèn)實(shí)例已經(jīng)被選舉為活躍(primary)節(jié)點(diǎn),可以在數(shù)據(jù)庫(kù)中插入信息了。
abc:PRIMARY> use school #活躍節(jié)點(diǎn)(primory)
switched to db school
abc:PRIMARY> db.info.insert({"id":1,"name":"lili"}) #插入兩條信息
WriteResult({ "nInserted" : 1 })
abc:PRIMARY> db.info.insert({"id":2,"name":"tom"})
WriteResult({ "nInserted" : 1 })
(4)查看日志記錄所有操作,在默認(rèn)數(shù)據(jù)庫(kù) local 中的oplog.rs 查看
abc:PRIMARY> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
school 0.000GB
abc:PRIMARY> use local #進(jìn)入 local 數(shù)據(jù)庫(kù)
switched to db local
abc:PRIMARY> show collections #顯示
me
oplog.rs
replset.election
replset.minvalid
startup_log
system.replset
system.rollback.id
abc:PRIMARY> db.oplog.rs.find() #查看日志記錄所有操作
通過(guò)日志記錄,可以找到剛才加入的兩條信息
"school.info", "ui" : UUID("2b9c93b6-a58a-4021-a2b4-33f9b19925d8"), "wall" : ISODate("2018-09-13T06:24:44.537Z"), "o" : { "_id" : ObjectId("5b9a02aca106c3eab9c639e5"), "id" : 1, "name" : "lili" } }
{ "ts" : Timestamp(1536819899, 1), "t" : NumberLong(1), "h" : NumberLong("-1447313909384631008"), "v" : 2, "op" : "i", "ns" : "school.info", "ui" : UUID("2b9c93b6-a58a-4021-a2b4-33f9b19925d8"), "wall" : ISODate("2018-09-13T06:24:59.186Z"), "o" : { "_id" : ObjectId("5b9a02bba106c3eab9c639e6"), "id" : 2, "name" : "tom" } }
3.模擬節(jié)點(diǎn)故障,如果主節(jié)點(diǎn)出現(xiàn)故障,另一個(gè)標(biāo)準(zhǔn)節(jié)點(diǎn)會(huì)選舉成為新的主節(jié)點(diǎn)。
(1)先查看當(dāng)前狀態(tài),使用命令 rs.status()
abc:PRIMARY> rs.status()
"members" : [
{
"_id" : 0,
"name" : "192.168.213.184:27017", #標(biāo)準(zhǔn)節(jié)點(diǎn),端口27017,作為主節(jié)點(diǎn)
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 2481,
"optime" : {
"ts" : Timestamp(1536820334, 1),
"t" : NumberLong(1)
},{
"_id" : 1,
"name" : "192.168.213.184:27018", #標(biāo)準(zhǔn)節(jié)點(diǎn) ,端口27018 為從節(jié)點(diǎn)
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1213,
"optime" : {
"ts" : Timestamp(1536820324, 1),
"t" : NumberLong(1)
},{
"_id" : 2,
"name" : "192.168.213.184:27019", #被動(dòng)節(jié)點(diǎn), 端口27019位從節(jié)點(diǎn)
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1213,
"optime" : {
"ts" : Timestamp(1536820324, 1),
"t" : NumberLong(1)
},{
"_id" : 3,
"name" : "192.168.213.184:27020", #仲裁節(jié)點(diǎn)
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 1213,
"lastHeartbeat" : ISODate("2018-09-13T06:32:14.152Z"),
(2)模擬主節(jié)點(diǎn)故障,關(guān)閉主節(jié)點(diǎn)服務(wù),登錄另一個(gè)標(biāo)準(zhǔn)節(jié)點(diǎn)端口 27018 ,查看麟能夠一個(gè)標(biāo)準(zhǔn)節(jié)點(diǎn)是否被選舉為主節(jié)點(diǎn)
abc:PRIMARY> exit
bye
[root@localhost logs]# mongod -f /etc/mongod.conf –shutdown #關(guān)閉主節(jié)點(diǎn)服務(wù)
killing process with pid: 4821
[root@localhost logs]# mongo --port 27018 #指定27018 端口進(jìn)入數(shù)據(jù)庫(kù)abc:PRIMARY> rs.status() #查看狀態(tài)
"members" : [
{
"_id" : 0,
"name" : "192.168.213.184:27017",
"health" : 0, #健康之為 0 ,說(shuō)明端口27017 已經(jīng)宕機(jī)了
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},{
"_id" : 1,
"name" : "192.168.213.184:27018", #此時(shí)另一臺(tái)標(biāo)準(zhǔn)節(jié)點(diǎn)被選舉為主節(jié)點(diǎn),端口為 27018
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 2812,
"optime" : {
"ts" : Timestamp(1536820668, 1),
"t" : NumberLong(2)
},{
"_id" : 2,
"name" : "192.168.213.184:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1552,
"optime" : {
"ts" : Timestamp(1536820668, 1),
"t" : NumberLong(2)
},{
"_id" : 3,
"name" : "192.168.213.184:27020",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 1552,其中, health 為 1 代表健康,0 代表宕機(jī)。 state 為 1 代表主節(jié)點(diǎn),為 2 代表從節(jié)點(diǎn)
(3)將標(biāo)準(zhǔn)節(jié)點(diǎn)服務(wù)全部關(guān)閉,查看被動(dòng)節(jié)點(diǎn)是否會(huì)被選舉為主節(jié)點(diǎn)
abc:PRIMARY> exit
bye
[root@localhost logs]# mongod -f /etc/mongod2.conf –shutdown #關(guān)閉主節(jié)點(diǎn)服務(wù)
killing process with pid: 4853
[root@localhost logs]# mongo --port 27019 #開(kāi)啟備用節(jié)點(diǎn)服務(wù)abc:SECONDARY> rs.status()
"members" : [
{
"_id" : 0,
"name" : "192.168.213.184:27017", #此時(shí)兩個(gè)標(biāo)準(zhǔn)節(jié)點(diǎn)都處于宕機(jī)狀態(tài),健康值為 0
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},{
"_id" : 1,
"name" : "192.168.213.184:27018",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},{
"_id" : 2,
"name" : "192.168.213.184:27019", #被動(dòng)節(jié)點(diǎn)并沒(méi)有被選舉為主節(jié)點(diǎn),說(shuō)明被動(dòng)節(jié)點(diǎn)不可能成為活躍節(jié)點(diǎn)
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 3102,
"optime" : {
"ts" : Timestamp(1536820928, 1),
"t" : NumberLong(2)
},{
"_id" : 3,
"name" : "192.168.213.184:27020",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 1849,
(4)再次啟動(dòng)標(biāo)準(zhǔn)節(jié)點(diǎn)服務(wù),查看主節(jié)點(diǎn)能否恢復(fù)。
abc:SECONDARY> exit
bye
[root@localhost logs]# mongod -f /etc/mongod.conf #啟動(dòng)實(shí)例1 的服務(wù)
about to fork child process, waiting until server is ready for connections.
forked process: 39839
child process started successfully, parent exiting
[root@localhost logs]# mongod -f /etc/mongod2.conf #啟動(dòng)實(shí)例2的服務(wù)
about to fork child process, waiting until server is ready for connections.
forked process: 39929
child process started successfully, parent exiting
[root@localhost logs]# mongo{
"_id" : 0,
"name" : "192.168.213.184:27017", #端口27017 恢復(fù)為主節(jié)點(diǎn),(與標(biāo)準(zhǔn)節(jié)點(diǎn)啟動(dòng)順序有關(guān),先啟動(dòng)的為主節(jié)點(diǎn))
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 25,
"optime" : {
"ts" : Timestamp(1536821324, 1),
"t" : NumberLong(3)
},{
"_id" : 1,
"name" : "192.168.213.184:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 14,
"optime" : {
"ts" : Timestamp(1536821324, 1),
"t" : NumberLong(3)
},{
"_id" : 2,
"name" : "192.168.213.184:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 23,
"optime" : {{
"_id" : 3,
"name" : "192.168.213.184:27020",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 23,
由此可以看出:只有標(biāo)準(zhǔn)節(jié)點(diǎn)可能被選舉為活躍節(jié)點(diǎn)(主 primary),被動(dòng)節(jié)點(diǎn)不可能成為活躍節(jié)點(diǎn),有選舉權(quán)。仲裁節(jié)點(diǎn)不可能成為活躍節(jié)點(diǎn)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。