91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

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

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

MongoDB復制集選舉原理及管理詳解

發布時間:2020-02-28 08:34:28 來源:網絡 閱讀:1281 作者:明月幽谷 欄目:MongoDB數據庫

MongoDB復制集的節點是通過選舉產生主節點的,下面將介紹復制集節點間選舉的過程

  • MongoDB復制的原理

復制是基于操作日志oplog,相當于MySQL中的二進制日志,只記錄發生改變的記錄。復制是將主節點的oplog日志同步并應用到其他從節點過程

  • MongoDB選舉的原理

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

(1)只有標準節點可能被選舉為主(primary)節點,有選舉權;被動節點有完整副本,只能作為復制集保存,不可能成為主節點,沒有選舉權;仲裁節點不存放數據,只負責投票選舉,不可能成為主節點,不存放數據,依然沒有選舉權

(2)標準節點與被動節點的區別:priority值高者是標準節點,低者則為被動節點

(3)選舉規則是票數高者獲勝,priority是優先權為0~1000的值,相當于額外增加0~1000的票數。選舉結果:票數高者獲勝;若票數相同,數據新者獲勝

  • MongoDB復制集節點間選舉如圖所示

MongoDB復制集選舉原理及管理詳解

下面通過實例來演示MongoDB復制集節點間的選舉原理

  • 在一臺CentOS7主機上使用yum在線安裝Mongodb,并創建多實例,進行部署MongoDB復制集

首先配置網絡YUM源,baseurl(下載路徑)指定為mongodb官網提供的yum倉庫

vim /etc/yum.repos.d/mongodb.repo

[mongodb-org]

name=MongoDB Repository

baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.6/x86_64/???????????? #指定獲得下載的路徑

gpgcheck=1???????????????????? #表示對從這個源下載的rpm包進行校驗

enabled=1?????????????????? #表示啟用這個源。

gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc

重新加載yum源,并使用yum命令下載安裝mongodb

yum list

yum -y install mongodb-org

準備4個實例,設置兩個標準節點, 一個被動節點和一個仲裁節點???

  • 創建數據文件和日志文件存儲路徑,并賦予權限

    [root@localhost ~]# mkdir -p /data/mongodb{2,3,4}
    [root@localhost ~]# mkdir /data/logs
    [root@localhost ~]# touch /data/logs/mongodb{2,3,4}.log
    [root@localhost ~]# chmod 777 /data/logs/mongodb*
    [root@localhost ~]# ll /data/logs/
    總用量 0
    -rwxrwxrwx. 1 root root 0 9月? 15 22:31 mongodb2.log
    -rwxrwxrwx. 1 root root 0 9月? 15 22:31 mongodb3.log
    -rwxrwxrwx. 1 root root 0 9月? 15 22:31 mongodb4.log

編輯4個MongoDB實例的配置文件

  • 先編輯yum安裝的默認實例的配置文件/etc/mongod.conf,指定監聽IP,端口默認為27017,開啟replication參數配置,replSetName:true(自定義)

[root@localhost ~]# vim /etc/mongod.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: /var/log/mongodb/mongod.log

# Where and how to store data.
storage:
? dbPath: /var/lib/mongo
? journal:
??? enabled: true
#? engine:
#? mmapv1:
#? wiredTiger:

# how the process runs
processManagement:
? fork: true? # fork and run in background
? pidFilePath: /var/run/mongodb/mongod.pid? # location of pidfile
? timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:?

? port: 27017??????????????????? #默認端口??????????
? bindIp: 0.0.0.0???????????? #監聽任意地址

#security:

#operationProfiling:

replication:?????????????????? #去掉前面的“#”注釋,開啟該參數設置
replSetName: true????????? #設置復制集名稱

  • 復制配置文件給其他實例,并將mongodb2.conf 中的port參數配置為27018,mongod3.conf中的port參數配置為27019,mongod4.conf中的port參數配置為27020。 同樣也將dbpath和logpath參數修改為對應的路徑值

cp? /etc/mongod.conf /etc/mongod2.conf

