前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MongoDB系列之复制数据库和Collection

MongoDB系列之复制数据库和Collection

作者头像
SmileNicky
发布2022-06-15 08:00:25
1.4K0
发布2022-06-15 08:00:25
举报
文章被收录于专栏:Nicky's blog

1、描述

描述:最近在批量执行一些MongoDB的脚本,所以考虑执行之前先备份一下数据库,但是MongoDB的备份命令不太熟,又是生产环境,就不想去尝试了,直接进行数据库复制和collection复制即可

环境:

  • MongoDB 4.0.10 查看MongoDB版本命令:db.version();

但是发现生产环境的MongoDB部署到docker里面,而且因为安全问题,端口也不对外开放,所以就不能使用Navicat这些客户端软件直接连接

有Navicat的话,我复制数据库和collection的,直接右键->复制集合->数据和结构就搞定了,但是没有Navicat可以使用,只能使用命令行了

2、复制数据库

复制数据库,4.0以下版本可以使用copyDatabase

代码语言:javascript
复制
db.copyDatabase(<from_dbname>, <to_dbname>, <from_hostname>);

但是生产环境使用的版本是4.0以上的,所以不能使用这个copyDatabase命令,然后可以使用什么命令替代?可以参考文档:https://www.mongodb.com/docs/manual/release-notes/4.0-compatibility/#remove-support-for-the-copydb-and-clone-commands

在官方文档里找到mongodumpmongorestore可以进行数据库备份,用于mongodump将test数据库转储到存档mongodump-test-db:

代码语言:javascript
复制
 mongodump --host="127.0.0.1:27017" --archive="mongodump-test-db" --db=test;

root@0ac56:/# mongodump --host=“127.0.0.1:27017” --archive=“mongodump-test-db” --db=test; 2022-06-14T06:26:23.529+0000 writing test.provinceCataLogModel_test to archive ‘mongodump-test-db’ 2022-06-14T06:26:23.531+0000 writing test.provinceCataLogModel_prod to archive ‘mongodump-test-db’ 2022-06-14T06:26:23.531+0000 writing test.provinceCataLogModel to archive ‘mongodump-test-db’ 2022-06-14T06:26:23.532+0000 writing test.user to archive ‘mongodump-test-db’ 2022-06-14T06:26:23.535+0000 done dumping test.provinceCataLogModel_test (10 documents) 2022-06-14T06:26:23.535+0000 writing test.inventory to archive ‘mongodump-test-db’ 2022-06-14T06:26:23.536+0000 done dumping test.user (9 documents) 2022-06-14T06:26:23.536+0000 writing test.pullLog to archive ‘mongodump-test-db’ 2022-06-14T06:26:23.538+0000 done dumping test.inventory (6 documents) 2022-06-14T06:26:23.538+0000 writing test.orders to archive ‘mongodump-test-db’ 2022-06-14T06:26:23.538+0000 done dumping test.provinceCataLogModel (10 documents) 2022-06-14T06:26:23.538+0000 done dumping test.pullLog (2 documents) 2022-06-14T06:26:23.627+0000 done dumping test.orders (2 documents) 2022-06-14T06:26:23.656+0000 done dumping test.provinceCataLogModel_prod (2253 documents)

使用mongorestore和从存档中恢复,nsFrom数据库名称注意修改,nsTo是恢复出来的新的数据库名称

代码语言:javascript
复制
mongorestore --host="127.0.0.1:27017" --archive="mongodump-test-db" --nsFrom='test.*' --nsTo='examples.*';

