教程:使用 DMS 将 MongoDB 脱机迁移到 Azure Cosmos DB 的用于 MongoDB 的 APITutorial: Migrate MongoDB to Azure Cosmos DB's API for MongoDB offline using DMS

可以使用 Azure 数据库迁移服务将数据库从 MongoDB 的本地或云实例脱机(一次性)迁移到 Azure Cosmos DB 的用于 MongoDB 的 API。You can use Azure Database Migration Service to perform an offline (one-time) migration of databases from an on-premises or cloud instance of MongoDB to Azure Cosmos DB's API for MongoDB.

本教程介绍如何执行下列操作:In this tutorial, you learn how to:

  • 创建 Azure 数据库迁移服务的实例。Create an instance of Azure Database Migration Service.
  • 使用 Azure 数据库迁移服务创建迁移项目。Create a migration project by using Azure Database Migration Service.
  • 运行迁移。Run the migration.
  • 监视迁移。Monitor the migration.

在本教程中,你将使用 Azure 数据库迁移服务,将 Azure 虚拟机中托管的 MongoDB 中的某个数据集迁移到 Azure Cosmos DB 的用于 MongoDB 的 API。In this tutorial, you migrate a dataset in MongoDB hosted in an Azure Virtual Machine to Azure Cosmos DB's API for MongoDB by using Azure Database Migration Service. 如果尚未设置 MongoDB 源,请参阅在 Azure 中的 Windows VM 上安装和配置 MongoDB 一文。If you don't have a MongoDB source set up already, see the article Install and configure MongoDB on a Windows VM in Azure.

先决条件Prerequisites

要完成本教程,需要:To complete this tutorial, you need to:

  • 完成迁移前步骤,例如估计吞吐量、选择分区键和索引策略。Complete the pre-migration steps such as estimating throughput, choosing a partition key, and the indexing policy.

  • 创建 Azure Cosmos DB 的用于 MongoDB 的 API 帐户Create an Azure Cosmos DB's API for MongoDB account.

  • 使用 Azure 资源管理器部署模型创建 Azure 数据库迁移服务的 Azure 虚拟网络,该网络将使用 ExpressRouteVPN 提供与本地源服务器的站点到站点连接。Create an Azure Virtual Network for Azure Database Migration Service by using Azure Resource Manager deployment model, which provides site-to-site connectivity to your on-premises source servers by using either ExpressRoute or VPN. 有关创建虚拟网络的详细信息,请参阅虚拟网络文档,尤其是提供了分步详细信息的快速入门文章。For more information about creating a virtual network, see the Virtual Network Documentation, and especially the quickstart articles with step-by-step details.

    Note

    在设置虚拟网络期间,如果将 ExpressRoute 与 Azure 的网络对等互连一起使用,请将以下服务终结点添加到将在其中预配服务的子网:During virtual network setup, if you use ExpressRoute with network peering to Azure, add the following service endpoints to the subnet in which the service will be provisioned:

    • 目标数据库终结点(例如,SQL 终结点、Cosmos DB 终结点等)Target database endpoint (for example, SQL endpoint, Cosmos DB endpoint, and so on)
    • 存储终结点Storage endpoint
    • 服务总线终结点Service bus endpoint

    Azure 数据库迁移服务缺少 Internet 连接,因此必须提供此配置。This configuration is necessary because Azure Database Migration Service lacks internet connectivity.

  • 确保虚拟网络的网络安全组 (NSG) 规则未阻止以下通信端口:53、443、445、9354 以及 10000-20000。Ensure that your virtual network Network Security Group (NSG) rules don't block the following communication ports: 53, 443, 445, 9354, and 10000-20000. 有关虚拟网络 NSG 流量筛选的更多详细信息,请参阅使用网络安全组筛选网络流量一文。For more detail on virtual network NSG traffic filtering, see the article Filter network traffic with network security groups.

  • 打开 Windows 防火墙,使 Azure 数据库迁移服务能够访问源 MongoDB 服务器(默认情况下为 TCP 端口 27017)。Open your Windows firewall to allow Azure Database Migration Service to access the source MongoDB server, which by default is TCP port 27017.

  • 在源数据库的前面使用了防火墙设备时,可能需要添加防火墙规则以允许 Azure 数据库迁移服务访问要迁移的源数据库。When using a firewall appliance in front of your source database(s), you may need to add firewall rules to allow Azure Database Migration Service to access the source database(s) for migration.