cp /etc/mongod2.conf /etc/mongod3.conf

cp /etc/mongod2.conf /etc/mongod4.conf

  • 實例2的配置文件mongodb2.conf 修改

vim /etc/mongod2.conf

systemLog:

? destination: file

? logAppend: true

path: /data/logs/mongodb2.log???

storage:

dbPath: /data/mongodb/mongodb2

? journal:

enabled: true

port: 27018?

bindIp: 0.0.0.0? # Listen to local interface only, comment to listen on all interfaces.

#security:

#operationProfiling:

replication:
replSetName: true

  • 實例3的配置文件mongodb3.conf 修改

vim /etc/mongod3.conf

systemLog:

? destination: file

? logAppend: true

path: /data/logs/mongodb3.log???

storage:

dbPath: /data/mongodb/mongodb3?

? journal:

enabled: true

port: 27019?

bindIp: 0.0.0.0? # Listen to local interface only, comment to listen on all interfaces.

#security:

#operationProfiling:

replication:
replSetName: true

  • 實例4的配置文件mongodb4.conf 修改

vim /etc/mongod4.conf

systemLog:

? destination: file

? logAppend: true

path: /data/logs/mongodb4.log

storage:

dbPath: /data/mongodb/mongodb4

? journal:

enabled: true

port: 27020

bindIp: 0.0.0.0? # Listen to local interface only, comment to listen on all interfaces.

#security:

#operationProfiling:

replication:
replSetName: true

啟動mongodb各實例

[root@localhost ~]# mongod -f /etc/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 93576
child process started successfully, parent exiting
[root@localhost ~]# mongod -f /etc/mongod2.conf
about to fork child process, waiting until server is ready for connections.
forked process: 93608
child process started successfully, parent exiting
[root@localhost ~]# mongod -f /etc/mongod3.conf
about to fork child process, waiting until server is ready for connections.
forked process: 93636
child process started successfully, parent exiting
[root@localhost ~]# mongod -f /etc/mongod4.conf
about to fork child process, waiting until server is ready for connections.
forked process: 93664
child process started successfully, parent exiting
[root@localhost ~]# netstat -antp | grep mongod??????????????????????? //查看mongodb進程狀態
tcp??????? 0????? 0 0.0.0.0:27019?????????? 0.0.0.0:*?????????????? LISTEN????? 93636/mongod??????
tcp??????? 0????? 0 0.0.0.0:27020?????????? 0.0.0.0:*?????????????? LISTEN????? 93664/mongod??????
tcp??????? 0????? 0 0.0.0.0:27017?????????? 0.0.0.0:*?????????????? LISTEN????? 93576/mongod??????
tcp??????? 0????? 0 0.0.0.0:27018?????????? 0.0.0.0:*?????????????? LISTEN????? 93608/mongod

配置復制集的優先級

  • 登錄默認實例 mongo,配置4個節點 MongoDB 復制集,設置兩個標準節點,一個被動節點和一個仲裁節點,

  • 根據優先級確定節點: 優先級為 100的為標準節點,端口號為 27017和27018? ,優先級為0 的為被動節點,端口號為27019;仲裁節點為27020

[root@localhost ~]# mongo
MongoDB shell version v3.6.7
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.7

