Share via


Azure CLI를 사용하여 Azure Database for MySQL - 유연한 서버 인스턴스 만들기 및 퍼블릭 액세스 연결 사용

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

이 CLI 샘플 스크립트는 Azure Database for MySQL - 유연한 서버를 만들고, 서버 수준 방화벽 규칙(퍼블릭 액세스 연결 방법)을 구성하고, 만든 후에 서버에 연결합니다.

스크립트가 성공적으로 실행되면 모든 Azure 서비스 및 구성된 IP 주소에서 MySQL 유연한 서버에 액세스할 수 있으며 대화형 모드에서 서버에 연결됩니다.

참고 항목

서버를 만든 후에는 연결 방법을 변경할 수 없습니다. 예를 들어 퍼블릭 액세스(허용된 IP 주소)를 사용하여 서버를 만드는 경우 만든 후에 프라이빗 액세스(VNet 통합)로 변경할 수 없습니다. 연결 방법에 대한 자세한 내용은 네트워킹 개념을 참조하세요.

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 an Azure Database for MySQL - Flexible Server Burstable B1ms instance
# and configure Public Access connectivity method

# Variable block
let "randomIdentifier=$RANDOM*$RANDOM"
location="East US"
resourceGroup="msdocs-mysql-rg-$randomIdentifier"
tag="create-connect-burstable-server-public-access-mysql"
server="msdocs-mysql-server-$randomIdentifier"
login="azureuser"
password="Pa$$w0rD-$randomIdentifier"
ipAddress="None"
# 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" --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

# Connect to server in interactive mode
az mysql flexible-server connect --name $server --admin-user $login --admin-password $password --interactive

리소스 정리

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

az group delete --name $resourceGroup

샘플 참조

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

Command 참고
az group create 모든 리소스가 저장되는 리소스 그룹을 만듭니다.
az mysql flexible-server create 데이터베이스를 호스트하는 유연한 서버를 만듭니다.
az mysql flexible-server firewall-rule create 입력한 IP 주소 범위에서 유연한 서버 및 해당 데이터베이스에 대한 액세스를 허용하는 방화벽 규칙을 만듭니다.
az mysql flexible-server connect 유연한 서버에 연결하여 서버 또는 데이터베이스 작업을 수행합니다.
az mysql flexible-server delete 유연한 서버를 삭제합니다.
az group delete 모든 중첩 리소스를 포함한 리소스 그룹을 삭제합니다.

다음 단계