成都创新互联网站制作重庆分公司

MongoDB复制集管理(后续)

简介:
复制集:
1:标准节点:
参与primary选举
2:被动节点:
只能成为secend,不参与选举
3:仲裁节点:
负责投票选举,不存放数据

我们提供的服务有:成都网站建设、网站建设、微信公众号开发、网站优化、网站认证、鱼峰ssl等。为上千企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的鱼峰网站制作公司

实验环境:2台标准节点 1台被动节点 1台仲裁点
具体实验步骤:(前半部分实验为上次实验操作)

[root@localhost ~]# mkdir -p /data/MongoDB/mongodb{2,3,4}
[root@localhost ~]# mkdir -p /data/mongodb/logs
[root@localhost ~]# touch /data/mongodb/logs/mongodb{2,3,4}.log
[root@localhost ~]# chmod 777 /data/mongodb/logs/*.log
编辑配置文件
[root@localhost logs]# vim /etc/mongod.conf
开启replication,名字自定义
replication:
replSetName: kgcrs
[root@localhost yum.repos.d]# mongod -f /etc/mongod.conf --shutdown #关闭服务
[root@localhost yum.repos.d]# mongod -f /etc/mongod.conf #开启服务

复制产生四个实例
[root@localhost yum.repos.d]# cp -p /etc/mongod.conf /etc/mongod2.conf
[root@localhost yum.repos.d]# cp -p /etc/mongod.conf /etc/mongod3.conf
[root@localhost yum.repos.d]# cp -p /etc/mongod.conf /etc/mongod4.conf
[root@localhost yum.repos.d]# vim /etc/mongod2.conf
#修改三处
path: /data/mongodb/logs/mongodb2.log
dbPath: /data/mongodb/mongodb2
port: 27018
#将其他几个都修改
[root@localhost yum.repos.d]# cp -p /etc/mongod2.conf /etc/mongod3.conf
[root@localhost yum.repos.d]# vim /etc/mongod3.conf
(还是那三条,序号改好即可)
[root@localhost yum.repos.d]# cp -p /etc/mongod2.conf /etc/mongod4.conf
[root@localhost yum.repos.d]# vim /etc/mongod4.conf
#修改完成后依次启动
[root@localhost yum.repos.d]# mongod -f /etc/mongod2.conf
[root@localhost yum.repos.d]# mongod -f /etc/mongod3.conf
[root@localhost yum.repos.d]# mongod -f /etc/mongod4.conf
#查看端口开启
[root@localhost yum.repos.d]# netstat -ntap
#登录MongoDB
#复制集

cfg={"_id":"kgcrs","members":[{"_id":0,"host":"192.168.120.136:27017","priority":100},{"_id":1,"host":"192.168.120.136:27018","priority":100},{"_id":2,"host":"192.168.120.136:27019","priority":0},{"_id":3,"host":"192.168.120.136:27020","arbiterOnly":true}]}
{
"_id" : "kgcrs",
"members" : [
{
"_id" : 0,
"host" : "192.168.120.136:27017",
"priority" : 100
},
{
"_id" : 1,
"host" : "192.168.120.136:27018",
"priority" : 100
},
{
"_id" : 2,
"host" : "192.168.120.136:27019",
"priority" : 0
},
{
"_id" : 3,
"host" : "192.168.120.136:27020",
"arbiterOnly" : true
}
]
}
#初始化
rs.initiate(cfg)
{
"ok" : 1,
"operationTime" : Timestamp(1536821356, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1536821356, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
#查看身份
kgcrs:PRIMARY> rs.isMaster()
{
"hosts" : [
"192.168.120.136:27017",
"192.168.120.136:27018"
],
"passives" : [
"192.168.120.136:27019"
],
"arbiters" : [
"192.168.120.136:27020"
],
"setName" : "kgcrs",
"setVersion" : 1,
"ismaster" : true,
"secondary" : false,
"primary" : "192.168.120.136:27017",
"me" : "192.168.120.136:27017",
"electionId" : ObjectId("7fffffff0000000000000001"),
"lastWrite" : {
"opTime" : {
"ts" : Timestamp(1536821508, 1),
"t" : NumberLong(1)
},
"lastWriteDate" : ISODate("2018-09-13T06:51:48Z"),
"majorityOpTime" : {
"ts" : Timestamp(1536821508, 1),
"t" : NumberLong(1)
},
"majorityWriteDate" : ISODate("2018-09-13T06:51:48Z")
},
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 100000,
"localTime" : ISODate("2018-09-13T06:51:54.761Z"),
"logicalSessionTimeoutMinutes" : 30,
"minWireVersion" : 0,
"maxWireVersion" : 6,
"readOnly" : false,
"ok" : 1,
"operationTime" : Timestamp(1536821508, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1536821508, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
-------------------------------------日志操作--------------------------------------------------
#创建库kgc,并进入kgc
kgcrs:PRIMARY> use kgc
switched to db kgc
在kgc库中创建t1集合,插入两条信息
kgcrs:PRIMARY> db.t1.insert({"id":1,"name":"jack"})
WriteResult({ "nInserted" : 1 })
kgcrs:PRIMARY> db.t1.insert({"id":2,"name":"tom"})
WriteResult({ "nInserted" : 1 })
#查看数据
kgcrs:PRIMARY> db.t1.find()
{ "_id" : ObjectId("5b9a0d9efa7413d9cb629b39"), "id" : 1, "name" : "jack" }
{ "_id" : ObjectId("5b9a0dacfa7413d9cb629b3a"), "id" : 2, "name" : "tom" }
#数据库操作结束后我们开始日志操作
#进入local
kgcrs:PRIMARY> use local
switched to db local
#查看集合
kgcrs:PRIMARY> show collections
me
oplog.rs
replset.election
replset.minvalid
startup_log
system.replset
system.rollback.id
#查看日志文件
kgcrs:PRIMARY> db.oplog.rs.find()
(所有操作都记录在这个日志中)
#我们都知道主节点能进行操作,从节点只能充当复制集(不能操作)那就不能查看,但有一条命令可以查看
#登录到从节点后查看,发现查看不了
kgcrs:SECONDARY> show dbs
2018-09-13T15:28:51.529+0800 E QUERY [thread1] Error: listDatabases failed:{
"operationTime" : Timestamp(1536823728, 1),
"ok" : 0,
"errmsg" : "not master and slaveOk=false",
"code" : 13435,
"codeName" : "NotMasterNoSlaveOk",
"$clusterTime" : {
"clusterTime" : Timestamp(1536823728, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
} :_getErrorWithCode@src/mongo/shell/utils.js:25:13
br/>_getErrorWithCode@src/mongo/shell/utils.js:25:13
br/>shellHelper.show@src/mongo/shell/utils.js:849:19
br/>@(shellhelp2):1:1
kgcrs:SECONDARY> rs.slaveOk()
#查看一下(可以)
kgcrs:SECONDARY> show dbs
admin 0.000GB
config 0.000GB
kgc 0.000GB
#命令记不住,可以查看帮助
kgcrs:SECONDARY> rs.help()
#查看日志时间和大小
kgcrs:SECONDARY> rs.printReplicationInfo()
configured oplog size: 990MB
log length start to end: 2812secs (0.78hrs)
oplog first event time: Thu Sep 13 2018 14:49:16 GMT+0800 (CST)
oplog last event time: Thu Sep 13 2018 15:36:08 GMT+0800 (CST)
now: Thu Sep 13 2018 15:36:12 GMT+0800 (CST)
#查看节点和复制时间
kgcrs:SECONDARY> rs.printSlaveReplicationInfo()
source: 192.168.120.136:27018
syncedTo: Thu Sep 13 2018 15:40:28 GMT+0800 (CST)
0 secs (0 hrs) behind the primary
source: 192.168.120.136:27019
syncedTo: Thu Sep 13 2018 15:40:28 GMT+0800 (CST)
0 secs (0 hrs) behind the primary
#调整日志文件大小(离线升级)
#首先进入admin
kgcrs:SECONDARY> use admin
switched to db admin
#关闭服务
kgcrs:SECONDARY> db.shutdownServer()
server should be down...
#注销Replia set相关启动参数,并修改port端口号成27028
#进入配置文件
[root@localhost ~]# vim /etc/mongod2.conf
#replication:
#replSetName: kgcrs将这两条先注释掉(退出复制集)
将端口27018改成27028
#重启服务
[root@localhost ~]# mongod -f /etc/mongod2.conf
#全备当前节点的所有oplog记录
[root@localhost ~]# mongodump --port 27028 --db local --collection 'oplog.rs'
2018-09-13T16:01:34.259+0800 writing local.oplog.rs to
2018-09-13T16:01:34.280+0800 done dumping local.oplog.rs (347 documents)
[root@localhost ~]# mongo --port 27028
use local
switched to db local
#删除原有的
db.oplog.rs.drop()
true
#创建新的,并设置大小(2048M)
db.runCommand({create:"oplog.rs",capped:true,size:(210241024*1024)})
{ "ok" : 1 }
#关闭服务
use admin
switched to db admin
db.shutdownServer()
#再进配置文件
[root@localhost ~]# vim /etc/mongod2.conf
#去掉注释,添加大小
replication:
replSetName: kgcrs
oplogSizeMB: 2048
#把端口改回27018
#开启服务
[root@localhost ~]# mongod -f /etc/mongod2.conf
#进入27018
#查看大小
kgcrs:SECONDARY> rs.printReplicationInfo()
configured oplog size: 2048MB
log length start to end: 100secs (0.03hrs)
oplog first event time: Thu Sep 13 2018 16:10:29 GMT+0800 (CST)
oplog last event time: Thu Sep 13 2018 16:12:09 GMT+0800 (CST)
now: Thu Sep 13 2018 16:12:16 GMT+0800 (CST)
-------------------------------部署认证复制---------------------------------------------
#j进入主节点
kgcrs:PRIMARY> use admin
switched to db admin
#创建用户设置密码
kgcrs:PRIMARY> db.createUser({"user":"root","pwd":"123","roles":["root"]})
Successfully added user: { "user" : "root", "roles" : [ "root" ] }
#编辑配置文件
[root@localhost ~]# vim /etc/mongod.conf
#开启security添加两条语句
security:
keyFile: /usr/bin/kgcrskeyl
clusterAuthMode: keyFile
#修改后面几台同步操作()
[root@localhost ~]# vim /etc/mongod2.conf
security:
keyFile: /usr/bin/kgcrskey2
clusterAuthMode: keyFile
[root@localhost ~]# vim /etc/mongod3.conf
security:
keyFile: /usr/bin/kgcrskey3
clusterAuthMode: keyFile
[root@localhost ~]# vim /etc/mongod4.conf
security:
keyFile: /usr/bin/kgcrskey4
clusterAuthMode: keyFile
#写入
[root@localhost ~]# cd /usr/bin/
[root@localhost bin]# echo "kgcrs key" > kgcrskey1
[root@localhost bin]# echo "kgcrs key" > kgcrskey2
[root@localhost bin]# echo "kgcrs key" > kgcrskey4
[root@localhost bin]# echo "kgcrs key" > kgcrskey3

[root@localhost bin]# mongod -f /etc/mongod.conf --shutdown
killing process with pid: 50970
[root@localhost bin]# chmod 600 kgc*
[root@localhost bin]# mongod -f /etc/mongod.conf
#依次重启四个节点
#进入primary
kgcrs:PRIMARY> show dbs #无法查看数据库
kgcrs:PRIMARY> rs.status() #无法查看复制集

kgcrs:PRIMARY>use admin #身份验证登录
kgcrs:PRIMARY>db.auth("root","123")
#登录成功,可以查看


分享题目:MongoDB复制集管理(后续)
URL地址:http://cxhlcq.com/article/pscsih.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部