MongoDB 数据备份迁移 导出导入

因为之前 MongoDB 云数据库 购买是深圳地区单节点版,续费略有些贵,一年续费1020元,目前主机是在香港,单节点MongoDB,香港区域的阿里云没得买,还有一个 Serverless 版 MongoDB,价格便宜一些,一年要三四百元,摸索了几天,发现太坑了,连接方式不一样,导入数据失败。

原来的数据库版本是 4.0,Serverless 版 MongoDB 的版本是4.2,将数据从4.0导出到4.2中,死活不行,数据导入一部分后提示 createIndex error

将数据导入到本地 4.0,升级 MongoDB 到 4.2 后导出,再导入到 Serverless 版 MongoDB,仍然是数据导入一部分后提示 createIndex error

在网上搜索也找不到相关内容 😓

想起 MongoDB 官方有个可以免费使用的 atlas,很早以前就注册了,一直没有怎么使用,试着将数据导入,顺畅一气呵成。从4.0导出,直接导入到了4.4.4版本,没有提示任何错误。看起来数据也很正常

因为 MongoDB atlas 创建时选择的是 Azure Hong Kong,从阿里云香港到 MongoDB atlas 到 ping 值在 3 毫秒左右,够啦,够啦。不过 MongoDB atlas 免费版没有数据备份,以后就要手动备份了。

数据备份导出

mongodump --uri="mongodb://username:password@dds-wz9c05****************.mongodb.rds.aliyuncs.com:3717/dbname"

替换上面的 usernamepassworddbname 以及 URL 地址

执行完成后,将在当前目录自动生成一个 dump 文件夹,里面即为备份文件

导出记录
coder@761a694cd888:~/project/server/db$ mongodump --uri="mongodb://funsapp:*****@dds-wz9c05****************.mongodb.rds.aliyuncs.com:3717/funsapp"
2021-03-27T02:28:08.272+0000    writing funsapp.sessions to dump/funsapp/sessions.bson
2021-03-27T02:28:08.341+0000    writing funsapp.stats to dump/funsapp/stats.bson
2021-03-27T02:28:08.348+0000    writing funsapp.posts to dump/funsapp/posts.bson
2021-03-27T02:28:08.355+0000    writing funsapp.signs to dump/funsapp/signs.bson
2021-03-27T02:28:08.397+0000    done dumping funsapp.signs (4888 documents)
2021-03-27T02:28:08.398+0000    writing funsapp.users to dump/funsapp/users.bson
2021-03-27T02:28:08.409+0000    done dumping funsapp.posts (299 documents)
2021-03-27T02:28:08.423+0000    done dumping funsapp.users (62 documents)
2021-03-27T02:28:08.423+0000    writing funsapp.apps to dump/funsapp/apps.bson
2021-03-27T02:28:08.431+0000    writing funsapp.links to dump/funsapp/links.bson
2021-03-27T02:28:08.432+0000    done dumping funsapp.stats (3338 documents)
2021-03-27T02:28:08.447+0000    writing funsapp.myapps to dump/funsapp/myapps.bson
2021-03-27T02:28:08.455+0000    done dumping funsapp.apps (51 documents)
2021-03-27T02:28:08.456+0000    done dumping funsapp.links (37 documents)
2021-03-27T02:28:08.456+0000    writing funsapp.comments to dump/funsapp/comments.bson
2021-03-27T02:28:08.457+0000    writing funsapp.promotions to dump/funsapp/promotions.bson
2021-03-27T02:28:08.469+0000    done dumping funsapp.myapps (28 documents)
2021-03-27T02:28:08.470+0000    writing funsapp.urls to dump/funsapp/urls.bson
2021-03-27T02:28:08.473+0000    done dumping funsapp.comments (24 documents)
2021-03-27T02:28:08.479+0000    writing funsapp.pages to dump/funsapp/pages.bson
2021-03-27T02:28:08.481+0000    done dumping funsapp.promotions (9 documents)
2021-03-27T02:28:08.482+0000    writing funsapp.tasks to dump/funsapp/tasks.bson
2021-03-27T02:28:08.493+0000    done dumping funsapp.urls (8 documents)
2021-03-27T02:28:08.497+0000    done dumping funsapp.tasks (3 documents)
2021-03-27T02:28:08.518+0000    done dumping funsapp.pages (8 documents)
2021-03-27T02:28:08.552+0000    done dumping funsapp.sessions (32454 documents)
mongodump.png

