MongoDB升级
当前版本为3.4.9升级至4.2.22
1、备份当前所有的数据库(需要超级管理员角色,或者单独备份每个库)
$ /opt/mongodb/bin/mongodump -u root -p 124 -o /opt/sudytech/bak/
2022-08-26T11:12:26.990+0800 writing admin.system.users to
2022-08-26T11:12:26.991+0800 done dumping admin.system.users (3 documents)
2022-08-26T11:12:26.991+0800 writing admin.system.roles to
2022-08-26T11:12:26.992+0800 done dumping admin.system.roles (1 document)
2022-08-26T11:12:26.992+0800 writing admin.system.version to
2022-08-26T11:12:26.993+0800 done dumping admin.system.version (2 documents)
2022-08-26T11:12:26.993+0800 writing ucpplus.ucpplus to
2022-08-26T11:12:26.993+0800 writing aaaa.aaaa to
2022-08-26T11:12:26.994+0800 done dumping ucpplus.ucpplus (5 documents)
2022-08-26T11:12:26.995+0800 done dumping aaaa.aaaa (4 documents)
2、 停掉当前mongodb进程
$ kill -2 `ps -ef | grep mongo | grep -v grep | awk '{print $2}'`
3、备份旧数据库文件,解压新版压缩包,修改文件名
$ mv mongodb mongodb3
$ tar -zxvf mongodb-linux-x86_64-rhel70-4.2.22.tgz
$ mv mongodb-linux-x86_64-rhel70-4.2.22 mongodb
4、创建data、logs、conf文件夹,拷贝配置文件
$ cd /opt/mongodb && mkdir data logs conf && cd conf && cp /opt/mongodb3/conf/mongodb.conf ./
5、修改mongodb.conf,先关闭权限认证
$ cat mongodb.conf
dbpath=/opt/mongodb/data
logpath=/opt/mongodb/logs/mongodb.log
logappend=true
fork=true
auth=false
6、启动新mongodb
$ /opt/mongodb/bin/mongod -f /opt/mongodb/conf/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 14414
child process started successfully, parent exiting
7、导入数据
$ /opt/mongodb/bin/mongorestore /opt/sudytech/bak/
2022-08-26T11:30:38.066+0800 preparing collections to restore from
2022-08-26T11:30:38.071+0800 reading metadata for ucpplus.ucpplus from /opt/sudytech/bak/ucpplus/ucpplus.metadata.json
2022-08-26T11:30:38.071+0800 reading metadata for aaaa.aaaa from /opt/sudytech/bak/aaaa/aaaa.metadata.json
2022-08-26T11:30:38.083+0800 restoring ucpplus.ucpplus from /opt/sudytech/bak/ucpplus/ucpplus.bson
2022-08-26T11:30:38.085+0800 restoring aaaa.aaaa from /opt/sudytech/bak/aaaa/aaaa.bson
2022-08-26T11:30:38.158+0800 no indexes to restore
2022-08-26T11:30:38.158+0800 finished restoring ucpplus.ucpplus (5 documents, 0 failures)
2022-08-26T11:30:38.160+0800 no indexes to restore
2022-08-26T11:30:38.160+0800 finished restoring aaaa.aaaa (4 documents, 0 failures)
2022-08-26T11:30:38.160+0800 restoring users from /opt/sudytech/bak/admin/system.users.bson
2022-08-26T11:30:38.188+0800 restoring roles from /opt/sudytech/bak/admin/system.roles.bson
2022-08-26T11:30:38.253+0800 9 document(s) restored successfully. 0 document(s) failed to restore.
8、登录mongodb 查看数据
$ /opt/mongodb/bin/mongo
> show dbs;
aaaa 0.000GB
admin 0.000GB
config 0.000GB
local 0.000GB
ucpplus 0.000GB
>
> use aaaa;
switched to db aaaa
> db.aaaa.find();
{ "_id" : ObjectId("630838b2137b1a303f0e0b05"), "id" : 123, "name" : "hello" }
{ "_id" : ObjectId("630838b4137b1a303f0e0b06"), "id" : 123, "name" : "hello2" }
{ "_id" : ObjectId("630838b6137b1a303f0e0b07"), "id" : 123, "name" : "hello3" }
{ "_id" : ObjectId("630838b8137b1a303f0e0b08"), "id" : 123, "name" : "hello4" }
> use admin;
switched to db admin
> show users;
{
"_id" : "admin.admin",
"user" : "admin",
"db" : "admin",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
],
"mechanisms" : [
"SCRAM-SHA-1"
]
}
{
"_id" : "admin.root",
"user" : "root",
"db" : "admin",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
],
"mechanisms" : [
"SCRAM-SHA-1"
]
}
>
9、开启权限认证模块,重启mongodb
$ cat mongodb.conf
dbpath=/opt/mongodb/data
logpath=/opt/mongodb/logs/mongodb.log
logappend=true
fork=true
auth=true