您好,登录后才能下订单哦!
前言介绍
复制的原理:
复制操作是基于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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。