你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

使用 Azure CLI 在 VNet 中创建 Azure Database for MySQL 灵活服务器数据库

适用于:Azure Database for MySQL - 灵活服务器

此示例 CLI 脚本在 VNet(专用访问连接方法)中创建 Azure Database for MySQL 灵活服务器,并从该 VNet 中的 VM 连接到该服务器。

注意

创建服务器后,无法更改连接方法。 例如,如果使用“专用访问(VNet 集成)”创建服务器,则在创建后无法更改为“公共访问(允许的 IP 地址)” 。 若要详细了解连接方法,请参阅网络概念

如果没有 Azure 订阅,请在开始之前创建一个 Azure 免费帐户。 目前,通过 Azure 免费帐户,可以在 12 个月内免费试用 Azure Database for MySQL - 灵活服务器。 有关详细信息,请参阅免费试用 Azure Database for MySQL - 灵活服务器

先决条件

示例脚本

启动 Azure Cloud Shell

Azure Cloud Shell 是免费的交互式 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 in a VNet

# Variable block
let "randomIdentifier=$RANDOM*$RANDOM"
location="East US"
resourceGroup="msdocs-mysql-rg-$randomIdentifier"
tag="create-connect-server-in-vnet-mysql"
server="msdocs-mysql-server-$randomIdentifier"
sku="Standard_D2ds_v4"
tier="GeneralPurpose"
storageSize="64"
storageAutoGrow="Enabled"
vNet="vNet-$randomIdentifier"
vNetAddressPrefix="155.5.0.0/24"
mySqlSubnet="msdocs-subnet-mysql-$randomIdentifier"
mySqlSubnetAddressPrefix="155.5.0.0/28"
rule="msdocs-rule-$randomIdentifier"
login="azureuser"
password="Pa$$w0rD-$randomIdentifier"
image="Ubuntu2204"
vm="msdocs-vm-$randomIdentifier"
vmSubnet="msdocs-subnet-vm-$randomIdentifier"
vmSubnetAddressPrefix="155.5.0.48/28"
dns="msdocsDNS.private.mysql.database.azure.com"
ipSku="basic"

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

# Create MySQL server in a VNET 

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

# Get available service endpoints for Azure region output in JSON
echo "List of available service endpoints for $location"
az network vnet list-endpoint-services --location "$location"

# Create the virtual network
echo "Creating $vNet"
az network vnet create --resource-group $resourceGroup --name $vNet --address-prefixes $vNetAddressPrefix --location "$location"

# Creates the mySqlSubnet
echo "Creating $mySqlSubnet in $vNet"
az network vnet subnet create --resource-group $resourceGroup --name $mySqlSubnet --vnet-name $vNet --address-prefix $mySqlSubnetAddressPrefix --service-endpoints Microsoft.SQL

# View service endpoints configured on a subnet
echo "Viewing the service endpoint to $mySqlSubnet in $vNet"
az network vnet subnet show --resource-group $resourceGroup --name $mySqlSubnet --vnet-name $vNet

# Create private DNS zone
echo "Creating $dns"
az network private-dns zone create -g $resourceGroup    -n $dns

# OPTIONAL : View all SKUs for Flexible Server
# az mysql flexible-server list-skus --location "$location"

# Name of a server maps to DNS name and is thus required to be globally unique in Azure.
# Create a MySQL Flexible server in the resource group
echo "Creating $server within $mySqlSubnet"
az mysql flexible-server create --name $server --resource-group $resourceGroup --location "$location" --sku-name $sku --tier $tier --storage-size $storageSize --storage-auto-grow $storageAutoGrow --admin-user $login --admin-password $password --vnet $vNet --subnet $mySqlSubnet --private-dns-zone $dns

# Connect to the MySQL server from a VM in the same VNET 

# Create a subnet for the virtual machine within the virtual network
echo "Creating $vmSubnet within $vNet"
az network vnet subnet create --resource-group $resourceGroup --vnet-name $vNet --name $vmSubnet --address-prefixes $vmSubnetAddressPrefix

# Create a VM within the VNET to connect to MySQL Flex Server
echo "Creating $vm within $vmSubnet"
az vm create --resource-group $resourceGroup --name $vm --location "$location" --image $image --admin-username $login --generate-ssh-keys --vnet-name $vNet --subnet $vmSubnet --public-ip-sku $ipSku

# Open port 80 for web traffic
echo "Opening port 80 for web traffic"
az vm open-port --port 80 --resource-group $resourceGroup --name $vm

# Follow steps in the parent article to test connectivity to the MySQL server from the VM

测试从 VM 到 MySQL 服务器的连接

通过使用 SSH 进行连接、下载 MySQL 工具,然后使用它们来连接到 MySQL 服务器,使用以下步骤来测试从 VM 到 MySQL 服务器的连接。

  1. 若要通过 SSH 连接到 VM,请首先获取公共 IP 地址,然后使用 MySQL 工具进行连接

    PUBLIC_IP=$(az vm list-ip-addresses --resource-group $RESOURCE_GROUP --name $VM --query "[].virtualMachine.network.publicIpAddresses[0].ipAddress" --output tsv)
    
    ssh azureuser@$PUBLIC_IP
    
  2. 下载 MySQL 工具并连接到服务器。 将 <server_name> 和 <admin_user> 替换为你的值。

    sudo apt-get update
    sudo apt-get install mysql-client
    
    wget --no-check-certificate https://dl.cacerts.digicert.com/DigiCertGlobalRootCA.crt.pem
    
    mysql -h <replace_with_server_name>.mysql.database.azure.com -u mysqladmin -p --ssl-mode=REQUIRED --ssl-ca=DigiCertGlobalRootCA.crt.pem
    

清理资源

使用 az group delete 命令删除资源组以及与其关联的所有资源 - 除非你持续需要这些资源。 其中一些资源在创建和删除时可能要稍等片刻。

az group delete --name $RESOURCE_GROUP

示例参考

此脚本使用以下命令。 表中的每条命令均链接到特定于命令的文档。

命令 说明
az group create 创建用于存储所有资源的资源组
az mysql flexible-server create 创建托管数据库的灵活服务器。
az network vnet subnet create 在 VNet 中创建子网。
az vm create 创建 Azure 虚拟机。
az vm open-port 将某个 VM 向指定端口上的入站流量开放。
az group delete 删除资源组,包括所有嵌套的资源。

后续步骤