Create and manage Azure Database for MySQL VNet service endpoints using Azure CLI
APPLIES TO: Azure Database for MySQL - Single Server
Azure Database for MySQL - Single Server is on the retirement path. We strongly recommend for you to upgrade to Azure Database for MySQL - Flexible Server. For more information about migrating to Azure Database for MySQL - Flexible Server, see What's happening to Azure Database for MySQL Single Server?
Virtual Network (VNet) services endpoints and rules extend the private address space of a Virtual Network to your Azure Database for MySQL server. Using convenient Azure CLI commands, you can create, update, delete, list, and show VNet service endpoints and rules to manage your server. For an overview of Azure Database for MySQL VNet service endpoints, including limitations, see Azure Database for MySQL Server VNet service endpoints. VNet service endpoints are available in all supported regions for Azure Database for MySQL.
If you prefer to run CLI reference commands locally, install the Azure CLI. If you're running on Windows or macOS, consider running Azure CLI in a Docker container. For more information, see How to run the Azure CLI in a Docker container.
If you're using a local installation, sign in to the Azure CLI by using the az login command. To finish the authentication process, follow the steps displayed in your terminal. For other sign-in options, see Sign in with the Azure CLI.
When you're prompted, install the Azure CLI extension on first use. For more information about extensions, see Use extensions with the Azure CLI.
Support for VNet service endpoints is only for General Purpose and Memory Optimized servers. In case of VNet peering, if traffic is flowing through a common VNet Gateway with service endpoints and is supposed to flow to the peer, please create an ACL/VNet rule to allow Azure Virtual Machines in the Gateway VNet to access the Azure Database for MySQL server.
Configure Vnet service endpoints for Azure Database for MySQL
The az network vnet commands are used to configure Virtual Networks.
If you have multiple subscriptions, choose the appropriate subscription in which the resource should be billed. The account must have the necessary permissions to create a virtual network and service endpoint. Service endpoints can be configured on virtual networks independently, by a user with write access to the virtual network.
To secure Azure service resources to a VNet, the user must have permission to "Microsoft.Network/virtualNetworks/subnets/joinViaServiceEndpoint/" for the subnets being added. This permission is included in the built-in service administrator roles, by default and can be modified by creating custom roles.
VNets and Azure service resources can be in the same or different subscriptions. If the VNet and Azure service resources are in different subscriptions, the resources should be under the same Active Directory (AD) tenant. Ensure that both the subscriptions have the Microsoft.Sql and Microsoft.DBforMySQL resource providers registered. For more information refer resource-manager-registration
It is highly recommended to read this article about service endpoint configurations and considerations before running the sample script below, or configuring service endpoints. Virtual Network service endpoint: A Virtual Network service endpoint is a subnet whose property values include one or more formal Azure service type names. VNet services endpoints use the service type name Microsoft.Sql, which refers to the Azure service named SQL Database. This service tag also applies to the Azure SQL Database, Azure Database for PostgreSQL and MySQL services. It is important to note when applying the Microsoft.Sql service tag to a VNet service endpoint it configures service endpoint traffic for all Azure Database services, including Azure SQL Database, Azure Database for PostgreSQL and Azure Database for MySQL servers on the subnet.
Launch Azure Cloud Shell
The Azure Cloud Shell is a free interactive shell that you can use to run the steps in this article. It has common Azure tools preinstalled and configured to use with your account.
To open the Cloud Shell, just select Try it from the upper right corner of a code block. You can also launch Cloud Shell in a separate browser tab by going to https://shell.azure.com.
When Cloud Shell opens, verify that Bash is selected for your environment. Subsequent sessions will use Azure CLI in a Bash environment, Select Copy to copy the blocks of code, paste it into the Cloud Shell, and press Enter to run it.
Sign in to Azure
Cloud Shell is automatically authenticated under the initial account signed-in with. Use the following script to sign in using a different subscription, replacing
<Subscription ID> with your Azure Subscription ID. If you don't have an Azure subscription, create an Azure free account before you begin.
subscription="<subscriptionId>" # add subscription here az account set -s $subscription # ...or use 'az login'
Run the script
# Create and manage Azure Database for MySQL VNet service endpoints # Variable block let "randomIdentifier=$RANDOM*$RANDOM" location="East US" resourceGroup="msdocs-mysql-rg-$randomIdentifier" tag="create-mysql-server" server="msdocs-mysql-server-$randomIdentifier" sku="GP_Gen5_2" vNet="vNet-$randomIdentifier" vNetAddressPrefix="10.0.0.0/16" subnet="subnet-$randomIdentifier" subnetAddressPrefix="10.0.1.0/24" rule="rule-$randomIdentifier" login="azureuser" password="Pa$$w0rD-$randomIdentifier" 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 server in the resource group # Name of a server maps to DNS name and is thus required to be globally unique in Azure. echo "Creating $server in $location..." az mysql server create --name $server --resource-group $resourceGroup --location "$location" --admin-user $login --admin-password $password --sku-name $sku # 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 subnet echo "Creating $subnet in $vNet" az network vnet subnet create --resource-group $resourceGroup --name $subnet --vnet-name $vNet --address-prefix $subnetAddressPrefix --service-endpoints Microsoft.SQL # View service endpoints configured on a subnet echo "Viewing the service endpoint to $subnet in $vNet" az network vnet subnet show --resource-group $resourceGroup --name $subnet --vnet-name $vNet # Create a VNet rule on the server to secure it to the subnet # Note: resource group (-g) parameter is where the database exists. # VNet resource group if different should be specified using subnet id (URI) instead of subnet, VNet pair. echo "Creating a VNet rule on $server to secure it to $subnet in $vNet" az mysql server vnet-rule create --name $rule --resource-group $resourceGroup --server $server --vnet-name $vNet --subnet $subnet
Clean up resources
Use the following command to remove the resource group and all resources associated with it using the az group delete command - unless you have an ongoing need for these resources. Some of these resources may take a while to create, as well as to delete.
az group delete --name $resourceGroup