通过 Azure CLI、REST API 创建和管理只读副本Create and manage read replicas from the Azure CLI, REST API

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

Azure 复制支持Azure replication support

只读副本逻辑解码都依赖 Postgres 预写日志 (WAL) 来获取信息。Read replicas and logical decoding both depend on the Postgres write ahead log (WAL) for information. 这两个功能需要来自 Postgres 的不同级别的日志记录。These two features need different levels of logging from Postgres. 逻辑解码需要的日志记录的级别比只读副本需要的更高。Logical decoding needs a higher level of logging than read replicas.

若要配置正确的日志记录级别,请使用 Azure 复制支持参数。To configure the right level of logging, use the Azure replication support parameter. Azure 复制支持有三个设置选项:Azure replication support has three setting options:

  • 关闭 - 在 WAL 中包含最少的信息。Off - Puts the least information in the WAL. 大多数 Azure Database for PostgreSQL 服务器上都不提供此设置。This setting is not available on most Azure Database for PostgreSQL servers.
  • 副本 - 比“关闭”详细。Replica - More verbose than Off. 这是运行只读副本所需的最低日志记录级别。This is the minimum level of logging needed for read replicas to work. 此设置是大多数服务器上的默认设置。This setting is the default on most servers.
  • 逻辑 - 比“副本”详细。Logical - More verbose than Replica. 这是运行逻辑解码所需的最低日志记录级别。This is the minimum level of logging for logical decoding to work. 使用此设置时,只读副本也可以运行。Read replicas also work at this setting.

更改此参数后,需要重启服务器。The server needs to be restarted after a change of this parameter. 在内部,此参数设置 Postgres 参数 wal_levelmax_replication_slotsmax_wal_sendersInternally, this parameter sets the Postgres parameters wal_level, max_replication_slots, and max_wal_senders.

Azure CLIAzure CLI

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

先决条件Prerequisites

准备主服务器Prepare the master server

  1. 检查主服务器的 azure.replication_support 值。Check the master server's azure.replication_support value. 该值至少应该是“REPLICA”,只读副本才能正常工作。It should be at least REPLICA for read replicas to work.

    az postgres server configuration show --resource-group myresourcegroup --server-name mydemoserver --name azure.replication_support
    
  2. 如果 azure.replication_support 并非至少是“REPLICA”,请将其设置为“REPLICA”。If azure.replication_support is not at least REPLICA, set it.

    az postgres server configuration set --resource-group myresourcegroup --server-name mydemoserver --name azure.replication_support --value REPLICA
    
  3. 重启服务器以应用更改。Restart the server to apply the change.

    az postgres server restart --name mydemoserver --resource-group myresourcegroup
    

创建只读副本Create a read replica

az postgres server replica create 命令需要以下参数:The az postgres 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.
namename mydemoserver-replicamydemoserver-replica 所创建的新副本服务器的名称。The name of the new replica server that is created.
source-serversource-server mydemoservermydemoserver 要从中进行复制的现有主服务器的名称或资源 ID。The name or resource ID of the existing master server to replicate from. 如果希望副本和主服务器的资源组不同,请使用资源 ID。Use the resource ID if you want the replica and master's resource groups to be different.

在下面的 CLI 示例中,副本在主服务器所在的区域中创建。In the CLI example below, the replica is created in the same region as the master.

az postgres server replica create --name mydemoserver-replica --source-server mydemoserver --resource-group myresourcegroup

若要创建跨区域只读副本,请使用 --location 参数。To create a cross region read replica, use the --location parameter. 下面的 CLI 示例在中国北部创建副本。The CLI example below creates the replica in China North.

az postgres server replica create --name mydemoserver-replica --source-server mydemoserver --resource-group myresourcegroup --location chinanorth

备注

若要详细了解可以在哪些区域中创建副本,请访问只读副本概念文章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 master server and restarted the server, you receive an error. 请在创建副本之前完成这两个步骤。Complete those two steps before you create a replica.

重要

查看“只读副本”概述的注意事项部分Review the considerations section of the Read Replica overview.

将主服务器设置更新为新值之前,请将副本设置更新为一个相等或更大的值。Before a master 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

可以使用 az postgres server replica list 命令查看主服务器的副本的列表。You can view the list of replicas of a master server by using az postgres server replica list command.

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

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

可以使用 az postgres server replica stop 命令停止在主服务器和只读副本之间的复制。You can stop replication between a master server and a read replica by using az postgres server replica stop command.

停止复制到主服务器和只读副本后,无法撤消该操作。After you stop replication to a master 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.

az postgres server replica stop --name mydemoserver-replica --resource-group myresourcegroup 

删除主服务器或副本服务器Delete a master or replica server

若要删除主服务器或副本服务器,请使用 az postgres server delete 命令。To delete a master or replica server, you use the az postgres server delete command.

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

az postgres server delete --name myserver --resource-group myresourcegroup

REST APIREST API

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

准备主服务器Prepare the master server

  1. 检查主服务器的 azure.replication_support 值。Check the master server's azure.replication_support value. 该值至少应该是“REPLICA”,只读副本才能正常工作。It should be at least REPLICA for read replicas to work.

    GET https://management.chinacloudapi.cn/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DBforPostgreSQL/servers/{masterServerName}/configurations/azure.replication_support?api-version=2017-12-01
    
  2. 如果 azure.replication_support 并非至少是“REPLICA”,请将其设置为“REPLICA”。If azure.replication_support is not at least REPLICA, set it.

    PUT https://management.chinacloudapi.cn/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DBforPostgreSQL/servers/{masterServerName}/configurations/azure.replication_support?api-version=2017-12-01
    
    {
     "properties": {
     "value": "replica"
     }
    }
    
  3. 重启服务器以应用更改。Restart the server to apply the change.

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

创建只读副本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.DBforPostgreSQL/servers/{replicaName}?api-version=2017-12-01
{
  "location": "chinaeast",
  "properties": {
    "createMode": "Replica",
    "sourceServerId": "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DBforPostgreSQL/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 master 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 master 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 master 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 master server using the replica list API:

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

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

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

停止复制到主服务器和只读副本后,无法撤消该操作。After you stop replication to a master 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.DBforPostgreSQL/servers/{masterServerName}?api-version=2017-12-01
{
  "properties": {
    "replicationRole":"None"  
   }
}

删除主服务器或副本服务器Delete a master or replica server

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

删除主服务器后,将停止复制到所有只读副本。When you delete a master 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.DBforPostgreSQL/servers/{serverName}?api-version=2017-12-01

后续步骤Next steps