真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

MongoDB復制集的選舉原理-創(chuàng)新互聯(lián)

復制的原理

成都創(chuàng)新互聯(lián)是專業(yè)的沁陽網(wǎng)站建設(shè)公司,沁陽接單;提供成都網(wǎng)站制作、成都做網(wǎng)站,網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進行沁陽網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!

       復制時基于啊哦做日志 oplog , 相當于 MySQL 中的二進制日志,只記錄發(fā)生改變的記錄。復制是將主節(jié)點的oplog 日志同步并應用到其他從節(jié)點的過程。

選舉的原理

     節(jié)點類型分為標準(host)節(jié)點 、被動(passive)節(jié)點和仲裁(arbiter)節(jié)點。

(1)只有標準節(jié)點可能被選舉為活躍(primary)節(jié)點,有選舉權(quán)。被動節(jié)點有完整副本,不可能成為活躍節(jié)點,有選舉權(quán)。仲裁節(jié)點不復制數(shù)據(jù),不可能成為活躍節(jié)點,只有選舉權(quán)。

(2)標準節(jié)點與被動節(jié)點的區(qū)別:primary 值高者是標準節(jié)點,低者則為被動節(jié)點。

(3)選舉規(guī)則是票數(shù)高者獲勝,primary 是優(yōu)先權(quán)為 0~1000 的值,相當于額為增加 0~1000 的票數(shù)。選舉結(jié)果:票數(shù)高者獲勝;若票數(shù)相同,數(shù)據(jù)新者獲勝。

實驗部署

搭建配置4個 MongoDB 實例 mongodb 、mongpdb2 、mongodb3 、mongodb4 。 IP 地址:192.168.213.184

由于MongoDB 數(shù)據(jù)庫安裝好后,mongodb 為默認第一個實例,因此只要再搭建2 ,3,4 實例即可

1. 配置復制集

(1)創(chuàng)建數(shù)據(jù)文件和日志文件存儲路徑

[root@localhost ~]# mkdir -p /data/mongodb/mongodb{2,3,4}               #創(chuàng)建數(shù)據(jù)庫文件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/ 目錄下的日志存儲路徑
[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)配置實例1 的配置文件,啟用復制集名稱

[root@localhost logs]# vim /etc/mongod.conf
replication:
     replSetName: abc                       #去掉注釋,指定復制集名稱(名稱隨便定義)

創(chuàng)建多實例配置文件

[root@localhost logs]# cp -p /etc/mongod.conf /etc/mongod2.conf       復制生成實例2 的配置文件
[root@localhost logs]# vim /etc/mongod2.conf

systemLog:
  destination: file
  logAppend: true
  path: /data/mongodb/logs/mongodb2.log            #日志文件位置

storage:
  dbPath: /data/mongodb/mongodb2                       #數(shù)據(jù)文件位置
  journal:
    enabled: true

port: 27018       #端口號,默認端口號為27017,因此實例2 的端口號要與之區(qū)別,實例3和4 依次往后排

配置其他實例,與實例2修改一樣,注意端口號

[root@localhost logs]# cp -p /etc/mongod2.conf /etc/mongod3.conf
[root@localhost logs]# cp -p /etc/mongod2.conf /etc/mongod4.conf

(3)啟動服務(wù)并查看端口是否開啟

[root@localhost logs]# mongod -f /etc/mongod.conf –shutdown                     #先關(guān)閉實例1 的服務(wù),在開啟
killing process with pid: 3997                                 #進程號
[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                                           #啟動實例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                                            #啟動實例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                                             #啟動實例4 的服務(wù)
about to fork child process, waiting until server is ready for connections.
forked process: 4955

查看節(jié)點端口是否開啟

[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)查看各實例能否進入 mongodb 數(shù)據(jù)庫 

[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 . 配置復制集的優(yōu)先級

(1)登錄默認實例 mongo,重新配置4個節(jié)點 MongoDB 復制集,設(shè)置兩個標準節(jié)點,有個被動節(jié)點和一個仲裁節(jié)點,命令如下:

根據(jù)優(yōu)先級確定節(jié)點: 優(yōu)先級為 100的為標準節(jié)點,端口號為 27017和27018 ,優(yōu)先級為0 的為被動節(jié)點,端口號為27019;仲裁節(jié)點為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",                  #標準節(jié)點,優(yōu)先級為100
            "priority" : 100
        },
        {
            "_id" : 1,
            "host" : "192.168.213.184:27018",                 #標準節(jié)點
            "priority" : 100
        },
        {
            "_id" : 2,
            "host" : "192.168.213.184:27019",                  #被動節(jié)點,優(yōu)先級為0
            "priority" : 0
        },
        {
            "_id" : 3,
            "host" : "192.168.213.184:27020",                  #仲裁節(jié)點
            "arbiterOnly" : true
        }
    ]
}

> rs.initiate(cfg)                   # 初始化復制集
{
    "ok" : 1,
    "operationTime" : Timestamp(1536819122, 1),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1536819122, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
}

(2)使用命令 rs.isMaster() 查看各節(jié)點身份

