服务器信息
ip | 系统 | 配置 | 服务 | 目录 |
---|---|---|---|---|
172.24.32.201 | centos7.7 | 2c4g | mongo-primary | /var/lib/mongo (the data directory) /var/log/mongodb (the log directory) |
172.24.32.201 | centos7.7 | 2c4g | mongo-secondary | /var/lib/mongo27018 (the data directory) /var/log/mongodb27018 (the log directory) |
172.24.32.201 | centos7.7 | 2c4g | mongo-secondary | /var/lib/mongo27019 (the data directory) /var/log/mongodb27019 (the log directory) |
升级前先进行数据备份
mkdir -p /data/mongodb_bak
mongodump -h 172.24.32.201:27017 -u lugo -p test123 -d lugotest -o /data/mongodb_bak
升级
在主节点上执行
db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } )
结果
lugotestrepl:PRIMARY> db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } )
{ "featureCompatibilityVersion" : "3.4", "ok" : 1 }
在主节点上执行
db.adminCommand( { setFeatureCompatibilityVersion: "3.4" } )
结果
lugotestrepl:PRIMARY> db.adminCommand( { setFeatureCompatibilityVersion: "3.4" } )
{ "ok" : 1 }
修改yum文件
cat >/etc/yum.repos.d/mongodb-org-3.4.repo<<EOF
[mongodb-org-3.6]
name=MongoDB Repository
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mongodb/yum/el7-3.6/
gpgcheck=0
enabled=1
EOF
升级指令:
yum -y upgrade mongodb-org
重启从
systemctl restart mongod27019
查看版本
[root@test02 lib]# mongo 172.24.32.201:27019
MongoDB shell version v3.6.18
connecting to: mongodb://172.24.32.201:27019/test?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("20f2b517-3c09-488c-a2e9-ef9138896cf8") }
MongoDB server version: 3.6.18
升级成功,下面重启另外的从和主
[root@test02 log]# systemctl restart mongod27018
[root@test02 log]# systemctl restart mongod27017
升级完毕,进入主,查看副本集状态
lugotestrepl:PRIMARY> rs.status();
{
"set" : "lugotestrepl",
"date" : ISODate("2020-05-14T10:28:01.084Z"),
"myState" : 1,
"term" : NumberLong(11),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1589452079, 1),
"t" : NumberLong(11)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1589452079, 1),
"t" : NumberLong(11)
},
"appliedOpTime" : {
"ts" : Timestamp(1589452079, 1),
"t" : NumberLong(11)
},
"durableOpTime" : {
"ts" : Timestamp(1589452079, 1),
"t" : NumberLong(11)
}
},
"members" : [
{
"_id" : 0,
"name" : "172.24.32.201:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 376,
"optime" : {
"ts" : Timestamp(1589452079, 1),
"t" : NumberLong(11)
},
"optimeDate" : ISODate("2020-05-14T10:27:59Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1589451718, 1),
"electionDate" : ISODate("2020-05-14T10:21:58Z"),
"configVersion" : 6,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "172.24.32.201:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 373,
"optime" : {
"ts" : Timestamp(1589452079, 1),
"t" : NumberLong(11)
},
"optimeDurable" : {
"ts" : Timestamp(1589452079, 1),
"t" : NumberLong(11)
},
"optimeDate" : ISODate("2020-05-14T10:27:59Z"),
"optimeDurableDate" : ISODate("2020-05-14T10:27:59Z"),
"lastHeartbeat" : ISODate("2020-05-14T10:28:00.915Z"),
"lastHeartbeatRecv" : ISODate("2020-05-14T10:27:59.300Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "172.24.32.201:27017",
"syncSourceHost" : "172.24.32.201:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 6
},
{
"_id" : 2,
"name" : "172.24.32.201:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 373,
"optime" : {
"ts" : Timestamp(1589452079, 1),
"t" : NumberLong(11)
},
"optimeDurable" : {
"ts" : Timestamp(1589452079, 1),
"t" : NumberLong(11)
},
"optimeDate" : ISODate("2020-05-14T10:27:59Z"),
"optimeDurableDate" : ISODate("2020-05-14T10:27:59Z"),
"lastHeartbeat" : ISODate("2020-05-14T10:28:00.915Z"),
"lastHeartbeatRecv" : ISODate("2020-05-14T10:28:00.914Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "172.24.32.201:27017",
"syncSourceHost" : "172.24.32.201:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 6
}
],
"ok" : 1,
"operationTime" : Timestamp(1589452079, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1589452079, 1),
"signature" : {
"hash" : BinData(0,"9Fk98Fv6GnTVxACxD129SauwrJs="),
"keyId" : NumberLong("6826644216827871238")
}
}
}
都正常,在master节点上执行
db.adminCommand( { setFeatureCompatibilityVersion: "3.6" } )
去三个节点上分别验证数据库 featureCompatibilityVersion 版本
lugotestrepl:PRIMARY> db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } )
{
"featureCompatibilityVersion" : {
"version" : "3.6"
},
"ok" : 1,
"operationTime" : Timestamp(1589451968, 3),
"$clusterTime" : {
"clusterTime" : Timestamp(1589451968, 3),
"signature" : {
"hash" : BinData(0,"n+H+Ku+VuWpj2j4wTPaS7rqE3Kc="),
"keyId" : NumberLong("6826644216827871238")
}
}
}
lugotestrepl:PRIMARY> exit
bye
[root@test02 lib]# mongo 172.24.32.201:27018
MongoDB shell version v3.6.18
connecting to: mongodb://172.24.32.201:27018/test?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("8abc2bea-8431-46d4-ac28-dc93a6278679") }
MongoDB server version: 3.6.18
lugotestrepl:SECONDARY> db.auth('root','test123')
1
lugotestrepl:SECONDARY> db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } )
{
"featureCompatibilityVersion" : {
"version" : "3.6"
},
"ok" : 1,
"operationTime" : Timestamp(1589451989, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1589451989, 1),
"signature" : {
"hash" : BinData(0,"kmr3W101rIo5UNuegNR3CZwe+9s="),
"keyId" : NumberLong("6826644216827871238")
}
}
}
lugotestrepl:SECONDARY> exit
bye
[root@test02 lib]# mongo 172.24.32.201:27019
MongoDB shell version v3.6.18
connecting to: mongodb://172.24.32.201:27019/test?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("009185ff-7e99-4ba0-af42-d37830d843c2") }
MongoDB server version: 3.6.18
lugotestrepl:SECONDARY> db.auth('root','test123')
1
lugotestrepl:SECONDARY> db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } )
{
"featureCompatibilityVersion" : {
"version" : "3.6"
},
"ok" : 1,
"operationTime" : Timestamp(1589452019, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1589452019, 1),
"signature" : {
"hash" : BinData(0,"cpcC9hNdr2VJ1qR9ZP3Mo66XiLU="),
"keyId" : NumberLong("6826644216827871238")
}
}
}
验证数据
lugotestrepl:PRIMARY> use lugotest;
switched to db lugotest
lugotestrepl:PRIMARY> db.auth('lugo','test123')
1
lugotestrepl:PRIMARY> db.movie.find().pretty()
{
"_id" : ObjectId("5ebbbca5692adbface98b2af"),
"moviename" : "大侦探福尔摩斯",
"points" : "9.5"
}
{
"_id" : ObjectId("5ebbbca5692adbface98b2b0"),
"moviename" : "掠夺",
"points" : "9.2"
}
{
"_id" : ObjectId("5ebbbca5692adbface98b2b1"),
"moviename" : "摇滚黑帮",
"points" : "9.9"
}
{
"_id" : ObjectId("5ebbbca5692adbface98b2b2"),
"moviename" : "两杆大烟枪",
"points" : "9.1"
}
{
"_id" : ObjectId("5ebcff34df72ed1bb0505234"),
"moviename" : "杀死比尔",
"points" : "9.0"
}
此次升级,无数据丢失