注册 Microsoft.DataMigration 资源提供程序Register the Microsoft.DataMigration resource provider

  1. 登录到 Azure 门户,选择“所有服务” ,然后选择“订阅” 。Sign in to the Azure portal, select All services, and then select Subscriptions.

    显示门户订阅

  2. 选择要在其中创建 Azure 数据库迁移服务实例的订阅,再选择“资源提供程序” 。Select the subscription in which you want to create the instance of the Azure Database Migration Service, and then select Resource providers.

    显示资源提供程序

  3. 搜索“迁移”,然后选择“注册” 。Search for migration, and then select Register.

    注册资源提供程序

创建实例Create an instance

  1. 在 Azure 门户中,选择 + 创建资源,搜索 Azure 数据库迁移服务,然后从下拉列表选择Azure 数据库迁移服务In the Azure portal, select + Create a resource, search for Azure Database Migration Service, and then select Azure Database Migration Service from the drop-down list.

    Azure 市场

  2. 在“Azure 数据库迁移服务”屏幕上,选择“创建” 。On the Azure Database Migration Service screen, select Create.

    创建 Azure 数据库迁移服务实例

  3. 在“创建迁移服务”屏幕上,为服务、订阅以及新的或现有资源组指定名称 。On the Create Migration Service screen, specify a name for the service, the subscription, and a new or existing resource group.

  4. 选择要在其中创建 Azure 数据库迁移服务实例的位置。Select the location in which you want to create the instance of Azure Database Migration Service.

  5. 选择现有虚拟网络或新建一个。Select an existing virtual network or create a new one.

    虚拟网络为 Azure 数据库迁移服务提供了对源 MongoDB 实例和目标 Azure Cosmos DB 帐户的访问权限。The virtual network provides Azure Database Migration Service with access to the source MongoDB instance and the target Azure Cosmos DB account.

    有关如何在 Azure 门户中创建虚拟网络的详细信息,请参阅使用 Azure 门户创建虚拟网络一文。For more information about how to create a virtual network in the Azure portal, see the article Create a virtual network using the Azure portal.

  6. 选择定价层。Select a pricing tier.

    有关成本和定价层的详细信息,请参阅价格页For more information on costs and pricing tiers, see the pricing page.

    配置 Azure 数据库迁移服务实例设置

  7. 选择“创建” 来创建服务。Select Create to create the service.

创建迁移项目Create a migration project

创建服务后,在 Azure 门户中找到并打开它,然后创建一个新的迁移项目。After the service is created, locate it within the Azure portal, open it, and then create a new migration project.

  1. 在 Azure 门户中,选择“所有服务” ,搜索 Azure 数据库迁移服务,然后选择“Azure 数据库迁移服务” 。In the Azure portal, select All services, search for Azure Database Migration Service, and then select Azure Database Migration Services.

    查找 Azure 数据库迁移服务的所有实例

  2. 在“Azure 数据库迁移服务”屏幕上,搜索你创建的 Azure 数据库迁移服务实例名称,然后选择该实例 。On the Azure Database Migration Services screen, search for the name of Azure Database Migration Service instance that you created, and then select the instance.

  3. 选择“+ 新建迁移项目” 。Select + New Migration Project.

  4. 在“新建迁移项目”屏幕上指定项目名称,在“源服务器类型”文本框中选择“MongoDB”,在“目标服务器类型”文本框中选择“CosmosDB (MongoDB API)”,然后在“选择活动类型”中选择“脱机数据迁移”。 On the New migration project screen, specify a name for the project, in the Source server type text box, select MongoDB, in the Target server type text box, select CosmosDB (MongoDB API), and then for Choose type of activity, select Offline data migration.

    创建数据库迁移服务项目

  5. 选择“创建并运行活动”,以便创建项目并运行迁移活动。 Select Create and run activity to create the project and run the migration activity.

