MongoDB系列之复制数据库和Collection

x33g5p2x  于2022-06-16 转载在 其他  
字(5.5k)|赞(0)|评价(0)|浏览(480)

1、描述

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

环境:

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

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

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

2、复制数据库

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

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:

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是恢复出来的新的数据库名称

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

  • 使用循环的方式
db.collection1.find().forEach(function(x){
    db.collection2.insert(x);
});
  • 只能将集合克隆到同一服务器。

  • 速度相对比较慢

  • 不复制集合属性和索引

  • 使用copyTo命令

db.collection1.copyTo("collection2");
  • 只能将集合克隆到同一服务器。

  • 速度相对比较慢

  • 不复制集合属性和索引

  • 只能针对 MongoDB 4.0 或更早版本运行

  • 使用aggregate命令

db.collection1.aggregate([{ $match: {} }, { $out: "collection2" }])
  • 只能将集合克隆到同一服务器。

  • 速度相对比较快

  • 不复制集合属性和索引

  • mongodump和mongorestore

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

mongoexport /host:<host> /port:<port> /db:test /collection:collection1 /out:collection1.json
mongoimport /host:<host> /port:<port> /db:test /collection:collection2 /file:collection1.json
  • 速度相对比较快
  • 不复制集合属性和索引
  • 可以将集合克隆到另一个数据库和服务器

相关文章