数据导入

mongorestore --uri mongodb+srv://username:password@cluster0.p****.mongodb.net/dbname
mongorestore --uri mongodb+srv://username:password@clust***.p4***.mongodb.net/dbname --db dbname ./dump/dbname

替换上面的 usernamepassworddbname 以及 URL 地址

--db 指定导入的数据库名,后面接导入数据源。可省略,默认导入当前目录下的 ./dump/ 中数据

导入记录
coder@761a694cd888:~/project/server/db27$ mongorestore --uri mongodb+srv://funsapp:*****@cluster0.p****.mongodb.net/funsapp --db funsapp ./dump/funsapp
2021-03-27T02:29:14.365+0000    the --db and --collection args should only be used when restoring from a BSON file. Other uses are deprecated and will not exist in the future; use --nsInclude instead
2021-03-27T02:29:14.369+0000    building a list of collections to restore from dump/funsapp dir
2021-03-27T02:29:14.462+0000    reading metadata for funsapp.sessions from dump/funsapp/sessions.metadata.json
2021-03-27T02:29:15.166+0000    reading metadata for funsapp.stats from dump/funsapp/stats.metadata.json
2021-03-27T02:29:15.169+0000    reading metadata for funsapp.posts from dump/funsapp/posts.metadata.json
2021-03-27T02:29:15.173+0000    reading metadata for funsapp.signs from dump/funsapp/signs.metadata.json
2021-03-27T02:29:15.177+0000    restoring funsapp.sessions from dump/funsapp/sessions.bson
2021-03-27T02:29:15.290+0000    restoring funsapp.stats from dump/funsapp/stats.bson
2021-03-27T02:29:15.326+0000    restoring funsapp.posts from dump/funsapp/posts.bson
2021-03-27T02:29:15.947+0000    restoring funsapp.signs from dump/funsapp/signs.bson
2021-03-27T02:29:17.199+0000    [#.......................]  funsapp.sessions   395KB/6.22MB    (6.2%)
2021-03-27T02:29:17.199+0000    [#############...........]     funsapp.stats   846KB/1.44MB   (57.5%)
2021-03-27T02:29:17.199+0000    [########################]     funsapp.posts  1.09MB/1.09MB  (100.0%)
2021-03-27T02:29:17.199+0000    [##############..........]     funsapp.signs    234KB/381KB   (61.5%)
2021-03-27T02:29:17.199+0000
2021-03-27T02:29:18.029+0000    [########################]  funsapp.signs  381KB/381KB  (100.0%)
2021-03-27T02:29:18.030+0000    no indexes to restore
2021-03-27T02:29:18.034+0000    finished restoring funsapp.signs (4888 documents, 0 failures)
2021-03-27T02:29:18.037+0000    reading metadata for funsapp.apps from dump/funsapp/apps.metadata.json
2021-03-27T02:29:18.148+0000    restoring funsapp.apps from dump/funsapp/apps.bson
2021-03-27T02:29:18.342+0000    no indexes to restore
2021-03-27T02:29:18.345+0000    finished restoring funsapp.apps (51 documents, 0 failures)
2021-03-27T02:29:18.348+0000    reading metadata for funsapp.users from dump/funsapp/users.metadata.json
2021-03-27T02:29:18.470+0000    restoring funsapp.users from dump/funsapp/users.bson
2021-03-27T02:29:18.579+0000    restoring indexes for collection funsapp.users from metadata
2021-03-27T02:29:19.031+0000    finished restoring funsapp.users (62 documents, 0 failures)
2021-03-27T02:29:19.035+0000    reading metadata for funsapp.pages from dump/funsapp/pages.metadata.json
2021-03-27T02:29:19.151+0000    restoring funsapp.pages from dump/funsapp/pages.bson
2021-03-27T02:29:19.245+0000    restoring indexes for collection funsapp.pages from metadata
2021-03-27T02:29:19.482+0000    finished restoring funsapp.pages (8 documents, 0 failures)
2021-03-27T02:29:19.485+0000    reading metadata for funsapp.myapps from dump/funsapp/myapps.metadata.json
2021-03-27T02:29:19.600+0000    restoring funsapp.myapps from dump/funsapp/myapps.bson
2021-03-27T02:29:19.661+0000    [########################]  funsapp.posts  1.09MB/1.09MB  (100.0%)
2021-03-27T02:29:19.661+0000    restoring indexes for collection funsapp.posts from metadata
2021-03-27T02:29:19.706+0000    no indexes to restore
2021-03-27T02:29:19.709+0000    finished restoring funsapp.myapps (28 documents, 0 failures)
2021-03-27T02:29:19.712+0000    reading metadata for funsapp.links from dump/funsapp/links.metadata.json
2021-03-27T02:29:19.824+0000    restoring funsapp.links from dump/funsapp/links.bson
2021-03-27T02:29:19.860+0000    finished restoring funsapp.posts (299 documents, 0 failures)
2021-03-27T02:29:19.863+0000    reading metadata for funsapp.comments from dump/funsapp/comments.metadata.json
2021-03-27T02:29:19.926+0000    restoring indexes for collection funsapp.links from metadata
2021-03-27T02:29:19.977+0000    restoring funsapp.comments from dump/funsapp/comments.bson
2021-03-27T02:29:20.104+0000    no indexes to restore
2021-03-27T02:29:20.108+0000    finished restoring funsapp.comments (24 documents, 0 failures)
2021-03-27T02:29:20.111+0000    reading metadata for funsapp.urls from dump/funsapp/urls.metadata.json
2021-03-27T02:29:20.125+0000    finished restoring funsapp.links (37 documents, 0 failures)
2021-03-27T02:29:20.127+0000    reading metadata for funsapp.promotions from dump/funsapp/promotions.metadata.json
2021-03-27T02:29:20.199+0000    [##......................]  funsapp.sessions   787KB/6.22MB   (12.4%)
2021-03-27T02:29:20.199+0000    [########################]     funsapp.stats  1.44MB/1.44MB  (100.0%)
2021-03-27T02:29:20.199+0000
2021-03-27T02:29:20.236+0000    restoring funsapp.urls from dump/funsapp/urls.bson
2021-03-27T02:29:20.270+0000    restoring funsapp.promotions from dump/funsapp/promotions.bson
2021-03-27T02:29:20.326+0000    no indexes to restore
2021-03-27T02:29:20.330+0000    finished restoring funsapp.urls (8 documents, 0 failures)
2021-03-27T02:29:20.334+0000    reading metadata for funsapp.tasks from dump/funsapp/tasks.metadata.json
2021-03-27T02:29:20.360+0000    [########################]  funsapp.stats  1.44MB/1.44MB  (100.0%)
2021-03-27T02:29:20.360+0000    no indexes to restore
2021-03-27T02:29:20.364+0000    finished restoring funsapp.stats (3338 documents, 0 failures)
2021-03-27T02:29:20.369+0000    no indexes to restore
2021-03-27T02:29:20.373+0000    finished restoring funsapp.promotions (9 documents, 0 failures)
2021-03-27T02:29:20.443+0000    restoring funsapp.tasks from dump/funsapp/tasks.bson
2021-03-27T02:29:20.555+0000    no indexes to restore
2021-03-27T02:29:20.559+0000    finished restoring funsapp.tasks (3 documents, 0 failures)
2021-03-27T02:29:23.199+0000    [#######.................]  funsapp.sessions  1.92MB/6.22MB  (30.9%)
2021-03-27T02:29:26.199+0000    [#############...........]  funsapp.sessions  3.45MB/6.22MB  (55.5%)
2021-03-27T02:29:29.199+0000    [###################.....]  funsapp.sessions  5.18MB/6.22MB  (83.2%)
2021-03-27T02:29:30.964+0000    [########################]  funsapp.sessions  6.22MB/6.22MB  (100.0%)
2021-03-27T02:29:30.965+0000    restoring indexes for collection funsapp.sessions from metadata
2021-03-27T02:29:31.244+0000    finished restoring funsapp.sessions (32454 documents, 0 failures)
2021-03-27T02:29:31.245+0000    41209 document(s) restored successfully. 0 document(s) failed to restore.
mongorestore.png

本文链接 https://www.yidiankuaile.com/post/mongodb-mongodump-mongorestore

最后更新