root@0ac56:/# mongorestore --host=“127.0.0.1:27017” --archive=“mongodump-test-db” --nsFrom=‘test.’ --nsTo='examples.’; 2022-06-14T06:28:58.300+0000 preparing collections to restore from 2022-06-14T06:28:58.328+0000 reading metadata for examples.provinceCataLogModel_test from archive ‘mongodump-test-db’ 2022-06-14T06:28:58.392+0000 restoring examples.provinceCataLogModel_test from archive ‘mongodump-test-db’ 2022-06-14T06:28:58.408+0000 restoring indexes for collection examples.provinceCataLogModel_test from metadata 2022-06-14T06:28:58.460+0000 finished restoring examples.provinceCataLogModel_test (10 documents) 2022-06-14T06:28:58.460+0000 reading metadata for examples.user from archive ‘mongodump-test-db’ 2022-06-14T06:28:58.506+0000 restoring examples.user from archive ‘mongodump-test-db’ 2022-06-14T06:28:58.527+0000 reading metadata for examples.inventory from archive ‘mongodump-test-db’ 2022-06-14T06:28:58.528+0000 no indexes to restore 2022-06-14T06:28:58.528+0000 finished restoring examples.user (9 documents) 2022-06-14T06:28:58.566+0000 restoring examples.inventory from archive ‘mongodump-test-db’ 2022-06-14T06:28:58.570+0000 no indexes to restore 2022-06-14T06:28:58.570+0000 finished restoring examples.inventory (6 documents) 2022-06-14T06:28:58.578+0000 reading metadata for examples.provinceCataLogModel from archive ‘mongodump-test-db’ 2022-06-14T06:28:58.587+0000 restoring examples.provinceCataLogModel from archive ‘mongodump-test-db’ 2022-06-14T06:28:58.601+0000 reading metadata for examples.pullLog from archive ‘mongodump-test-db’ 2022-06-14T06:28:58.663+0000 restoring examples.pullLog from archive ‘mongodump-test-db’ 2022-06-14T06:28:58.664+0000 restoring indexes for collection examples.provinceCataLogModel from metadata 2022-06-14T06:28:58.674+0000 reading metadata for examples.orders from archive ‘mongodump-test-db’ 2022-06-14T06:28:58.729+0000 finished restoring examples.provinceCataLogModel (10 documents) 2022-06-14T06:28:58.764+0000 restoring examples.orders from archive ‘mongodump-test-db’ 2022-06-14T06:28:58.767+0000 reading metadata for examples.provinceCataLogModel_prod from archive ‘mongodump-test-db’ 2022-06-14T06:28:58.767+0000 no indexes to restore 2022-06-14T06:28:58.767+0000 finished restoring examples.pullLog (2 documents) 2022-06-14T06:28:58.794+0000 restoring examples.provinceCataLogModel_prod from archive ‘mongodump-test-db’ 2022-06-14T06:28:58.794+0000 no indexes to restore 2022-06-14T06:28:58.795+0000 finished restoring examples.orders (2 documents) 2022-06-14T06:28:59.010+0000 restoring indexes for collection examples.provinceCataLogModel_prod from metadata 2022-06-14T06:28:59.524+0000 finished restoring examples.provinceCataLogModel_prod (2253 documents) 2022-06-14T06:28:59.524+0000 done

3、复制Collection

  • 使用循环的方式
代码语言:javascript
复制
db.collection1.find().forEach(function(x){
    db.collection2.insert(x);
});
  • 只能将集合克隆到同一服务器。
  • 速度相对比较慢
  • 不复制集合属性和索引
  • 使用copyTo命令
代码语言:javascript
复制
db.collection1.copyTo("collection2");
  • 只能将集合克隆到同一服务器。
  • 速度相对比较慢
  • 不复制集合属性和索引
  • 只能针对 MongoDB 4.0 或更早版本运行
  • 使用aggregate命令
代码语言:javascript
复制
db.collection1.aggregate([{ $match: {} }, { $out: "collection2" }])
  • 只能将集合克隆到同一服务器。
  • 速度相对比较快
  • 不复制集合属性和索引
  • mongodump和mongorestore
代码语言:javascript
复制
mongodump --host <host> --port <port> --db test --collection collection1 --out "\opt\out"
mongorestore --host <主机> --port <端口> --db test --collection collection2 "\opt\out\test\collection1.bson"
  • 速度相对比较快
  • 不复制集合属性和索引
  • 可以将集合克隆到另一个数据库和服务器
  • mongoexport 和mongoimport
代码语言:javascript
复制
mongoexport /host:<host> /port:<port> /db:test /collection:collection1 /out:collection1.json
mongoimport /host:<host> /port:<port> /db:test /collection:collection2 /file:collection1.json
  • 速度相对比较快
  • 不复制集合属性和索引
  • 可以将集合克隆到另一个数据库和服务器
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-06-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、描述
  • 2、复制数据库
  • 3、复制Collection
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档