abc:PRIMARY> rs.isMaster()
{
    "hosts" : [                                        #標準節(jié)點 
        "192.168.213.184:27017",
        "192.168.213.184:27018"
    ],
    "passives" : [                                   #被動節(jié)點
        "192.168.213.184:27019"
    ],
    "arbiters" : [                                    #仲裁節(jié)點
        "192.168.213.184:27020"
    ],
    "setName" : "abc",
    "setVersion" : 1,
    "ismaster" : true,
    "secondary" : false,
    "primary" : "192.168.213.184:27017",

(3)此時默認實例已經(jīng)被選舉為活躍(primary)節(jié)點,可以在數(shù)據(jù)庫中插入信息了。

abc:PRIMARY> use school                                                #活躍節(jié)點(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)查看日志記錄所有操作,在默認數(shù)據(jù)庫 local 中的oplog.rs  查看

abc:PRIMARY> show dbs
admin  0.000GB
config 0.000GB
local  0.000GB
school 0.000GB
abc:PRIMARY> use local                 #進入 local 數(shù)據(jù)庫
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()    #查看日志記錄所有操作

通過日志記錄,可以找到剛才加入的兩條信息

"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é)點故障,如果主節(jié)點出現(xiàn)故障,另一個標準節(jié)點會選舉成為新的主節(jié)點。

(1)先查看當前狀態(tài),使用命令 rs.status()

abc:PRIMARY> rs.status()

"members" : [
        {
            "_id" : 0,                  
            "name" : "192.168.213.184:27017",              #標準節(jié)點,端口27017,作為主節(jié)點
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 2481,
            "optime" : {
                "ts" : Timestamp(1536820334, 1),
                "t" : NumberLong(1)
            },

{
            "_id" : 1,
            "name" : "192.168.213.184:27018",                #標準節(jié)點 ,端口27018 為從節(jié)點
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 1213,
            "optime" : {
                "ts" : Timestamp(1536820324, 1),
                "t" : NumberLong(1)
            },

{
            "_id" : 2,
            "name" : "192.168.213.184:27019",              #被動節(jié)點, 端口27019位從節(jié)點
            "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é)點
            "health" : 1,
            "state" : 7,
            "stateStr" : "ARBITER",
            "uptime" : 1213,
            "lastHeartbeat" : ISODate("2018-09-13T06:32:14.152Z"),

(2)模擬主節(jié)點故障,關(guān)閉主節(jié)點服務(wù),登錄另一個標準節(jié)點端口 27018 ,查看麟能夠一個標準節(jié)點是否被選舉為主節(jié)點

abc:PRIMARY> exit
bye
[root@localhost logs]# mongod -f /etc/mongod.conf –shutdown                      #關(guān)閉主節(jié)點服務(wù)
killing process with pid: 4821
[root@localhost logs]# mongo --port 27018                                                         #指定27018 端口進入數(shù)據(jù)庫

abc:PRIMARY> rs.status()                                                             #查看狀態(tài)

"members" : [
        {
            "_id" : 0,
            "name" : "192.168.213.184:27017",
            "health" : 0,                                                #健康之為 0 ,說明端口27017 已經(jīng)宕機了
            "state" : 8,
            "stateStr" : "(not reachable/healthy)",
            "uptime" : 0,
            "optime" : {
                "ts" : Timestamp(0, 0),
                "t" : NumberLong(-1)
            },

{
            "_id" : 1,
            "name" : "192.168.213.184:27018",              #此時另一臺標準節(jié)點被選舉為主節(jié)點,端口為 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 代表宕機。 state 為 1 代表主節(jié)點,為 2 代表從節(jié)點

(3)將標準節(jié)點服務(wù)全部關(guān)閉,查看被動節(jié)點是否會被選舉為主節(jié)點

abc:PRIMARY> exit
bye
[root@localhost logs]# mongod -f /etc/mongod2.conf –shutdown                    #關(guān)閉主節(jié)點服務(wù)
killing process with pid: 4853
[root@localhost logs]# mongo --port 27019                                                               #開啟備用節(jié)點服務(wù)

abc:SECONDARY> rs.status()

"members" : [
        {
            "_id" : 0,
            "name" : "192.168.213.184:27017",                                  #此時兩個標準節(jié)點都處于宕機狀態(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",                           #被動節(jié)點并沒有被選舉為主節(jié)點,說明被動節(jié)點不可能成為活躍節(jié)點
            "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)再次啟動標準節(jié)點服務(wù),查看主節(jié)點能否恢復。

abc:SECONDARY> exit
bye
[root@localhost logs]# mongod -f /etc/mongod.conf                                    #啟動實例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                                  #啟動實例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 恢復為主節(jié)點,(與標準節(jié)點啟動順序有關(guān),先啟動的為主節(jié)點)
            "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,

由此可以看出:只有標準節(jié)點可能被選舉為活躍節(jié)點(主 primary),被動節(jié)點不可能成為活躍節(jié)點,有選舉權(quán)。仲裁節(jié)點不可能成為活躍節(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è)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。


分享標題:MongoDB復制集的選舉原理-創(chuàng)新互聯(lián)
鏈接URL:http://weahome.cn/article/cdhcid.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部