您好,登錄后才能下訂單哦!
前言介紹
復制的原理:
復制操作是基于oplog,類似mysql中的bin-log,只記錄發生改變的記錄。
選舉的原理:
節點分為:標準節點、被動節點和仲裁節點。
標準節點(priority值高):只有標準節點才可成為primary;
被動節點(priority值低):被動節點只能是secondary;
仲裁節點:不能復制數據、不可成為活躍點、只有選舉權;
選舉結果:票數高者獲勝;若票數相同,數據新者獲勝
一、復制集選舉實驗介紹
實驗步驟
查看oplog日志
配置復制集的優先級
模擬主節點故障
模擬所有標準節點故障
二、復制集選舉實驗內容
---------------------------查看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節點,被動節點無法成為主節點
三、復制集管理簡介
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: yandadamongod -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.confnet:
port: 27017replication:
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 admin
switched to db admin
yandada:PRIMARY> db.auth("root","123")
1 #返回值為1,表示授權成功
yandada:PRIMARY> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
school 0.000GB
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。