Share via


Azure CLI를 사용하여 Azure Database for MySQL - 유연한 서버에서 읽기 복제본 만들기 및 관리

적용 대상: Azure Database for MySQL - 유연한 서버

이 샘플 CLI 스크립트는 Azure Database for MySQL - 유연한 서버에서 읽기 복제본을 만들고 관리합니다.

Important

기존 복제본이 없는 원본에 대한 복제본을 만드는 경우 원본이 먼저 다시 시작하여 자체적으로 복제할 준비를 합니다. 이를 고려하고 사용량이 적은 기간 동안 이러한 작업을 수행합니다.

Azure를 구독하고 있지 않다면 시작하기 전에 Azure 체험 계정을 만듭니다. 현재 Azure 체험 계정을 사용하면 Azure Database for MySQL - 유연한 서버를 12개월 동안 무료로 사용해 볼 수 있습니다. 자세한 내용은 Azure Database for MySQL - 유연한 서버 체험을 참조하세요.

필수 조건

샘플 스크립트

Azure Cloud Shell 시작

Azure Cloud Shell은 이 문서의 단계를 실행하는 데 무료로 사용할 수 있는 대화형 셸입니다. 공용 Azure 도구가 사전 설치되어 계정에서 사용하도록 구성되어 있습니다.

Cloud Shell을 열려면 코드 블록의 오른쪽 위 모서리에 있는 사용해 보세요를 선택하기만 하면 됩니다. 또한 https://shell.azure.com 로 이동하여 별도의 브라우저 탭에서 Cloud Shell을 시작할 수 있습니다.

Cloud Shell이 열리면 환경에 대해 Bash가 선택되어 있는지 확인합니다. 후속 세션은 Bash 환경에서 Azure CLI를 사용합니다. 복사를 선택하여 코드 블록을 복사하고 Cloud Shell에 붙여넣고 Enter 키를 눌러 실행합니다.

Azure에 로그인

Cloud Shell은 로그인한 초기 계정에서 자동으로 인증됩니다. 다음 스크립트를 통해 다른 구독을 사용하여 로그인하고 <Subscription ID>를 Azure 구독 ID로 바꿉니다. Azure를 구독하고 있지 않다면 시작하기 전에 Azure 체험 계정을 만듭니다.

subscription="<subscriptionId>" # add subscription here

az account set -s $subscription # ...or use 'az login'

자세한 내용은 활성 구독 설정 또는 대화형으로 로그인을 참조하세요.

스크립트 실행

# Create and manage MySQL - Flexible Server read replicas

# Variable block
let "randomIdentifier=$RANDOM*$RANDOM"
subscriptionId="$(az account show --query id -o tsv)"
location="East US"
resourceGroup="msdocs-mysql-rg-$randomIdentifier"
tag="monitor-and-scale-mysql"
server="msdocs-mysql-server-$randomIdentifier"
login="azureuser"
password="Pa$$w0rD-$randomIdentifier"
ipAddress="None"
sku="Standard_D2ds_v4"
tier="GeneralPurpose"
storageSize="64"
replica="msdocs-replica-mysql-$randomIdentifier" # Substitute with preferred name for the replica server. 

# Specifying an IP address of 0.0.0.0 allows public access from any resources
# deployed within Azure to access your server. Setting it to "None" sets the server 
# in public access mode but does not create a firewall rule.
# For your public IP address, https://whatismyipaddress.com

echo "Using resource group $resourceGroup with login: $login, password: $password..."

# Create a resource group
echo "Creating $resourceGroup in $location..."
az group create --name $resourceGroup --location "$location" --tags $tag

# Create a MySQL Flexible server in the resource group
echo "Creating $server"
az mysql flexible-server create --name $server --resource-group $resourceGroup --location "$location" --sku-name $sku --tier $tier --storage-size $storageSize --admin-user $login --admin-password $password --public-access $ipAddress

# Optional: Add firewall rule to connect from all Azure services
# To limit to a specific IP address or address range, change start-ip-address and end-ip-address
echo "Adding firewall for IP address range"
az mysql flexible-server firewall-rule create --name $server --resource-group $resourceGroup --rule-name AllowAzureIPs --start-ip-address 0.0.0.0 --end-ip-address 0.0.0.0

# Create Replica Server
echo "Creating $replica"
az mysql flexible-server replica create --replica-name $replica --source-server $server --resource-group $resourceGroup

# List all read replicas for the source server
echo "List replicas on $server"
az mysql flexible-server replica list --source-server $server --resource-group $resourceGroup

# Stop replication to a read replica and make it a read/write server.
echo "Stop replication to $replica"
az mysql flexible-server replica stop-replication --resource-group $resourceGroup --name $replica --yes

리소스 정리

다음 명령을 사용하여 이러한 리소스가 계속해서 필요한 경우가 아니면 az group delete 명령을 사용하여 리소스 그룹 및 연결된 모든 리소스를 제거합니다. 이러한 리소스 중 일부는 만들고 삭제하는 데 시간이 걸릴 수 있습니다.

az group delete --name $resourceGroup

샘플 참조

이 스크립트는 다음 명령을 사용합니다. 테이블에 있는 각 명령은 명령에 해당하는 문서에 연결됩니다.

Command 참고
az group create 모든 리소스가 저장되는 리소스 그룹을 만듭니다.
az mysql flexible-server create 데이터베이스를 호스트하는 유연한 서버를 만듭니다.
az mysql flexible-server replica create 서버용 읽기 복제본을 만듭니다.
az mysql flexible-server replica list 지정된 서버의 모든 읽기 복제본을 나열합니다.
az mysql flexible-server replica stop-replication 읽기 복제본에 대한 복제를 중지하고 읽기/쓰기 서버로 만듭니다.
az mysql flexible-server delete 유연한 서버를 삭제합니다.
az group delete 모든 중첩 리소스를 포함한 리소스 그룹을 삭제합니다.

다음 단계