如何使用 PowerShell 在 Azure Database for MySQL 中创建和管理只读副本How to create and manage read replicas in Azure Database for MySQL using PowerShell

备注

将要查看的是 Azure Database for MySQL 的新服务。You are viewing the new service of Azure Database for MySQL. 若要查看经典 MySQL Database for Azure 的文档,请访问此页To view the documentation for classic MySQL Database for Azure, please visit this page.

本文介绍如何使用 PowerShell 在 Azure Database for MySQL 服务中创建和管理只读副本。In this article, you learn how to create and manage read replicas in the Azure Database for MySQL service using PowerShell. 若要详细了解只读副本,请参阅概述To learn more about read replicas, see the overview.

Azure PowerShellAzure PowerShell

可以使用 PowerShell 创建和管理只读副本。You can create and manage read replicas using PowerShell.

先决条件Prerequisites

若要完成本操作指南,需要:To complete this how-to guide, you need:

重要

尽管 Az.MySql PowerShell 模块为预览版,但必须使用以下命令从 Az PowerShell 模块单独安装它:Install-Module -Name Az.MySql -AllowPrereleaseWhile the Az.MySql PowerShell module is in preview, you must install it separately from the Az PowerShell module using the following command: Install-Module -Name Az.MySql -AllowPrerelease. Az.MySql PowerShell 模块正式版推出后,它会包含在将来的 Az PowerShell 模块发行版中。Once the Az.MySql PowerShell module is generally available, it becomes part of future Az PowerShell module releases.

使用 Connect-AzAccount cmdlet 连接到 Azure 帐户。Connect to your Azure account using the Connect-AzAccount cmdlet.

重要

只读副本功能仅适用于“常规用途”或“内存优化”定价层中的 Azure Database for MySQL 服务器。The read replica feature is only available for Azure Database for MySQL servers in the General Purpose or Memory Optimized pricing tiers. 请确保主服务器位于其中一个定价层中。Ensure the master server is in one of these pricing tiers.

创建只读副本Create a read replica

重要

如果为没有现有副本的主服务器创建副本,主服务器将首先重启以便为复制准备自身。When you create a replica for a master that has no existing replicas, the master will first restart to prepare itself for replication. 请考虑这一点并在非高峰期执行这些操作。Take this into consideration and perform these operations during an off-peak period.

可以使用以下命令创建只读副本服务器:A read replica server can be created using the following command:

Get-AzMySqlServer -Name mydemoserver -ResourceGroupName myresourcegroup |
  New-AzMySqlReplica -Name mydemoreplicaserver -ResourceGroupName myresourcegroup

New-AzMySqlReplica 命令需要以下参数:The New-AzMySqlReplica command requires the following parameters:

设置Setting 示例值Example value 说明Description  
ResourceGroupNameResourceGroupName  myresourcegroupmyresourcegroup  在其中创建副本服务器的资源组。The resource group where the replica server is created. 
名称Name mydemoreplicaservermydemoreplicaserver 所创建的新副本服务器的名称。The name of the new replica server that is created.

若要创建跨区域只读副本,请使用 Location 参数。To create a cross region read replica, use the Location parameter. 以下示例在“中国北部”区域中创建一个副本。The following example creates a replica in the China North region.

Get-AzMySqlServer -Name mrdemoserver -ResourceGroupName myresourcegroup |
  New-AzMySqlReplica -Name mydemoreplicaserver -ResourceGroupName myresourcegroup -Location chinanorth

若要详细了解可以在哪些区域中创建副本,请访问只读副本概念文章To learn more about which regions you can create a replica in, visit the read replica concepts article.

默认情况下,除非指定了 Sku 参数,否则将使用与主服务器相同的服务器配置来创建只读副本。By default, read replicas are created with the same server configuration as the master unless the Sku parameter is specified.

备注

建议副本服务器的配置应保持在与主服务器相同或更大的值,以确保副本能够跟上主服务器。It is recommended that the replica server's configuration should be kept at equal or greater values than the master to ensure the replica is able to keep up with the master.

列出主服务器的副本List replicas for a master server

若要查看给定的主服务器的所有副本,请运行以下命令:To view all replicas for a given master server, run the following command:

Get-AzMySqlReplica -ResourceGroupName myresourcegroup -ServerName mydemoserver

Get-AzMySqlReplica 命令需要以下参数:The Get-AzMySqlReplica command requires the following parameters:

设置Setting 示例值Example value 说明Description  
ResourceGroupNameResourceGroupName  myresourcegroupmyresourcegroup  要在其中创建副本服务器的资源组。The resource group where the replica server will be created to. 
ServerNameServerName mydemoservermydemoserver 主服务器的名称或 ID。The name or ID of the master server.

删除副本服务器Delete a replica server

可以通过运行 Remove-AzMySqlServer cmdlet 来删除只读副本服务器。Deleting a read replica server can be done by running the Remove-AzMySqlServer cmdlet.

Remove-AzMySqlServer -Name mydemoreplicaserver -ResourceGroupName myresourcegroup

删除主服务器Delete a master server

重要

删除主服务器会停止复制到所有副本服务器,并删除主服务器本身。Deleting a master server stops replication to all replica servers and deletes the master server itself. 副本服务器成为现在支持读取和写入的独立服务器。Replica servers become standalone servers that now support both read and writes.

若要删除主服务器,可以运行 Remove-AzMySqlServer cmdlet。To delete a master server, you can run the Remove-AzMySqlServer cmdlet.

Remove-AzMySqlServer -Name mydemoserver -ResourceGroupName myresourcegroup

后续步骤Next steps