溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Mongodb基操--將復(fù)制集與節(jié)點(diǎn)選舉玩弄于股掌之中

發(fā)布時(shí)間:2020-03-04 05:58:40 來源:網(wǎng)絡(luò) 閱讀:2969 作者:一介余民 欄目:MongoDB數(shù)據(jù)庫
復(fù)制集簡介

Mongodb復(fù)制集由一組Mongod實(shí)例(進(jìn)程)組成,包含一個(gè)Primary節(jié)點(diǎn)和多個(gè)Secondary節(jié)點(diǎn),Mongodb Driver(客戶端)的所有數(shù)據(jù)都寫入Primary,Secondary從Primary同步寫入的數(shù)據(jù),以保持復(fù)制集內(nèi)所有成員存儲(chǔ)相同的數(shù)據(jù)集,提供數(shù)據(jù)的高可用。

 


什么是復(fù)制?
  • 保障數(shù)據(jù)的安全性

  • 數(shù)據(jù)高可用性 (24*7)

  • 災(zāi)難恢復(fù)

  • 無需停機(jī)維護(hù)(如備份,重建索引,壓縮)

  • 分布式讀取數(shù)據(jù)

 

副本集特征:
  • N 個(gè)節(jié)點(diǎn)的集群

  • 任何節(jié)點(diǎn)可作為主節(jié)點(diǎn)

  • 所有寫入操作都在主節(jié)點(diǎn)上

  • 自動(dòng)故障轉(zhuǎn)移

  • 自動(dòng)恢復(fù)

 

復(fù)制集圖解

Mongodb基操--將復(fù)制集與節(jié)點(diǎn)選舉玩弄于股掌之中

 

實(shí)驗(yàn)環(huán)境

系統(tǒng):centos7

mongodb版本:v3.6.7

 

操作流程

添加三個(gè)實(shí)例

