MongoDB複製集的選舉原理詳解以及複製集管理簡介

前言介紹mysql

複製的原理:sql

複製操做是基於oplog,相似mysql中的bin-log,只記錄發生改變的記錄。vim

選舉的原理:服務器

節點分爲:標準節點、被動節點和仲裁節點。app

  • 標準節點(priority值高):只有標準節點纔可成爲primary;ide

  • 被動節點(priority值低):被動節點只能是secondary;spa

  • 仲裁節點:不能複製數據、不可成爲活躍點、只有選舉權;日誌

選舉結果:票數高者獲勝;若票數相同,數據新者獲勝server

1、複製集選舉實驗介紹部署

實驗步驟

  1. 查看oplog日誌

  2. 配置複製集的優先級

  3. 模擬主節點故障

  4. 模擬全部標準節點故障

2、複製集選舉實驗內容

---------------------------查看oplog日誌-------------------------

> use school
switched to db school
> db.info.insert({"id":1,"name":"tom"})
WriteResult({ "nInserted" : 1 })
> db.info.find()
{ "_id" : ObjectId("5b9a0873692de658bd931c64"), "id" : 1, "name" : "tom" }
> use local
switched to db local
> show collections
me
oplog.rs
…
> db.oplog.rs.find()                
{ "ts" : Timestamp(1536723445, 3), … : { "create" : "transactions", "idIndex" : { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "config.transactions" } } }
{ "ts" : Timestamp(1536723445, 5), …: { "create" : "system.keys", "idIndex" : { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "admin.system.keys" } } }

------------------------配置複製集的優先級----------------------------

cfg={"_id":"yandada","members": [{"_id":0,"host":"192.168.218.149:27017","priority":100},{"_id":1,"host":"192.168.218.149:27018","priority":100},{"_id":2,"host":"192.168.218.149:27019","priority":0},{"_id":3,"host":"192.168.218.149:27020","arbiterOnly":true}]}

rs.reconfig(cfg)

        { "ok" : 1 }              //顯示OK:1時表示節點配置成功

rs.status()                    //查看狀態信息

rs.isMaster()              //查看節點信息

{                                           //顯示信息以下
     "hosts" : [
         "192.168.218.149:27017",
         "192.168.218.149:27018"
     ],
     "passives" : [
         "192.168.218.149:27019"
     ],
     "arbiters" : [
         "192.168.218.149:27020"
     ]

------------------------模擬主節點故障----------------------------

關閉主節點服務器

yandada:PRIMARY> use admin                #進入admin集合才能進行下一步操做
switched to db admin
yandada:PRIMARY> db.shutdownServer()               #關閉服務器
server should be down…

以上操做等同於[root@yandada3 ~]# mongod -f /etc/mongod.conf --shutdown

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

yandada:PRIMARY> rs.status()

查看狀態信息後會發現MongoDB複製集會選舉第二個標準節點做爲主節點

---------------------------模擬全部標準節點故障------------------------

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

[root@yandada3 ~]# mongo --port 27019

yandada:SECONDARY> rs.status()

查看查看狀態信息後會發現無primary節點,被動節點沒法成爲主節點

3、複製集管理簡介

1.配置容許從節點讀取數據

yandada:SECONDARY> rs.slaveOk()

2.查看複製集狀態信息

rs.help()

yandada:PRIMARY> rs.printReplicationInfo()
configured oplog size:   990MB               #oplog存儲大小爲990MB
log length start to end: 101403secs (28.17hrs)
oplog first event time:  Wed Sep 12 2018 11:37:13 GMT+0800 (CST)
oplog last event time:   Thu Sep 13 2018 15:47:16 GMT+0800 (CST)
now:                     Thu Sep 13 2018 15:47:17 GMT+0800 (CST)
yandada:PRIMARY> rs.printSlaveReplicationInfo()
source: 192.168.218.149:27018
     syncedTo: Thu Sep 13 2018 15:47:26 GMT+0800 (CST)
     0 secs (0 hrs) behind the primary
source: 192.168.218.149:27019
     syncedTo: Thu Sep 13 2018 15:47:26 GMT+0800 (CST)
     0 secs (0 hrs) behind the primary

3.更改oplog大小

         1.第一步,退出複製集

yandada:PRIMARY> use admin             
switched to db admin
yandada:PRIMARY> db.shutdownServer()             
server should be down…

         2.第二步,更改端口(複製集中含有源端口),關閉配置文件中複製集名稱,啓動mongod

vim /etc/mongod.conf

net:
   port: 2

#replication:
  #   replSetName: yandada

mongod -f /etc/mongod.conf

          3.第三步,更改oplog大小

[root@yandada3 ~]#  mongo --port 27028

> use local
switched to db local
> db.oplog.rs.drop()
true
> db.runCommand({create:"oplog.rs",capped:true,size:(2*2048*2048*2048)})
{ "ok" : 1 }

[root@yandada3 ~]# mongod -f /etc/mongod.conf --shutdown
killing process with pid: 8296
[root@yandada3 ~]# vim /etc/mongod.conf

net:
   port: 27017

replication:
     replSetName: yandada
     oplogSizeMB: 16384

[root@yandada3 ~]# mongod -f /etc/mongod.conf

[root@yandada3 ~]# mongo

yandada:SECONDARY> rs.printReplicationInfo()

configured oplog size: 16384MB             #oplog大小更改成16G

4.認證部署

         1.第一步,建立認證用戶

yandada:PRIMARY> use admin
switched to db admin
yandada:PRIMARY> db.createUser({"user":"root","pwd":"123","roles":["root"]})
Successfully added user: { "user" : "root", "roles" : [ "root" ] }

         2.第二步,編輯認證配置

vim /etc/mongod.conf

     security:

           keyFile: /usr/bin/kgcrskey1

           clusterAuthMode:keyFile              注:須要與上一行齊平

同理,修改mongod[2,3,4]文件

[root@yandada3 ~]# echo "kgcrs key" > /usr/bin/kgcrskey[1,2,3,4]

[root@yandada3 bin]# echo  "kgcrs key" > /usr/bin/kgcrskey1
[root@yandada3 bin]# echo  "kgcrs key" > /usr/bin/kgcrskey2
[root@yandada3 bin]# echo  "kgcrs key" > /usr/bin/kgcrskey3
[root@yandada3 bin]# echo  "kgcrs key" > /usr/bin/kgcrskey4

[root@yandada3 bin]# chmod 600 /usr/bin/kgcrskey{1,2,3,4}

           3.第三步,重啓服務

[root@yandada3 bin]# mongod -f /etc/mongod.conf --shutdown

[root@yandada3 bin]# mongod -f /etc/mongod.conf

同理,重啓mongod[2,3,4]

          4.第四步,查看配置情況

yandada:PRIMARY> show dbs     

    "ok" : 0                  #無權查看

yandada:PRIMARY> use adminswitched to db adminyandada:PRIMARY> db.auth("root","123")1                               #返回值爲1,表示受權成功yandada:PRIMARY> show dbsadmin   0.000GBconfig  0.000GBlocal   0.000GBschool  0.000GB

相關文章
相關標籤/搜索