1. 升级前准备
在升级MongoDB之前,需要进行充分的准备工作,包括检查当前版本、备份数据、检查硬件环境等。更多学习教程www.fgedu.net.cn
# /mongodb/current/bin/mongod –version
MongoDB server version: 4.4.10
# 检查硬件环境
# free -h
total used free shared buff/cache available
Mem: 64G 2.1G 60G 8.5M 1.8G 61G
Swap: 32G 0B 32G
# 检查磁盘空间
# df -h /mongodb
Filesystem Size Used Avail Use% Mounted on
/dev/sdb1 2.0T 200G 1.8T 10% /mongodb
– 确保有足够的磁盘空间用于备份和安装新版本
– 确保内存足够,MongoDB 5.0对内存的使用可能会有所增加
– 安排在业务低峰期进行升级
– 提前通知相关业务方,做好升级计划
– 检查MongoDB 5.0的系统要求,确保操作系统版本兼容
2. 数据备份
在升级之前,必须对MongoDB数据进行备份,以防止升级过程中出现问题导致数据丢失。学习交流加群风哥微信: itpux-com
# mkdir -p /backup/mongodb/upgrade
# 执行备份
# /mongodb/current/bin/mongodump –host 192.168.1.51 –port 27017 –username admin –password fgedudb123 –authenticationDatabase admin –out /backup/mongodb/upgrade/$(date +”%Y%m%d%H%M%S”)
# 检查备份文件
# ls -la /backup/mongodb/upgrade/
total 16
drwxr-xr-x 3 root root 4096 Mar 31 10:00 20260331100000
drwxr-xr-x 2 root root 4096 Mar 31 10:00 .
drwxr-xr-x 3 root root 4096 Mar 31 10:00 ..
3. 下载新版本
从MongoDB官方网站下载最新版本的MongoDB 5.0。
# cd /tmp
# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-5.0.8.tgz
# 解压源码包
# tar -zxvf mongodb-linux-x86_64-rhel70-5.0.8.tgz
# 检查解压结果
# ls -la mongodb-linux-x86_64-rhel70-5.0.8/
total 2048
drwxr-xr-x 6 root root 4096 Mar 31 10:05 mongodb-linux-x86_64-rhel70-5.0.8
-rw-r–r– 1 root root 1048576 Mar 31 10:05 mongodb-linux-x86_64-rhel70-5.0.8.tgz
4. 停止服务
在安装新版本之前,需要停止当前运行的MongoDB服务。
# systemctl stop mongodb
# 检查服务状态
# systemctl status mongodb
● mongodb.service – MongoDB Server
Loaded: loaded (/etc/systemd/system/mongodb.service; enabled; vendor preset: disabled)
Active: inactive (dead) since Mon 2026-03-31 10:10:00 CST; 1min ago
Process: 12345 ExecStop=/mongodb/current/bin/mongod –config /mongodb/conf/mongod.conf –shutdown (code=exited, status=0/SUCCESS)
Main PID: 12345 (code=exited, status=0/SUCCESS)
5. 安装新版本
安装MongoDB 5.0版本。
# mv /tmp/mongodb-linux-x86_64-rhel70-5.0.8 /mongodb
# 创建新的符号链接
# rm -f /mongodb/current
# ln -s /mongodb/mongodb-linux-x86_64-rhel70-5.0.8 /mongodb/current
# 检查安装结果
# ls -la /mongodb/current/bin/
total 153600
drwxr-xr-x 2 root root 4096 Mar 31 10:15 .
drwxr-xr-x 3 root root 4096 Mar 31 10:15 ..
-rwxr-xr-x 1 root root 10693632 Mar 31 10:15 mongod
-rwxr-xr-x 1 root root 1441536 Mar 31 10:15 mongos
-rwxr-xr-x 1 root root 14414336 Mar 31 10:15 mongo
-rwxr-xr-x 1 root root 10693632 Mar 31 10:15 mongodump
-rwxr-xr-x 1 root root 5347840 Mar 31 10:15 mongoexport
-rwxr-xr-x 1 root root 5347840 Mar 31 10:15 mongoimport
-rwxr-xr-x 1 root root 1441536 Mar 31 10:15 mongorestore
-rwxr-xr-x 1 root root 5347840 Mar 31 10:15 mongostat
-rwxr-xr-x 1 root root 5347840 Mar 31 10:15 mongotop
6. 配置文件迁移
将旧版本的配置文件迁移到新版本,并根据MongoDB 5.0的新特性进行优化。学习交流加群风哥QQ113257174
# cp /mongodb/conf/mongod.conf /mongodb/conf/mongod.conf.bak
# 编辑配置文件,添加MongoDB 5.0的新特性
# vi /mongodb/conf/mongod.conf
# 添加以下配置
storage:
dbPath: /mongodb/data
journal:
enabled: true
engine: wiredTiger
wiredTiger:
engineConfig:
cacheSizeGB: 48
directoryForIndexes: true
net:
port: 27017
bindIp: 192.168.1.51
processManagement:
fork: true
pidFilePath: /mongodb/run/mongod.pid
systemLog:
destination: file
logAppend: true
path: /mongodb/log/mongod.log
security:
authorization: enabled
operationProfiling:
mode: slowOp
slowOpThresholdMs: 100
replication:
replSetName: fgedudb
# MongoDB 5.0新特性:时间序列集合
timeseries:
collectionDefaults:
granularity: seconds
# MongoDB 5.0新特性:查询计划缓存
query:
planCacheSizeMB: 100
7. 启动服务
使用新版本的MongoDB启动服务。
# systemctl start mongodb
# 检查服务状态
# systemctl status mongodb
● mongodb.service – MongoDB Server
Loaded: loaded (/etc/systemd/system/mongodb.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2026-03-31 10:20:00 CST; 1min ago
Main PID: 67890 (mongod)
Tasks: 24
Memory: 1.0G
CGroup: /system.slice/mongodb.service
└─67890 /mongodb/current/bin/mongod –config /mongodb/conf/mongod.conf
8. 升级验证
升级完成后,需要验证MongoDB服务是否正常运行。
# /mongodb/current/bin/mongo –host 192.168.1.51 –port 27017 -u admin -p fgedudb123 –authenticationDatabase admin
# 测试命令
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
fgedudb 0.000GB
> use fgedudb
switched to db fgedudb
> db.fgedu_users.insert({name: “测试用户”, age: 25, email: “test@fgedu.net.cn”})
WriteResult({ “nInserted” : 1 })
> db.fgedu_users.find()
{ “_id” : ObjectId(“60a7b3c4d5e6f7g8h9i0j1k2”), “name” : “测试用户”, “age” : 25, “email” : “test@fgedu.net.cn” }
> db.serverStatus()
{
“host” : “fgedudb01”,
“version” : “5.0.8”,
“process” : “mongod”,
“pid” : NumberLong(67890),
“uptime” : 360,
“uptimeMillis” : NumberLong(360000),
“uptimeEstimate” : NumberLong(360),
“localTime” : ISODate(“2026-03-31T10:26:00Z”),
“asserts” : {
“regular” : 0,
“warning” : 0,
“msg” : 0,
“user” : 0,
“rollovers” : 0
},
“connections” : {
“current” : 1,
“available” : 65534,
“totalCreated” : 1
}
}
> exit
9. 性能调优
MongoDB 5.0引入了一些新的性能特性,需要进行相应的调优。更多学习教程公众号风哥教程itpux_com
# vi /mongodb/conf/mongod.conf
# MongoDB 5.0性能调优
storage:
dbPath: /mongodb/data
journal:
enabled: true
engine: wiredTiger
wiredTiger:
engineConfig:
cacheSizeGB: 48
directoryForIndexes: true
concurrentTransactions: 128
net:
port: 27017
bindIp: 192.168.1.51
maxIncomingConnections: 10000
wireObjectCheck: false
processManagement:
fork: true
pidFilePath: /mongodb/run/mongod.pid
systemLog:
destination: file
logAppend: true
path: /mongodb/log/mongod.log
logRotate: reopen
security:
authorization: enabled
operationProfiling:
mode: slowOp
slowOpThresholdMs: 100
replication:
replSetName: fgedudb
oplogSizeMB: 10240
# MongoDB 5.0新特性:时间序列集合
timeseries:
collectionDefaults:
granularity: seconds
# MongoDB 5.0新特性:查询计划缓存
query:
planCacheSizeMB: 100
maxQueryPlanCacheSizeBytes: 104857600
# MongoDB 5.0新特性:连接池优化
setParameter:
maxConnsPerHost: 200
maxIdleTimeMS: 300000
socketTimeoutMS: 60000
– 根据服务器内存大小调整wiredTiger.cacheSizeGB参数
– 启用并发事务处理以提高性能
– 合理设置连接池参数
– 定期监控MongoDB的性能指标
– 利用MongoDB 5.0的新特性,如时间序列集合和查询计划缓存
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
