文章目录
环境初定搭建过程现象观察
环境初定
MongoDB安装过程参考:https://blog.csdn.net/weixin_42480750/article/details/108902137
MongoDB自动安装脚本:https://blog.csdn.net/weixin_42480750/article/details/108908684
功用IPHOST
Replica Set Primary13.13.5.5/16mongo01Replica Set Secondary13.13.6.6/16mongo02Replica Set Secondary13.13.7.7/16mongo03
搭建过程
设定/etc/hosts(all)
[root@mongo01 ~
]
[root@mongo01 ~
]
13.13.5.5 mongo01
13.13.6.6 mongo02
13.13.7.7 mongo03
[root@mongo01 ~
]
设定/etc/mongod.conf(all)
[root@mongo01 ~
]
[root@mongo01 ~
]
[root@mongo01 ~
]
systemLog:
destination:
file
logAppend:
true
path: /var/log/mongodb/mongod.log
storage:
dbPath: /var/lib/mongo
journal:
enabled:
true
processManagement:
fork:
true
pidFilePath: /var/run/mongodb/mongod.pid
timeZoneInfo: /usr/share/zoneinfo
net:
port: 27017
bindIp: 127.0.0.1,mongo01
replication:
replSetName:
"rs0"
[root@mongo01 ~
]
[root@mongo02 ~
]
....
bindIp: 127.0.0.1,mongo02
....
[root@mongo02 ~
]
[root@mongo03 ~
]
....
bindIp: 127.0.0.1,mongo03
....
[root@mongo03 ~
]
设定防火墙(all)
[root@mongo01 ~
]
success
[root@mongo01 ~
]
success
[root@mongo01 ~
]
[root@mongo01 ~
]
初始化RS(mongo01)
[root@mongo01 ~
]
> rs.initiate
( {
... _id
: "rs0",
... members:
[
...
{ _id: 0, host:
"mongo01" },
...
{ _id: 1, host:
"mongo02" },
...
{ _id: 2, host:
"mongo03" }
...
]
...
})
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp
(1601699100, 1
),
"signature" : {
"hash" : BinData
(0,
"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong
(0
)
}
},
"operationTime" : Timestamp
(1601699100, 1
)
}
rs0:SECONDARY
>
rs0:PRIMARY
>
rs0:PRIMARY
>
经历了短暂的选举过程后,mongo01当选主节点
[root@mongo02 ~
]
rs0:SECONDARY
>
[root@mongo03 ~
]
rs0:SECONDARY
>
rs0:PRIMARY
> rs.status
()
{
"set" : "rs0",
"date" : ISODate
("2020-10-03T04:27:08.038Z"),
"myState" : 1,
....
"members" : [
{
"_id" : 0,
"name" : "mongo01:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 171,
"optime" : {
"ts" : Timestamp
(1601699221, 1
),
"t" : NumberLong
(1
)
},
....
{
"_id" : 1,
"name" : "mongo02:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 127,
"optime" : {
"ts" : Timestamp
(1601699221, 1
),
"t" : NumberLong
(1
)
},
....
{
"_id" : 2,
"name" : "mongo03:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 127,
"optime" : {
"ts" : Timestamp
(1601699221, 1
),
"t" : NumberLong
(1
)
},
....
rs0:PRIMARY
>
现象观察
主库宕机从库升主
[root@mongo01 ~
]
[root@mongo01 ~
]
[root@mongo02 ~
]
rs0:PRIMARY
>
老主库恢复化身为从
[root@mongo01 ~
]
[root@mongo01 ~
]
rs0:SECONDARY
>
主库可读可写
rs0:PRIMARY
> use d1
switched to db d1
rs0:PRIMARY
> db.inventory.insertOne
({ item:
"canvas", qty: 100, tags:
["cotton"], size:
{ h: 28, w: 35.5, uom:
"cm" } })
{
"acknowledged" : true,
"insertedId" : ObjectId
("5f781dffb7a689be3046aa1a")
}
rs0:PRIMARY
> db.inventory.find
()
{ "_id" : ObjectId
("5f781dffb7a689be3046aa1a"),
"item" : "canvas",
"qty" : 100,
"tags" : [ "cotton" ],
"size" : { "h" : 28,
"w" : 35.5,
"uom" : "cm" } }
rs0:PRIMARY
>
从库默认不可读
rs0:SECONDARY
> use d1
switched to db d1
rs0:SECONDARY
> show tables
uncaught exception: Error: listCollections failed:
{
....
rs0:SECONDARY
>
从库可设置可读
rs0:SECONDARY
> rs.secondaryOk
()
rs0:SECONDARY
> show tables
inventory
rs0:SECONDARY
> db.inventory.find
()
{ "_id" : ObjectId
("5f781dffb7a689be3046aa1a"),
"item" : "canvas",
"qty" : 100,
"tags" : [ "cotton" ],
"size" : { "h" : 28,
"w" : 35.5,
"uom" : "cm" } }
从库不可写
rs0:SECONDARY
> db.inventory.insertOne
({ item:
"journal", qty: 25, tags:
["blank",
"red"], size:
{ h: 14, w: 21, uom:
"cm" } })
uncaught exception: WriteCommandError
({
....
rs0:SECONDARY
>