如何使用 Azure CLI 和 REST API 在 Azure Database for MariaDB 中创建和管理只读副本How to create and manage read replicas in Azure Database for MariaDB using the Azure CLI and REST API

本文介绍如何使用 Azure CLI 和 REST API 在 Azure Database for MariaDB 服务中创建和管理只读副本。In this article, you will learn how to create and manage read replicas in the Azure Database for MariaDB service using the Azure CLI and REST API.

Azure CLIAzure CLI

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

先决条件Prerequisites

重要

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

创建只读副本Create a read replica

重要

如果为没有现有副本的源服务器创建副本,源服务器将首先重启,以便为复制做好准备。When you create a replica for a source that has no existing replicas, the source 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:

az mariadb server replica create --name mydemoreplicaserver --source-server mydemoserver --resource-group myresourcegroup

az mariadb server replica create 命令需要以下参数:The az mariadb server replica create command requires the following parameters:

设置Setting 示例值Example value 说明Description
resource-groupresource-group myresourcegroupmyresourcegroup 要在其中创建副本服务器的资源组。The resource group where the replica server will be created to.
namename mydemoreplicaservermydemoreplicaserver 所创建的新副本服务器的名称。The name of the new replica server that is created.
source-serversource-server mydemoservermydemoserver 要从中进行复制的现有源服务器的名称或 ID。The name or ID of the existing source server to replicate from.

若要创建跨区域只读副本,请使用 --location 参数。To create a cross region read replica, use the --location parameter.

下面的 CLI 示例在中国北部 2 创建副本。The CLI example below creates the replica in China North 2.

az mariadb server replica create --name mydemoreplicaserver --source-server mydemoserver --resource-group myresourcegroup --location chinanorth2

备注

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

备注

只读副本使用与主服务器相同的服务器配置创建。Read replicas are created with the same server configuration as the master. 副本服务器配置在创建后可以更改。The replica server configuration can be changed after it has been created. 建议副本服务器的配置应始终采用与源服务器相同或更大的值,以确保副本能够与主服务器保持一致。It is recommended that the replica server's configuration should be kept at equal or greater values than the source to ensure the replica is able to keep up with the master.

列出源服务器的副本List replicas for a source server

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

az mariadb server replica list --server-name mydemoserver --resource-group myresourcegroup

az mariadb server replica list 命令需要以下参数:The az mariadb server replica list command requires the following parameters:

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

停止复制到副本服务器Stop replication to a replica server

重要

停止复制到服务器操作不可逆。Stopping replication to a server is irreversible. 一旦源服务器和副本服务器之间的复制停止,将无法撤消它。Once replication has stopped between a source and replica, it cannot be undone. 然后,副本服务器将成为独立服务器,并且现在支持读取和写入。The replica server then becomes a standalone server and now supports both read and writes. 此服务器不能再次成为副本服务器。This server cannot be made into a replica again.

可以使用以下命令停止复制到只读副本服务器:Replication to a read replica server can be stopped using the following command:

az mariadb server replica stop --name mydemoreplicaserver --resource-group myresourcegroup

az mariadb server replica stop 命令需要以下参数:The az mariadb server replica stop command requires the following parameters:

设置Setting 示例值Example value 说明Description
resource-groupresource-group myresourcegroupmyresourcegroup 副本服务器所在的资源组。The resource group where the replica server exists.
namename mydemoreplicaservermydemoreplicaserver 要停止在其上进行复制的副本服务器的名称。The name of the replica server to stop replication on.

删除副本服务器Delete a replica server

可以通过运行 az mariadb server delete 命令删除只读副本服务器。Deleting a read replica server can be done by running the az mariadb server delete command.

az mariadb server delete --resource-group myresourcegroup --name mydemoreplicaserver

删除源服务器Delete a source server

重要

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

若要删除源服务器,可以运行 az mariadb server delete 命令。To delete a source server, you can run the az mariadb server delete command.

az mariadb server delete --resource-group myresourcegroup --name mydemoserver

REST APIREST API

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

创建只读副本Create a read replica

可以使用创建 API 创建只读副本:You can create a read replica by using the create API:

PUT https://management.chinacloudapi.cn/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DBforMariaDB/servers/{replicaName}?api-version=2017-12-01
{
  "location": "chinanorth2",
  "properties": {
    "createMode": "Replica",
    "sourceServerId": "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DBforMariaDB/servers/{masterServerName}"
  }
}

备注

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

如果尚未在“常规用途”或“内存优化”源服务器上将 azure.replication_support 参数设置为“REPLICA”并重启服务器,将会收到错误。If you haven't set the azure.replication_support parameter to REPLICA on a General Purpose or Memory Optimized source server and restarted the server, you receive an error. 请在创建副本之前完成这两个步骤。Complete those two steps before you create a replica.

使用与主服务器相同的计算和存储设置创建副本。A replica is created by using the same compute and storage settings as the master. 创建副本后,可以独立于源服务器更改多项设置:计算代系、vCore 数、存储和备份保留期。After a replica is created, several settings can be changed independently from the source server: compute generation, vCores, storage, and back-up retention period. 定价层也可以独立更改,但“基本”层除外。The pricing tier can also be changed independently, except to or from the Basic tier.

重要

将源服务器设置更新为新值之前,请将副本设置更新为一个相等或更大的值。Before a source server setting is updated to a new value, update the replica setting to an equal or greater value. 此操作可帮助副本与主服务器发生的任何更改保持同步。This action helps the replica keep up with any changes made to the master.

列出副本List replicas

可以使用副本列表 API 查看源服务器的副本列表:You can view the list of replicas of a source server using the replica list API:

GET https://management.chinacloudapi.cn/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DBforMariaDB/servers/{masterServerName}/Replicas?api-version=2017-12-01

停止复制到副本服务器Stop replication to a replica server

可以使用更新 API 停止源服务器与只读副本之间的复制。You can stop replication between a source server and a read replica by using the update API.

停止复制到源服务器和只读副本后,无法撤消该操作。After you stop replication to a source server and a read replica, it can't be undone. 只读副本将成为支持读取和写入的独立服务器。The read replica becomes a standalone server that supports both reads and writes. 独立服务器不能再次成为副本。The standalone server can't be made into a replica again.

PATCH https://management.chinacloudapi.cn/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DBforMariaDB/servers/{masterServerName}?api-version=2017-12-01
{
  "properties": {
    "replicationRole":"None"  
   }
}

删除源服务器或副本服务器Delete a source or replica server

若要删除源服务器或副本服务器,请使用删除 APITo delete a source or replica server, you use the delete API:

删除源服务器后,将停止复制到所有只读副本的操作。When you delete a source server, replication to all read replicas is stopped. 只读副本将成为支持读取和写入的独立服务器。The read replicas become standalone servers that now support both reads and writes.

DELETE https://management.chinacloudapi.cn/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DBforMariaDB/servers/{serverName}?api-version=2017-12-01

后续步骤Next steps