> cfg={"_id":"true","members":[{"_id":0,"host":"192.168.195.137:27017","priority":100},???????????????????????

{"_id":1,"host":"192.168.195.137:27018","priority":100},{"_id":2,"host":"192.168.195.137:27019","priority":0},{"_id":3,"host":"192.168.195.137:27020","arbiterOnly":true}]}???????????????????????
{
??? "_id" : "true",
??? "members" : [
??????? {
??????????? "_id" : 0,
??????????? "host" : "192.168.195.137:27017",??????????????? #標準節點1,優先級為100
??????????? "priority" : 100
??????? },
??????? {
??????????? "_id" : 1,
??????????? "host" : "192.168.195.137:27018",?????????????? #標準節點2,優先級為100
??????????? "priority" : 100
??????? },
??????? {
??????????? "_id" : 2,
??????????? "host" : "192.168.195.137:27019",????????????? #被動節點,優先級為0
??????????? "priority" : 0
??????? },
??????? {
??????????? "_id" : 3,
??????????? "host" : "192.168.195.137:27020",??????????????? #仲裁節點
??????????? "arbiterOnly" : true

> rs.initiate(cfg)?????????????????????????????? #初始化配置
{
??? "ok" : 1,
??? "operationTime" : Timestamp(1537077618, 1),
??? "$clusterTime" : {
??????? "clusterTime" : Timestamp(1537077618, 1),
??????? "signature" : {
??????????? "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
??????????? "keyId" : NumberLong(0)
??????? }
??? }

  • 使用命令 rs.isMaster()? 查看各節點身份

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

  • 在主節點上進行增,刪,改。查操作

true:PRIMARY> use kfc
switched to db kfc
true:PRIMARY> db.info.insert({"id":1,"name":"tom"})
WriteResult({ "nInserted" : 1 })
true:PRIMARY> db.info.insert({"id":2,"name":"jack"})
WriteResult({ "nInserted" : 1 })
true:PRIMARY> db.info.find()
{ "_id" : ObjectId("5b9df3ff690f4b20fa330b18"), "id" : 1, "name" : "tom" }
{ "_id" : ObjectId("5b9df40f690f4b20fa330b19"), "id" : 2, "name" : "jack

true:PRIMARY> db.info.update({"id":2},{$set:{"name":"lucy"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
true:PRIMARY> db.info.remove({"id":1})
WriteResult({ "nRemoved" : 1 })

?

  • 查看主節點的oplog日志記錄所有操作 ,在默認數據庫 local 中的oplog.rs?? 查看


true:PRIMARY> use local
switched to db local
true:PRIMARY> show tables
me
oplog.rs
replset.election
replset.minvalid
startup_log
system.replset
system.rollback.id
true:PRIMARY> db.oplog.rs.find()?????????????????? #查看日志記錄所有操作????

............?????????????????????????????????????????????????????? # 通過日志記錄,可以找到剛才的操作信息

{ "ts" : Timestamp(1537078271, 2), "t" : NumberLong(1), "h" : NumberLong("-5529983416084904509"), "v" : 2, "op" : "c", "ns" : "kfc.$cmd", "ui" : UUID("2de2277f-df99-4fb2-96ef-164b59dfc768"), "wall" : ISODate("2018-09-16T06:11:11.072Z"), "o" : { "create" : "info", "idIndex" : { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "kfc.info" } } }
{ "ts" : Timestamp(1537078271, 3), "t" : NumberLong(1), "h" : NumberLong("-1436300260967761649"), "v" : 2, "op" : "i", "ns" : "kfc.info", "ui" : UUID("2de2277f-df99-4fb2-96ef-164b59dfc768"), "wall" : ISODate("2018-09-16T06:11:11.072Z"), "o" : { "_id" : ObjectId("5b9df3ff690f4b20fa330b18"), "id" : 1, "name" : "tom" } }
{ "ts" : Timestamp(1537078287, 1), "t" : NumberLong(1), "h" : NumberLong("9052955074674132871"), "v" : 2, "op" : "i", "ns" : "kfc.info", "ui" : UUID("2de2277f-df99-4fb2-96ef-164b59dfc768"), "wall" : ISODate("2018-09-16T06:11:27.562Z"), "o" : { "_id" : ObjectId("5b9df40f690f4b20fa330b19"), "id" : 2, "name" : "jack" } }

...............

{ "ts" : Timestamp(1537078543, 1), "t" : NumberLong(1), "h" : NumberLong("-5120962218610090442"), "v" : 2, "op" : "u", "ns" : "kfc.info", "ui" : UUID("2de2277f-df99-4fb2-96ef-164b59dfc768"), "o2" : { "_id" : ObjectId("5b9df40f690f4b20fa330b19") }, "wall" : ISODate("2018-09-16T06:15:43.494Z"), "o" : { "$v" : 1, "$set" : { "name" : "lucy" } } }

模擬標準節點1故障

  • 如果主節點出現故障,另一個標準節點會選舉成為新的主節點。

[root@localhost ~]# mongod -f /etc/mongod.conf --shutdown??????????? #關閉主節點服務
killing process with pid: 52986
[root@localhost ~]# mongo --port 27018?????????????????????????????? #登錄另一個標準節點端口 27018

MongoDB shell version v3.6.7
connecting to: mongodb://127.0.0.1:27018/
MongoDB server version: 3.6.7

true:PRIMARY> rs.status()????????????????????????????? #查看狀態,可以看到這臺標準節點已經選舉為主節點

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

{
??????????? "_id" : 1,
??????????? "name" : "192.168.195.137:27018",
??????????? "health" : 1,??????????????
??????????? "state" : 1,
??????????? "stateStr" : "PRIMARY",?????????????????????????? #此時另一臺標準節點被選舉為主節點,端口為 27018
??????????? "uptime" : 3192,
??????????? "optime" : {
??????????????? "ts" : Timestamp(1537080552, 1),
??????????????? "t" : NumberLong(2)
??????????? },

模擬標準節點2故障

  • 將標準節點服務全部關閉,查看被動節點是否會被選舉為主節點

[root@localhost ~]# mongod -f /etc/mongod2.conf --shutdown???????? #關閉第二個標準節點服務
killing process with pid: 53018
[root@localhost ~]# mongo --port 27019?????????????????????????? #進入第三個被動節點實例
MongoDB shell version v3.6.7
connecting to: mongodb://127.0.0.1:27019/
MongoDB server version: 3.6.7

true:SECONDARY> rs.status()??????????????????????????? #查看復制集狀態信息??????????????????????

..............

"members" : [
??????? {
??????????? "_id" : 0,
??????????? "name" : "192.168.195.137:27017",
??????????? "health" : 0,
??????????? "state" : 8,
??????????? "stateStr" : "(not reachable/healthy)",
??????????? "uptime" : 0,
??????????? "optime" : {
??????????????? "ts" : Timestamp(0, 0),
??????????????? "t" : NumberLong(-1)
??????????? },
??????????? "optimeDurable" : {
??????????????? "ts" : Timestamp(0, 0),
??????????????? "t" : NumberLong(-1)
??????????? },

.................

{
??????????? "_id" : 1,
??????????? "name" : "192.168.195.137:27018",
??????????? "health" : 0,
??????????? "state" : 8,
??????????? "stateStr" : "(not reachable/healthy)",
??????????? "uptime" : 0,
??????????? "optime" : {
??????????????? "ts" : Timestamp(0, 0),
??????????????? "t" : NumberLong(-1)
??????????? },
??????????? "optimeDurable" : {
??????????????? "ts" : Timestamp(0, 0),
??????????????? "t" : NumberLong(-1)
??????????? },

..................

{
??????????? "_id" : 2,
??????????? "name" : "192.168.195.137:27019",
??????????? "health" : 1,
??????????? "state" : 2,
??????????? "stateStr" : "SECONDARY",????????????????????????? #被動節點并沒有被選舉為主節點,說明被動節點不可能成為活躍節點
??????????? "uptime" : 3972,
??????????? "optime" : {
??????????????? "ts" : Timestamp(1537081303, 1),
??????????????? "t" : NumberLong(2)
??????????? },

..................

{
??????? "_id" : 3,
??????? "name" : "192.168.195.137:27020",
??????? "health" : 1,
??????? "state" : 7,
??????? "stateStr" : "ARBITER",
??????? "uptime" : 3722,

另外我們可以通過啟動標準節點的先后順序,實現人為指定主節點,默認誰先啟動,誰就是主節點。

允許從節點讀取數據

  • 默認MongoDB復制集的從節點不能讀取數據,可以使用rs.slaveOk()命令允許能夠在從節點讀取數據

  • 重新啟動兩個標準節點

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

  • 進入復制集的其中一個從節點,配置其允許讀取數據


[root@localhost ~]# mongo --port 27018
MongoDB shell version v3.6.7
connecting to: mongodb://127.0.0.1:27018/
MongoDB server version: 3.6.7

true:SECONDARY> rs.slaveOk()?????????????????????????? #允許默認從節點讀取數據
true:SECONDARY> show dbs????????????? #讀取成功
admin?? 0.000GB
config? 0.000GB
kfc???? 0.000GB
local?? 0.000GB

查看復制狀態信息

  • 可以使用rs.printReplicationInfo()和rs.printSlaveReplicationInfo()命令來查看復制集狀態

true:SECONDARY> rs.printReplicationInfo()???????? #查看日志文件能夠使用的大小 默認oplog大小會占用64位實例5%的可用磁盤空間
configured oplog size:? 990MB
log length start to end: 5033secs (1.4hrs)
oplog first event time:? Sun Sep 16 2018 14:00:18 GMT+0800 (CST)
oplog last event time:?? Sun Sep 16 2018 15:24:11 GMT+0800 (CST)
now:???????????????????? Sun Sep 16 2018 15:24:13 GMT+0800 (CST)
true:SECONDARY> rs.printSlaveReplicationInfo()??????????????? #查看節點????????
source: 192.168.195.137:27018
??? syncedTo: Sun Sep 16 2018 15:24:21 GMT+0800 (CST)
??? 0 secs (0 hrs) behind the primary
source: 192.168.195.137:27019
??? syncedTo: Sun Sep 16 2018 15:24:21 GMT+0800 (CST)
??? 0 secs (0 hrs) behind the primary

會發現仲裁節點并不具備數據復制

更改oplog大小

  • oplog即operations log簡寫,存儲在local數據庫中。oplog中新操作會自動替換舊的操作,以保證oplog不會超過預設的大小。默認情況下,oplog大小會占用64位的實例5%的可用磁盤

  • 在MongoDB復制的過程中,主節點應用業務操作修改到數據庫中,然后記錄這些操作到oplog中,從節點復制這些oplog,然后應用這些修改。這些操作是異步的。如果從節點的操作已經被主節點落下很遠,oplog日志在從節點還沒執行完,oplog可能已經輪滾一圈了,從節點跟不上同步,復制就會停下,從節點需要重新做完整的同步,為了避免此種情況,盡量保證主節點的oplog足夠大,能夠存放相當長時間的操作記錄

  • (1)關閉mongodb

true:PRIMARY> use admin
switched to db admin
true:PRIMARY> db.shutdownServer()

  • (2)修改配置文件,注銷掉replication相關設置,并修改端口號,目的使其暫時脫離復制集成為一個獨立的單體,

vim /etc/mongod.conf

port: 27027

#replication:

# replSetName: true

  • (3)單實例模式啟動,并將之前的oplog備份一下

mongod -f /etc/mongod.conf

mongodump --port=27028 -d local -c oplog.rs -o /opt/

  • (4)進入實例中,刪除掉原來的oplog.rs,使用db.runCommand命令重新創建oplog.rs,并更改oplog大小

[root@localhost logs]# mongo --port 27027

> use local
> db.oplog.rs.drop()
> db.runCommand( { create: "oplog.rs", capped: true, size: (2 * 1024 * 1024 * 1024) } )

AI

西藏| 太仓市| 岳池县| 肥西县| 凌云县| 津南区| 灯塔市| 枣强县| 南平市| 尚志市| 邳州市| 嘉兴市| 神池县| 石泉县| 阳新县| 桦川县| 宁南县| 明水县| 长顺县| 深泽县| 双峰县| 永昌县| 兴山县| 乐山市| 奈曼旗| 和龙市| 民权县| 武定县| 留坝县| 兴隆县| 额济纳旗| 肇东市| 安溪县| 桂东县| 石楼县| 大同县| 通海县| 巴林左旗| 新丰县| 湄潭县| 获嘉县|