mkdir -p /data/mongodb/mongodb{2,3,4}    /首先創(chuàng)建數(shù)據(jù)文件存放位置
mkdir -p /data/mongodb/logs                     /日志文件存放位置
touch /data/mongodb/logs/mongodb{2,3,4}.log   /日志文件
chmod 777 /data/mongodb/logs/*.log    /給日志文件加權(quán)

cd /data/mongodb/               /記得檢查操作是否奏效
[root@cent mongodb]# ls
logs  mongodb2  mongodb3  mongodb4
[root@cent mongodb]# cd logs/
[root@cent logs]# ll
總用量 0
-rwxrwxrwx. 1 root root 0 9月  12 09:48 mongodb2.log
-rwxrwxrwx. 1 root root 0 9月  12 09:48 mongodb3.log
-rwxrwxrwx. 1 root root 0 9月  12 09:48 mongodb4.log

編輯2.3.4的配置文件

vim /etc/mongod2.conf
修改如下
systemLog:
destination: file   
logAppend: true   
path:        //后面的3,4則改為mongodb3.log,mongodb4.log  
storage:   dbPath:            /后面的3,4則改為mongodb3,4   
journal:     
 enabled: true
net: 
  port:                   /后面的3,4則改為27019,27020 
  bindIp: 0.0.0.0
replication:      
   replSetName: yang

啟動(dòng)與檢測·

開啟服務(wù)
[root@cent logs]# mongod -f /etc/mongod2.conf  about to fork child process, waiting until server is ready for connections.
forked process: 83795child process started successfully, parent exiting
[root@cent logs]# mongod -f /etc/mongod3.conf  about to fork child process, waiting until server is ready for connections.
forked process: 83823child process started successfully, parent exiting
[root@cent logs]# mongod -f /etc/mongod4.conf  about to fork child process, waiting until server is ready for connections.
forked process: 83851child process started successfully, parent exiting
[root@cent logs]# netstat -ntap                                     /檢測端口,分別看到27017,18,19,20


登陸測試

[root@cent logs]# mongo --port 27019      /指定端口登陸
MongoDB shell version v3.6.7
connecting to: mongodb://127.0.0.1:27019
/MongoDB server version: 3.6.7
>

復(fù)制集操作

定義復(fù)制集

[root@cent logs]# mongo     //27017端口
>cfg={"_id":"yang","members":[{"_id":0,"host":"192.168.137.11:27017"},{"_id":1,"host":"192.168.137.11:27018"},{"_id":2,"host":"192.168.137.11:27019"}]}
  //定義復(fù)制集
  { 
    "_id" : "yang",
    "members" : [
        {
            "_id" : 0,
            "host" : "192.168.137.11:27017"
        },
        {
            "_id" : 1,
            "host" : "192.168.137.11:27018"
        },
        {
            "_id" : 2,
            "host" : "192.168.137.11:27019"
        }
    ]
}
啟動(dòng)
> rs.initiate(cfg)
{
    "ok" : 1,
    "operationTime" : Timestamp(1536848891, 1),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1536848891, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
}


查看復(fù)制集信息

yang:OTHER> db.stats(){
    "db" : "test",
    "collections" : 0,
    "views" : 0,
    "objects" : 0,
    "avgObjSize" : 0,
    "dataSize" : 0,
    "storageSize" : 0,
    "numExtents" : 0,
    "indexes" : 0,
    "indexSize" : 0,
    "fileSize" : 0,
    "fsUsedSize" : 0,
    "fsTotalSize" : 0,
    "ok" : 1,
    "operationTime" : Timestamp(1536741495, 1),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1536741495, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
}

查看復(fù)制集狀態(tài)

yang:SECONDARY> rs.status()               
{
    "set" : "yang",
    "date" : ISODate("2018-09-12T08:58:56.358Z"),
    "myState" : 1,
    "term" : NumberLong(1),
    "syncingTo" : "",
    "syncSourceHost" : "",
    "syncSourceId" : -1,
    "heartbeatIntervalMillis" : NumberLong(2000),
    "optimes" : {
        "lastCommittedOpTime" : {
            "ts" : Timestamp(1536742728, 1),
            "t" : NumberLong(1)
        },
        "readConcernMajorityOpTime" : {
            "ts" : Timestamp(1536742728, 1),
            "t" : NumberLong(1)
        },
        "appliedOpTime" : {
            "ts" : Timestamp(1536742728, 1),
            "t" : NumberLong(1)
        },
        "durableOpTime" : {
            "ts" : Timestamp(1536742728, 1),
            "t" : NumberLong(1)
        }
    },
    "members" : [{
            "_id" : 0,
            "name" : "192.168.137.11:27017",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 24741,
            "optime" : {
                "ts" : Timestamp(1536742728, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2018-09-12T08:58:48Z"),
            "syncingTo" : "",
            "syncSourceHost" : "",
            "syncSourceId" : -1,
            "infoMessage" : "",
            "electionTime" : Timestamp(1536741506, 1),
            "electionDate" : ISODate("2018-09-12T08:38:26Z"),
            "configVersion" : 1,
            "self" : true,
            "lastHeartbeatMessage" : ""
        },
        {
            "_id" : 1,
            "name" : "192.168.137.11:27018",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 1240,
            "optime" : {
                "ts" : Timestamp(1536742728, 1),
                "t" : NumberLong(1)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1536742728, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2018-09-12T08:58:48Z"),
            "optimeDurableDate" : ISODate("2018-09-12T08:58:48Z"),
            "lastHeartbeat" : ISODate("2018-09-12T08:58:54.947Z"),
            "lastHeartbeatRecv" : ISODate("2018-09-12T08:58:55.699Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "192.168.137.11:27017",
            "syncSourceHost" : "192.168.137.11:27017",
            "syncSourceId" : 0,
            "infoMessage" : "",
            "configVersion" : 1
        },
        {
            "_id" : 2,
            "name" : "192.168.137.11:27019",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 1240,
            "optime" : {
                "ts" : Timestamp(1536742728, 1),
                "t" : NumberLong(1)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1536742728, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2018-09-12T08:58:48Z"),
            "optimeDurableDate" : ISODate("2018-09-12T08:58:48Z"),
            "lastHeartbeat" : ISODate("2018-09-12T08:58:54.947Z"),
            "lastHeartbeatRecv" : ISODate("2018-09-12T08:58:55.760Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "192.168.137.11:27017",
            "syncSourceHost" : "192.168.137.11:27017",
            "syncSourceId" : 0,
            "infoMessage" : "",
            "configVersion" : 1
        }],
    "ok" : 1,
    "operationTime" : Timestamp(1536742728, 1),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1536742728, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
}

添加與刪除

添加節(jié)點(diǎn)時(shí),不要有數(shù)據(jù),否則可能丟失數(shù)據(jù)。

添加一個(gè)27020節(jié)點(diǎn)

yang:PRIMARY> rs.add("192.168.137.11:27020")
{
    "ok" : 1,
    "operationTime" : Timestamp(1536849195, 1),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1536849195, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
}

查驗(yàn)

yang:PRIMARY> rs.status()

{
            "_id" : 3,
            "name" : "192.168.137.11:27020",          //27020在末尾出現(xiàn)了

 

刪除27020

yang:PRIMARY> rs.remove("192.168.137.11:27020")
{
    "ok" : 1,
    "operationTime" : Timestamp(1536849620, 1),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1536849620, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
}


故障轉(zhuǎn)移切換

注釋:mongodb中每個(gè)實(shí)例都對(duì)應(yīng)一個(gè)進(jìn)程,故結(jié)束進(jìn)程則節(jié)點(diǎn)停運(yùn),以此達(dá)到模擬故障。

查看進(jìn)程

[root@cent mongodb]# ps aux | grep mongod
root      74970  0.4  3.1 1580728 59392 ?       Sl   21:47   0:15 mongod -f /etcmongod.conf
root      75510  0.4  2.8 1465952 53984 ?       Sl   22:16   0:07 mongod -f /etcmongod2.conf
root      75538  0.4  2.9 1501348 54496 ?       Sl   22:17   0:07 mongod -f /etcmongod3.conf
root      75566  0.3  2.7 1444652 52144 ?       Sl   22:17   0:06 mongod -f /etcmongod4.conf

結(jié)束primary(27017)

[root@cent mongodb]# kill -9 74970
[root@cent mongodb]# ps aux | grep mongod
root      75510  0.4  2.9 1465952 55016 ?       Sl   22:16   0:10 mongod -f /etcmongod2.conf
root      75538  0.4  2.9 1493152 55340 ?       Sl   22:17   0:10 mongod -f /etcmongod3.conf
root      75566  0.3  2.7 1444652 52168 ?       Sl   22:17   0:08 mongod -f /etcmongod4.conf

進(jìn)入27018查驗(yàn)

ARY> rs.status()

yang:SECOND

"_id" : 0,
        "name" : "192.168.137.11:27017",
        "health" : 0,                              //健康值為0,顯示原本的primary已經(jīng)失效

"_id" : 2,
            "name" : "192.168.137.11:27019",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",                 //而這一臺(tái)服務(wù)器搶到了primary

手動(dòng)切換

手動(dòng)切換需要在primary下進(jìn)行操作,現(xiàn)在primary是27018

暫停三十秒不進(jìn)行選舉

[root@cent mongodb]# mongo --port 27019

yang:PRIMARY> rs.freeze(30)
{
    "ok" : 0,
    "errmsg" : "cannot freeze node when primary or running for election. state: Primary",
    "code" : 95,
    "codeName" : "NotSecondary",
    "operationTime" : Timestamp(1536851239, 1),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1536851239, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
}

交出主節(jié)點(diǎn)位置,維持從節(jié)點(diǎn)狀態(tài)不少于60秒,等待30秒使主節(jié)點(diǎn)和從節(jié)點(diǎn)日至同步。

yang:PRIMARY> rs.stepDown(60,30)
2018-09-13T23:07:48.655+0800 E QUERY    [thread1] 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:1341:12
@(shell):1:1
2018-09-13T23:07:48.658+0800 I NETWORK  [thread1] trying reconnect to 127.0.0.1:27019 (127.0.0.1) failed
2018-09-13T23:07:48.659+0800 I NETWORK  [thread1] reconnect 127.0.0.1:27019 (127.0.0.1) ok
yang:SECONDARY>                       //可以看到它直接變成了從


節(jié)點(diǎn)選舉

 復(fù)制集通過replSetInitiate命令(或mongo shell的rs.initiate())進(jìn)行初始化,初始化后各個(gè)成員間開始發(fā)送心跳消息,并發(fā)起Priamry選舉操作,獲得『大多數(shù)』成員投票支持的節(jié)點(diǎn),會(huì)成為Primary,其余節(jié)點(diǎn)成為Secondary。

回到上面定義復(fù)制集那一步,這里把語句稍作改變,添加優(yōu)先值與仲裁節(jié)點(diǎn)。

> cfg={"_id":"yang","members":[{"_id":0,"host":"192.168.137.11:27017","priority":100},{"_id":1,"host":"192.168.137.11:27018","priority":100},{"_id":2,"host":"192.168.137.11:27019","priority":0},{"_id":3,"host":"192.168.137.11:27020","arbiterOnly":true}]}
{
    "_id" : "yang",
    "members" : [
        {
            "_id" : 0,
            "host" : "192.168.137.11:27017",
            "priority" : 100                          //優(yōu)先級(jí)
        },
        {
            "_id" : 1,
            "host" : "192.168.137.11:27018",
            "priority" : 100                       //優(yōu)先級(jí)
        },
        {
            "_id" : 2,
            "host" : "192.168.137.11:27019",
            "priority" : 0                       //優(yōu)先級(jí)
        },
        {
            "_id" : 3,
            "host" : "192.168.137.11:27020",
            "arbiterOnly" : true
        }
    ]
}


啟動(dòng)cfg

> rs.initiate(cfg)
{
    "ok" : 1,
    "operationTime" : Timestamp(1536852325, 1),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1536852325, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
}


查看各實(shí)例關(guān)系與日志

yang:OTHER> rs.isMaster()
{
    "hosts" : [                                    //標(biāo)準(zhǔn)節(jié)點(diǎn)
        "192.168.137.11:27017",
        "192.168.137.11:27018"
    ],
    "passives" : [                             //被動(dòng)節(jié)點(diǎn)
        "192.168.137.11:27019"
    ],
    "arbiters" : [                               //動(dòng)態(tài)節(jié)點(diǎn)
        "192.168.137.11:27020"
    ],
    "setName" : "yang",
    "setVersion" : 1,
    "ismaster" : false,
    "secondary" : true,
    "me" : "192.168.137.11:27017",
    "lastWrite" : {
        "opTime" : {
            "ts" : Timestamp(1536852325, 1),
            "t" : NumberLong(-1)
        },
        "lastWriteDate" : ISODate("2018-09-13T15:25:25Z")
    },
    "maxBsonObjectSize" : 16777216,
    "maxMessageSizeBytes" : 48000000,
    "maxWriteBatchSize" : 100000,
    "localTime" : ISODate("2018-09-13T15:25:29.008Z"),
    "logicalSessionTimeoutMinutes" : 30,
    "minWireVersion" : 0,
    "maxWireVersion" : 6,
    "readOnly" : false,
    "ok" : 1,
    "operationTime" : Timestamp(1536852325, 1),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1536852325, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
}


添加一個(gè)集合,做一些基本操作,讓日志更新。

yang:SECONDARY> use mood
switched to db mood
yang:PRIMARY> db.info.insert({"id":1,"name":"mark"})
WriteResult({ "nInserted" : 1 })
yang:PRIMARY> db.info.find()
{ "_id" : ObjectId("5b9a8244b4360d88324a69fc"), "id" : 1, "name" : "mark" }
yang:PRIMARY> db.info.update({"id":1},{$set:{"name":"zhangsan"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
yang:PRIMARY> db.info.find()
{ "_id" : ObjectId("5b9a8244b4360d88324a69fc"), "id" : 1, "name" : "zhangsan" }


查看日志

yang:PRIMARY> use local
switched to db local
yang:PRIMARY> show collections
me
oplog.rs
replset.election
replset.minvalid
startup_log
system.replset
system.rollback.id
yang:PRIMARY> db.oplog.rs.find()      //數(shù)量很多

搶占測試

首先把主節(jié)點(diǎn)關(guān)閉

[root@cent ~]# mongod -f /etc/mongod.conf --shutdown
killing process with pid: 74970

登陸下一個(gè)節(jié)點(diǎn)27018,發(fā)現(xiàn)它變成了主節(jié)點(diǎn),繼續(xù)關(guān)閉27018,兩個(gè)標(biāo)準(zhǔn)節(jié)點(diǎn)都沒有了,實(shí)驗(yàn)被動(dòng)節(jié)點(diǎn)時(shí)候會(huì)變?yōu)橹鞴?jié)點(diǎn)。

登陸被動(dòng)節(jié)點(diǎn)

yang:SECONDARY> rs.status()

"_id" : 2,
            "name" : "192.168.137.11:27019",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",

發(fā)現(xiàn)并不會(huì),啟動(dòng)前面關(guān)閉的兩個(gè)節(jié)點(diǎn)。

[root@cent ~]# mongod -f /etc/mongod2.conf
about to fork child process, waiting until server is ready for connections.
forked process: 77132
child process started successfully, parent exiting
[root@cent ~]# mongod -f /etc/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 77216
child process started successfully, parent exiting

其中27018又變?yōu)橹鞴?jié)點(diǎn)了。

[root@cent ~]# mongo --port 27018

yang:PRIMARY>

可見只有標(biāo)準(zhǔn)節(jié)點(diǎn)會(huì)互相搶占主節(jié)點(diǎn)。

 

數(shù)據(jù)權(quán)限

Primary與Secondary之間通過oplog來同步數(shù)據(jù),Primary上的寫操作完成后,會(huì)向特殊的local.oplog.rs特殊集合寫入一條oplog,Secondary不斷的從Primary取新的oplog并應(yīng)用。

因oplog的數(shù)據(jù)會(huì)不斷增加,local.oplog.rs被設(shè)置成為一個(gè)capped集合,當(dāng)容量達(dá)到配置上限時(shí),會(huì)將最舊的數(shù)據(jù)刪除掉。另外考慮到oplog在Secondary上可能重復(fù)應(yīng)用,oplog必須具有冪等性,即重復(fù)應(yīng)用也會(huì)得到相同的結(jié)果。


只有主節(jié)點(diǎn)有權(quán)限查看數(shù)據(jù),從節(jié)點(diǎn)是沒有權(quán)限的,會(huì)出現(xiàn)以下回饋。

yang:SECONDARY> show dbs
2018-09-13T23:58:34.112+0800 E QUERY    [thread1] Error: listDatabases failed:{
    "operationTime" : Timestamp(1536854312, 1),
    "ok" : 0,
    "errmsg" : "not master and slaveOk=false",
    "code" : 13435,
    "codeName" : "NotMasterNoSlaveOk",
    "$clusterTime" : {
        "clusterTime" : Timestamp(1536854312, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:65:1
shellHelper.show@src/mongo/shell/utils.js:849:19
shellHelper@src/mongo/shell/utils.js:739:15
@(shellhelp2):1:1


使用如下命令在從節(jié)點(diǎn)上查看信息

yang:SECONDARY> rs.slaveOk()
yang:SECONDARY> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
mood    0.000GB

仲裁節(jié)點(diǎn)不會(huì)復(fù)制主節(jié)點(diǎn)中的復(fù)制信息

可以看到只有兩臺(tái)節(jié)點(diǎn)又?jǐn)?shù)據(jù)日志,27020仲裁節(jié)點(diǎn)是沒有的

yang:SECONDARY> rs.printSlaveReplicationInfo()
source: 192.168.137.11:27017
    syncedTo: Fri Sep 14 2018 00:03:52 GMT+0800 (CST)
    0 secs (0 hrs) behind the primary 
source: 192.168.137.11:27019
    syncedTo: Fri Sep 14 2018 00:03:52 GMT+0800 (CST)
    0 secs (0 hrs) behind the primary

 

獲取復(fù)制集的幫助

yang:SECONDARY> rs.help()         //獲取副本集相關(guān)的幫助命令
    rs.status()                                { replSetGetStatus : 1 } checks repl set status
    rs.initiate()                              { replSetInitiate : null } initiates set with default settings
    rs.initiate(cfg)                           { replSetInitiate : cfg } initiates set with configuration cfg
    rs.conf()                                  get the current configuration object from local.system.replset
    rs.reconfig(cfg)                           updates the configuration of a running replica set with cfg (disconnects)
    rs.add(hostportstr)                        add a new member to the set with default attributes (disconnects)
    rs.add(membercfgobj)                       add a new member to the set with extra attributes (disconnects)
    rs.addArb(hostportstr)                     add a new member which is arbiterOnly:true (disconnects)
    rs.stepDown([stepdownSecs, catchUpSecs])   step down as primary (disconnects)
    rs.syncFrom(hostportstr)                   make a secondary sync from the given member
    rs.freeze(secs)                            make a node ineligible to become primary for the time specified
    rs.remove(hostportstr)                     remove a host from the replica set (disconnects)
    rs.slaveOk()                               allow queries on secondary nodes

    rs.printReplicationInfo()                  check oplog size and time range
    rs.printSlaveReplicationInfo()             check replica set members and replication lag
    db.isMaster()                              check who is primary

    reconfiguration helpers disconnect from the database so the shell will display
    an error, even if the command succeeds.

 

總結(jié)一下各節(jié)點(diǎn)的屬性:

Arbiter

Arbiter節(jié)點(diǎn)只參與投票,不能被選為Primary,并且不從Primary同步數(shù)據(jù)。

比如你部署了一個(gè)2個(gè)節(jié)點(diǎn)的復(fù)制集,1個(gè)Primary,1個(gè)Secondary,任意節(jié)點(diǎn)宕機(jī),復(fù)制集將不能提供服務(wù)了(無法選出Primary),這時(shí)可以給復(fù)制集添加一個(gè)Arbiter節(jié)點(diǎn),即使有節(jié)點(diǎn)宕機(jī),仍能選出Primary。

Arbiter本身不存儲(chǔ)數(shù)據(jù),是非常輕量級(jí)的服務(wù),當(dāng)復(fù)制集成員為偶數(shù)時(shí),最好加入一個(gè)Arbiter節(jié)點(diǎn),以提升復(fù)制集可用性。

Priority0

Priority0節(jié)點(diǎn)的選舉優(yōu)先級(jí)為0,不會(huì)被選舉為Primary

比如你跨機(jī)房A、B部署了一個(gè)復(fù)制集,并且想指定Primary必須在A機(jī)房,這時(shí)可以將B機(jī)房的復(fù)制集成員Priority設(shè)置為0,這樣Primary就一定會(huì)是A機(jī)房的成員。(注意:如果這樣部署,最好將『大多數(shù)』節(jié)點(diǎn)部署在A機(jī)房,否則網(wǎng)絡(luò)分區(qū)時(shí)可能無法選出Primary)

Vote0

Mongodb 3.0里,復(fù)制集成員最多50個(gè),參與Primary選舉投票的成員最多7個(gè),其他成員(Vote0)的vote屬性必須設(shè)置為0,即不參與投票。

Hidden

Hidden節(jié)點(diǎn)不能被選為主(Priority為0),并且對(duì)Driver不可見。

因Hidden節(jié)點(diǎn)不會(huì)接受Driver的請(qǐng)求,可使用Hidden節(jié)點(diǎn)做一些數(shù)據(jù)備份、離線計(jì)算的任務(wù),不會(huì)影響復(fù)制集的服務(wù)。

Delayed

Delayed節(jié)點(diǎn)必須是Hidden節(jié)點(diǎn),并且其數(shù)據(jù)落后與Primary一段時(shí)間(可配置,比如1個(gè)小時(shí))。

因Delayed節(jié)點(diǎn)的數(shù)據(jù)比Primary落后一段時(shí)間,當(dāng)錯(cuò)誤或者無效的數(shù)據(jù)寫入Primary時(shí),可通過Delayed節(jié)點(diǎn)的數(shù)據(jù)來恢復(fù)到之前的時(shí)間點(diǎn)。


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI