You can use mongodump and mongorestore to seed MongoDB Atlas cluster with data from an existing MongoDB standalone or replica set. For guidance on seeding data from an existing MongoDB sharded cluster, contact Atlas support:
在 Atlas 中,转到 Support(项目设置)页面。
如果尚未显示,组织从导航栏中的Organizations菜单。
单击导航栏中的 Support 图标。
单击 View plan(连接)。
显示“支持”页面。
While you can scale a Free cluster (formerly known as M0) to an M10+ paid cluster using the Atlas UI, you can also use mongodump and mongorestore procedures in this section to copy data from a Free cluster to an M10+ cluster.
关于此任务
建议的 mongodump 和 mongorestore 版本
Use the latest stable release version of mongodump and mongorestore for this procedure.
所需停机时间
To ensure an up-to-date migration, schedule a maintenance window where you can stop all writes to your source cluster. Any write operations issued to the source cluster after the mongodump portion of the procedure completes are not migrated to the destination cluster.
After mongorestore completes data restoration, you must cut-over your applications to the destination Atlas cluster before resuming write operations. To connect to an Atlas cluster, see Connect to an Atlas Cluster.
所需的总停机时间取决于多种因素,例如要迁移数据的大小以及源集群与 Atlas 之间的网络连接。如果您对延长的停机时间有任何疑问或顾虑,请联系 Atlas 支持部门:
在 Atlas 中,转到 Support(项目设置)页面。
如果尚未显示,组织从导航栏中的Organizations菜单。
单击导航栏中的 Support 图标。
单击 View plan(连接)。
显示“支持”页面。
集群安全性
Atlas 管理数据库用户创建。如果源集群强制执行身份验证:
允许对主节点进行读取访问。
If you want to use
mongorestorewith the--oplogReplayoption, you must delete theadminandconfigdirectories from thedumpdirectory thatmongodumpcreates. Theadminandconfigdirectories contain database user information that you can't add to an Atlas cluster withmongorestore. Use themongorestore--nsExcludeto exclude theadmin.system.*namespace.
您不能将任何现有用户或角色信息迁移到 Atlas。为目标 Atlas 集群创建适当的数据库用户,支持应用程序的使用模式。作为切换过程的一部分,更新应用程序,使用新的数据库用户。要了解更多信息,请参阅配置数据库用户。
性能
This procedure requires running mongodump and mongorestore on a host in the source cluster. These programs use system resources such as CPU and memory, and may impact the performance of the host.
在非系统使用高峰期或计划维护窗口期间运行此程序。如果来源是副本集,则可以在从节点的主机上运行此过程。停止写入集群后,在启动此过程之前,让从节点集群赶上主节点集群。
管道行为
This procedure uses linux pipes to stream the output of mongodump to mongorestore. If the mongorestore process can't keep up with the mongodump process, you may see broken pipe errors.
有关解决持续性管道中断错误的指导,请联系 Atlas 支持部门:
在 Atlas 中,转到 Support(项目设置)页面。
如果尚未显示,组织从导航栏中的Organizations菜单。
单击导航栏中的 Support 图标。
单击 View plan(连接)。
显示“支持”页面。
步骤
The following tutorial uses mongodump and mongorestore to upload data from an existing MongoDB cluster to an Atlas cluster:
在源副本集中创建数据库用户。
重要
Optional
如果您的源集群不强制执行身份验证,则跳过此步骤。
如果源部署强制执行身份验证,则必须提供一个在此过程中具有读取任何数据库权限的数据库用户。要了解有关数据库用户权限的更多信息,请参阅 MongoDB 基于角色的访问控制。
If no such user exists, create a user in your source MongoDB replica set with the backup role on the admin database.
汇编 mongodump 命令。
根据您使用的连接字符串类型,将以下模板之一复制到您的首选文本编辑器中:
注意
要连接 Atlas 集群,我们建议您使用 --uri 选项,通过 DNS 种子列表连接字符串进行连接。
mongodump --uri "mongodb://username:password@mongodb0.example.com:<Port>,mongodb1.example.com:<Port1>,mongodb2.example.com:<Port2>/?replicaSet=<ReplicaSetName>&authSource=admin" \ --archive
用副本集节点的信息替换主机示例。将 <ReplicaSetName> 替换为源副本集的名称。
对于独立运行的部署,请排除 replicaSet=<ReplicaSetName> 并仅指定独立运行的部署的主机名。例如,--uri "mongodb://standalone-mongod.example.net:27017"
mongodump --uri "mongodb+srv://username:password@cluster0.example.mongodb.net" \ --archive
注意
mongodumpmongorestore如果在Ubuntu.18 上使用04 或cannot unmarshal DNS ,则在使用带有 选项的 SRV 连接字符串(采用mongodb+srv:// 形式)时,可能会遇到--uri 错误消息。如果是这样,请改用以下选项之一:
the
--urioption with a non-SRV connection string (in the formmongodb://)--host选项指定直接连接的主机
注意
如果密码包含特殊字符,则必须使用百分号编码。
先不要运行此命令。修改模板后请继续下一步操作。
在目标 Atlas 集群中设置数据库用户。
To run mongorestore against an Atlas cluster, you must specify a database user in the Atlas cluster that has the Atlas admin role.
如果不存在该用户,则创建该用户:
在 Atlas 中,进入项目的 Database Access 页面。
如果尚未显示,请从导航栏上的 Organizations 菜单中选择包含项目的组织。
如果尚未显示,请从导航栏的 Projects 菜单中选择您的项目。
在侧边栏中,单击 Security 标题下的 Database & Network Access。
The Database & Network Access page displays.
单击 Add New Database User(添加新的数据库用户)。
添加 Atlas admin 用户。
要了解有关用户管理的更多信息,请参阅配置数据库用户。
AtlasGoClusters在Atlas中,Go项目的 页面。
如果尚未显示,请从导航栏上的 Organizations 菜单中选择包含所需项目的组织。
如果尚未显示,请从导航栏的Projects菜单中选择所需的项目。
在侧边栏中,单击 Database 标题下的 Clusters。
会显示集群页面。
检索并修改 mongorestore 连接模板。
The Binary Import and Export Tools section of the Command Line Tools tab displays a copyable template with the minimum required options for connecting mongorestore to your Atlas cluster.
此模板包含某些选项的占位符值。 将模板复制粘贴到您的首选文本编辑器中, 然后进行以下修改:
password:将其替换为username中指定的用户的密码。该模板包括一个项目数据库用户,作为username的值。如果要以其他用户身份进行身份验证,请替换username的值,并在password中指定该用户的密码。Add
--nsExcludeand set its value to"admin.system.*".Add
--archive.
根据使用的连接字符串类型,您的模板应类似于以下命令之一:
mongorestore --uri "mongodb://username:password@00.foo.mongodb.net:27017,01.foo.mongodb.net:27017,02.foo.mongodb.net:27017/?replicaSet=myRepl&authSource=admin" \ --archive \ --ssl \ --nsExclude "admin.system.*"
mongorestore --uri "mongodb+srv://username:password@cluster1.example.mongodb.net" \ --archive \ --nsExclude "admin.system.*"
Run mongodump and mongorestore.
重要
Ensure that the host where you are running mongodump and mongorestore is in the project IP Access List.
要查看项目 IP 访问列表,请单击侧边栏 Security 部分中的 Network Access。随后会显示 IP Access List 标签页。要了解更多信息,请参阅 IP 访问列表。
In your preferred text editor, use the pipe | operator to separate the mongodump and mongorestore commands. Based on the type of connection string you use, the final command should resemble one of the following:
mongodump --uri "mongodb://username:password@mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?replicaSet=sourceRS&authSource=admin" \ --archive \ | \ mongorestore --uri "mongodb://username:password@00.foo.mongodb.net:27017,01.foo.mongodb.net:27017,02.foo.mongodb.net:27017/?replicaSet=myAtlasRS&authSource=admin" \ --archive \ --ssl \ --nsExclude "admin.system.*"
mongodump --uri "mongodb+srv://username:password@cluster0.example.mongodb.net" \ --archive \ | \ mongorestore --uri "mongodb+srv://username:password@cluster1.example.mongodb.net" \ --archive \ --nsExclude "admin.system.*"
从连接到源群集主机的终端 或 Shell 运行已完成的命令。
Upon successful completion of the procedure, connect to your Atlas cluster using mongosh and verify the result of the procedure. To learn how, see Connect to a Cluster via mongosh.
在恢复写入操作之前,您必须更新应用程序以点Atlas 集群。要学习;了解如何将应用程序连接到Atlas,请参阅通过客户端库连接到集群。