指定源详细信息Specify source details

  1. 在“源详细信息” 屏幕上,指定源 MongoDB 服务器的连接详细信息。On the Source details screen, specify the connection details for the source MongoDB server.

    Important

    Azure 数据库迁移服务不支持将 Azure Cosmos DB 作为源。Azure Database Migration Service does not support Azure Cosmos DB as a source.

    可通过三种模式连接到源:There are three modes to connect to a source:

    • 标准模式:接受完全限定的域名或 IP 地址、端口号和连接凭据。Standard mode, which accepts a fully qualified domain name or an IP address, Port number, and connection credentials.

    • 连接字符串模式:接受连接字符串 URI 格式一文中所述的 MongoDB 连接字符串。Connection string mode, which accepts a MongoDB Connection string as described in the article Connection String URI Format.

    • Azure 存储中的数据:接受 Blob 容器 SAS URL。Data from Azure storage, which accepts a blob container SAS URL. 如果 Blob 容器包含 MongoDB bsondump 工具生成的 BSON 转储,请选择“Blob 包含 BSON 转储”;如果容器包含 JSON 文件,请取消选择该选项。 Select Blob contains BSON dumps if the blob container has BSON dumps produced by the MongoDB bsondump tool, and de-select it if the container contains JSON files.

      如果选择此选项,则请确保存储帐户连接字符串按以下格式显示:If you select this option, be sure that the storage account connection string appears in the format:

      https://blobnameurl/container?SASKEY
      

      可以在 Azure 存储资源管理器中找到此 blob 容器 SAS 连接字符串。This blob container SAS connection string can be found in Azure Storage explorer. 为相关容器创建 SA 将为你提供上述请求格式的 URL。Creating the SAS for the concerned container will provide you the URL in above requested format.

      此外,根据 Azure 存储中的类型转储选项,记住以下详细信息。Also, based on the type dump information in Azure Storage, keep the following detail in mind.

      • 对于 BSON 转储,blob 容器中的数据必须采用 bsondump 格式,这样数据文件才会放置到按 collection.bson 格式以包含数据库命令的文件夹中。For BSON dumps, the data within the blob container must be in bsondump format, such that data files are placed into folders named after the containing databases in the format collection.bson. 元数据文件(如有)应采用 collection.metadata.json 格式进行命名 。Metadata files (if any) should be named using the format collection.metadata.json.

      • 对于 JSON 转储,blob 容器中的文件必须放置到以包含数据库命名的文件夹中。For JSON dumps, the files in the blob container must be placed into folders named after the containing databases. 在每个数据库文件夹中,数据文件必须放置到名为“数据”且采用 collection.json 格式命名的子文件夹中 。Within each database folder, data files must be placed in a subfolder called "data" and named using the format collection.json. 元数据文件(如有)必须放置到名为“元数据”且同样采用 collection.json 格式命名的子文件夹中 。Metadata files (if any) must be placed in a subfolder called "metadata" and named using the same format, collection.json. 元数据文件必须采用由 MongoDB bsondump 工具所生成的相同格式。The metadata files must be in the same format as produced by the MongoDB bsondump tool.

    Important

    建议不要在 mongo 服务器上使用自签名证书。It is discouraged to use a self-signed certificate on the mongo server. 但是,如果使用了自签名证书,请使用连接字符串模式连接到服务器,并确保连接字符串包含 ""However, if one is used, please connect to the server using connection string mode and ensure that your connection string has ""

    &sslVerifyCertificate=false
    

    即使不能使用 DNS 名称解析,也可使用 IP 地址。You can also use the IP Address for situations in which DNS name resolution isn't possible.

    指定源详细信息

  2. 选择“保存” 。Select Save.

指定目标详细信息Specify target details

  1. 在“迁移目标详细信息”屏幕上,指定目标 Azure Cosmos DB 帐户的连接详细信息,该帐户是要将 MongoDB 数据迁移到其中的已提前预配的 Azure Cosmos DB 的用于 MongoDB 的 API 帐户 。On the Migration target details screen, specify the connection details for the target Azure Cosmos DB account, which is the pre-provisioned Azure Cosmos DB's API for MongoDB account to which you're migrating your MongoDB data.

    指定目标详细信息

  2. 选择“保存” 。Select Save.

