mongodump 与 mongorestore 方式
更新时间:2025-10-28 08:01:00
PDF
MongoDB 支持使用 mongodump 和 mongorestore 工具,实现 MongoDB 数据迁移。
-
mongodump是一个数据导出工具,可以将 MongoDB 中的数据导出。 -
mongorestore是一个数据导入工具,可以将mongodump导出的数据导入到 MongoDB 中,实现 MongoDB 数据的恢复。
本小节主要简单介绍通过 MongoDB 工具导入和导出数据方法。
前提条件
操作命令
执行如下命令,可将数据在两个数据库之间迁移。
$ mongodump --archive --username=<username_cluster1> --password=<password_cluster1> --authenticationDatabase=admin --host=<node_ip_cluster1> --db=<db_name_cluster1>
| mongorestore --archive --username=<username_cluster2> --password=<password_cluster2> --authenticationDatabase=admin --host=<node_ip_cluster2> --db=<db_name_cluster2>
以下示例,将集群 A 的数据导入到集群 B 中。
$ mongodump --archive --username=user001 --password=Pwd00001 --authenticationDatabase=admin --host=192.168.110.101 --db=db1
| mongorestore --archive --username=user002 --password=Pwd00002 --authenticationDatabase=admin --host=192.168.110.102 --db=db2
其中:
-
集群 A 用户名为
user001,密码为Pwd00001,节点 IP 为192.168.110.101,验证数据库为admin,导出数据库minor为db1。 -
集群 B 用户名为
user002,密码为Pwd00002,节点 IP 为192.168.110.102,验证数据库为admin,导入数据库名为db2。