之前的文章也有说到过正式环境,不建议使用单节点,Mongodb备份恢复到任意时间点,在这一篇中说过。
由于历史原因,我们有一个不太重要的业务,正式环境使用单节点,随着业务增长,需要升级为副本集保证高可用。
由于需要重启服务,我们和业务线讨论决定,在凌晨业务低峰期,进行操作,数据存储目前40G左右。预计维护时间一小时。
1、原节点配置信息:
IP:10.10.20.17
对应域名:app.rs1.mongodb.com
1.1、原配置文件
systemLog:
destination: file
logAppend: true
path: /data2/mongodb2/logs/mongodb.log
storage:
dbPath: /data2/mongodb2/data
journal:
enabled: true
directoryPerDB: true
wiredTiger:
engineConfig:
journalCompressor: none
directoryForIndexes: true
collectionConfig:
blockCompressor: none
indexConfig:
prefixCompression: false
processManagement:
fork: true
pidFilePath: /data2/mongodb2/pid/mongodb.pid
net:
port: 27011
bindIp: app.rs1.mongodb.com
maxIncomingConnections: 5000
1.2、需要在原来配置文件增加复制集配置和认证配置:
认证配置需要注释掉,复制集配置完成,管理员用户创建完成,在开启。
#security:
# keyFile: /data2/mongodb/conf/keyfile
# authorization: enabled
replication:
replSetName: rs1
2、新节点信息:
SECONDARY 1节点 IP:10.10.20.31
SECONDARY 2 节点IP:10.0.6.44
我们使用内网域名配置:
IP:10.10.20.17 对应域名: app.rs1.mongodb.com
IP:10.10.20.31 对应域名: app.rs2.mongodb.com
IP:10.0.6.44 对应域名: app.rs3.mongodb.com
2.2、启动三个节点:
两个SECONDARY节点复制PRIMARY节点配置文件,需要修改相应的 "bindIp: 为本机的域名"
[mongod@VM_20_17_centos ~]$ /home/mongod/mongodb-3.6.9/bin/mongod -f /data2/mongodb2/conf/mongodb2.conf
[mongod@VM_20_31_centos ~]$ /home/mongod/mongodb-3.6.9/bin/mongod -f /data/mongodb2/conf/mongodb2.conf
[mongod@VM_6_44_centos ~]$ /home/mongod/mongodb-3.6.9/bin/mongod -f /data/mongodb2/conf/mongodb2.conf
about to fork child process, waiting until server is ready for connections.
forked process: 11474
child process started successfully, parent exiting #返回successfully表示启动成功
2.3、如果配置有问题返回异常:
about to fork child process, waiting until server is ready for connections.
forked process: 10135
ERROR: child process failed, exited with error number 48
To see additional information in this output, start without the "--fork" option.
查看日志根据日志错误修改相应配置。
3、使用mongo shell连接到其中一个节点
[mongod@VM_20_17_centos ~]$ /home/mongod/mongodb-3.6.9/bin/mongo 10.10.20.17:27011/admin
MongoDB shell version v3.6.9
connecting to: mongodb://10.10.20.17:27011/admin
Implicit session: session { "id" : UUID("d4c23867-c181-4a72-ab7d-a23b4b522d04") }
MongoDB server version: 3.6.9
Server has startup warnings:
2020-08-10T11:07:35.351+0800 I CONTROL [initandlisten]
> config = { _id:"rs1", members:[
... {_id:0,host:"app.rs1.mongodb.com:27011",priority:90},
... {_id:1,host:"app.rs2.mongodb.com:27011",priority:90},
... {_id:2,host:"app.rs3.mongodb.com:27011",priority:90}
... ]
... }
{
"_id" : "rs1",
"members" : [
{
"_id" : 0,
"host" : "app.rs1.mongodb.com:27011",
"priority" : 90
},
{
"_id" : 1,
"host" : "app.rs2.mongodb.com:27011",
"priority" : 90
},
{
"_id" : 2,
"host" : "app.rs3.mongodb.com:27011",
"priority" : 90
}
]
}
> rs.initiate(config); //初始化副本集
{
"ok" : 1, //返回OK:1成功,返回OK:0失败
"operationTime" : Timestamp(1597029060, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1597029060, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
3.1、验证复制集数据是否同步完成
### 查看副本集PRIMARY节点数据:
rs1:PRIMARY> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
test_scene 34.011GB
rs1:PRIMARY> use test_scene
switched to db test_scene
rs1:PRIMARY> show tables;
test_scene_page
rs1:PRIMARY> db.test_scene_page
db.test_scene_page
rs1:PRIMARY> db.test_scene_page.count()
2120587
rs1:PRIMARY>
### 查看副本集其他节点成员:
[mongod@VM_6_44_centos ~]$ /home/mongod/mongodb-3.6.9/bin/mongo app.rs3.mongodb.com:27011
rs1:SECONDARY> db.test_scene_page.count()
2120587
rs1:SECONDARY>
3.2、到这里我们单节点升级副本集基本完成了,还有几个步骤:
1、需要创建管理员用户,
2、并开启认证,
3、重启副本集成员节点,
这三步不详细说了请看Mongodb复制集开启认证授权