[mongod@node01 ~]# openssl rand -base64 666 > /var/lib/mongo/keyfile
[mongod@node01 ~]# chown mongod:mongod /var/lib/mongo/keyfile
[mongod@node01 ~]# chmod 600 /var/lib/mongo/keyfile
[mongod@node01 ~]# l /var/lib/mongo/keyfile
-rw------- 1 mongod mongod 902 Sep 1 10:37 /var/lib/mongo/keyfile
[mongod@node01 ~]# scp /var/lib/mongo/keyfile node02:/var/lib/mongo/
[mongod@node01 ~]# scp /var/lib/mongo/keyfile node03:/var/lib/mongo/
[root@node01 ~]# vim /etc/mongod.conf
security:
authorization: enabled
clusterAuthMode: keyFile
keyFile: /var/lib/mongo/keyfile
replication:
replSetName: "rs01"
[root@node01 ~]# systemctl restart mongod
[root@node01 ~]# mongo -u "root" -p "abcd.1234" --authenticationDatabase "admin"
> rs.initiate()
rs01:PRIMARY> use local
switched to db local
rs01:PRIMARY> db.oplog.rs.stats().maxSize
1038090240
node01执行备份
[mongod@node01 ~]# mongodump -uroot -pabcd.1234 --authenticationDatabase=admin --oplog -o /home/mongod/backup
通过scp复制到node02
[mongod@node01 ~]# scp -r backup/* node02:/home/mongod/backup/
node02以单实例方式启动
[root@node02 ~]# vim /etc/mongod.conf
#security:
# authorization: enabled
# clusterAuthMode: keyFile
# keyFile: /var/lib/mongo/keyfile
#replication:
# replSetName: "rs01"
node02执行恢复
[mongod@node02 ~]$ mongorestore --oplogReplay --dir /home/mongod/backup
在node02创建oplog.rs集合
[mongod@node02 ~]$ mongo
> use local
switched to db local
> db.createCollection("oplog.rs",{"capped":true,"size":1038090240})
{ "ok" : 1 }
在node02恢复oplog.rs集合
[mongod@node02 ~]$ mongorestore -d local -c oplog.rs /home/mongod/backup/oplog.bson
在node01查询replset.election集合
rs01:PRIMARY> use local
switched to db local
rs01:PRIMARY> db.replset.election.find()
{ "_id" : ObjectId("64f14f56845ba9f11d295ee1"), "term" : NumberLong(1), "candidateIndex" : NumberLong(0) }
在node02保存node01查询的ection集合的数据
> use local
switched to db local
> db.replset.election.save({ "_id" : ObjectId("64f14f56845ba9f11d295ee1"), "term" : NumberLong(1), "candidateIndex" : NumberLong(0)})
node02以副本集方式启动
[root@node02 ~]# vim /etc/mongod.conf
security:
authorization: enabled
clusterAuthMode: keyFile
keyFile: /var/lib/mongo/keyfile
replication:
replSetName: "rs01"
[root@node02 ~]# systemctl restart mongod
在node01执行增加节点操作
rs01:PRIMARY> rs.add("192.168.1.102:27017")
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1693542188, 1),
"signature" : {
"hash" : BinData(0,"RGkEIE8wI79S5Uk7f7eUVqGFA1g="),
"keyId" : NumberLong("7273693227931140101")
}
},
"operationTime" : Timestamp(1693542188, 1)
}
rs01:PRIMARY> rs.status().members
[
{
"_id" : 0,
"name" : "node01:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 8783,
"optime" : {
"ts" : Timestamp(1693544857, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2023-09-01T05:07:37Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1693538676, 2),
"electionDate" : ISODate("2023-09-01T03:24:36Z"),
"configVersion" : 2,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "192.168.1.102:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 2674,
"optime" : {
"ts" : Timestamp(1693544857, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1693544857, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2023-09-01T05:07:37Z"),
"optimeDurableDate" : ISODate("2023-09-01T05:07:37Z"),
"lastHeartbeat" : ISODate("2023-09-01T05:07:42.388Z"),
"lastHeartbeatRecv" : ISODate("2023-09-01T05:07:43.268Z"),
"pingMs" : umberLong(1),
"lastHeartbeatMessage" : "",
"syncingTo" : "node01:27017",
"syncSourceHost" : "node01:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 2
}
]
验证
rs01:PRIMARY> use tpcc10
switched to db tpcc10
rs01:PRIMARY> db.customer.count()
299702
rs01:SECONDARY> use tpcc10
switched to db tpcc10
rs01:SECONDARY> db.customer.count()
299702
rs01:SECONDARY> rs.secondaryOk()
标签:node02,mongod,node01,mongo,rs01,MongoDB,备份,db,副本
From: https://blog.51cto.com/dbprofessional/7322804