映射到目标数据库Map to target databases

  1. 在“映射到目标数据库” 屏幕上,映射源和目标数据库以进行迁移。On the Map to target databases screen, map the source and the target database for migration.

    如果目标数据库包含的数据库名称与源数据库的相同,则 Azure 数据库迁移服务默认会选择目标数据库。If the target database contains the same database name as the source database, Azure Database Migration Service selects the target database by default.

    如果字符串 Create 显示在数据库名称旁边,则表明 Azure 数据库迁移服务没有找到目标数据库,因此会为你创建该数据库。If the string Create appears next to the database name, it indicates that Azure Database Migration Service didn't find the target database, and the service will create the database for you.

    在迁移的这个时候,可以预配吞吐量At this point in the migration, you can provision throughput. 在 Cosmos DB 中,可在数据库级别预配吞吐量,也可为每个集合单独进行预配。In Cosmos DB, you can provision throughput either at the database-level or individually for each collection. 吞吐量以请求单位 (RU) 来度量。Throughput is measured in Request Units (RUs). 深入了解 Azure Cosmos DB 定价Learn more about Azure Cosmos DB pricing.

    映射到目标数据库

  2. 选择“保存” 。Select Save.

  3. 在“集合设置”屏幕上,展开集合列表,然后查看要迁移的集合的列表。 On the Collection setting screen, expand the collections listing, and then review the list of collections that will be migrated.

    Azure 数据库迁移服务会自动选择存在于源 MongoDB 实例上但不存在于目标 Azure Cosmos DB 帐户上的所有集合。Azure Database Migration Service auto selects all the collections that exist on the source MongoDB instance that don't exist on the target Azure Cosmos DB account. 若要重新迁移已含数据的集合,需要在此边栏选项卡上明确选择集合。If you want to remigrate collections that already include data, you need to explicitly select the collections on this blade.

    可以指定希望集合使用的 RU 量。You can specify the amount of RUs that you want the collections to use. Azure 数据库迁移服务建议使用根据集合大小设置的智能默认值。Azure Database Migration Service suggests smart defaults based on the collection size.

    Note

    必要时使用 Azure 数据库迁移服务的多个实例并行执行数据库迁移和集合,以便加快运行速度。Perform the database migration and collection in parallel using multiple instances of Azure Database Migration Service, if necessary, to speed up the run.

    也可指定分片键来利用 Azure Cosmos DB 中的分区,以便优化可伸缩性。You can also specify a shard key to take advantage of partitioning in Azure Cosmos DB for optimal scalability. 确保查看选择分片/分区键的最佳做法Be sure to review the best practices for selecting a shard/partition key.

    选择集合表

  4. 选择“保存” 。Select Save.

  5. 在“迁移摘要”屏幕的“活动名称”文本框中指定迁移活动的名称。 On the Migration summary screen, in the Activity name text box, specify a name for the migration activity.

    迁移摘要

运行迁移Run the migration

  • 选择“运行迁移” 。Select Run migration.

    迁移活动窗口随即出现,活动的“状态”为“未启动” 。The migration activity window appears, and the Status of the activity is Not started.

    活动状态

监视迁移Monitor the migration

  • 在迁移活动屏幕上,选择“刷新” 来更新显示,直到迁移的“状态” 显示为“已完成” 。On the migration activity screen, select Refresh to update the display until the Status of the migration shows as Completed.

    Note

    可以选择“活动”,以便获取数据库级别和集合级别迁移指标的详细信息。You can select the Activity to get details of database- and collection-level migration metrics.

    活动状态为“已完成”

验证 Cosmos DB 中的数据Verify data in Cosmos DB

  • 在迁移完成后,可以检查你的 Azure Cosmos DB 帐户,验证所有集合是否已成功迁移。After the migration completes, you can check your Azure Cosmos DB account to verify that all the collections were migrated successfully.

    活动状态为“已完成”

迁移后优化Post-migration optimization

将 MongoDB 数据库中存储的数据迁移到 Azure Cosmos DB 的 API for MongoDB 后,你可以连接到 Azure Cosmos DB 并管理数据。After you migrate the data stored in MongoDB database to Azure Cosmos DB's API for MongoDB, you can connect to Azure Cosmos DB and manage the data. 你还可以执行其他迁移后优化步骤,例如优化索引策略,为你的 Azure Cosmos DB 帐户更新默认一致性级别或配置全局分发。You can also perform other post-migration optimization steps such as optimizing the indexing policy, update the default consistency level, or configure global distribution for your Azure Cosmos DB account. 有关详细信息,请参阅迁移后优化一文。For more information, see the Post-migration optimization article.

其他资源Additional resources

后续步骤Next steps