Create an Azure Database for MySQL - Flexible Server in a VNet using Azure CLI

This sample CLI script creates an Azure Database for MySQL - Flexible Server in a VNet (private access connectivity method) and connects to the server from a VM within the VNet.

Note

The connectivity method cannot be changed after creating the server. For example, if you create server using Private access (VNet Integration), you cannot change to Public access (allowed IP addresses) after creation. To learn more about connectivity methods, see Networking concepts.

If you don't have an Azure subscription, create an Azure free account before you begin. With an Azure free account, you can now try Azure Database for MySQL - Flexible Server for free for 12 months. For more information, see Try Flexible Server for free.

Prerequisites

Sample script

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'

For more information, see set active subscription or log in interactively

Run the script

# 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="UbuntuLTS"
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

Test connectivity to the MySQL server from the VM

Use the following steps to test connectivity to the MySQL server from the VM by connecting using SSH, downloading MySQL tools, and then using them to connect to the MySQL server.

  1. To SSH into the VM, start by getting the public IP address and then use MySQL tools to connect

    publicIp=$(az vm list-ip-addresses --resource-group $resourceGroup --name $vm --query "[].virtualMachine.network.publicIpAddresses[0].ipAddress" --output tsv)
    
    ssh azureuser@$publicIp
    
  2. Download MySQL tools and connect to the server. Substitute <server_name> and <admin_user> with your values.

    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
    

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

Sample reference

This script uses the following commands. Each command in the table links to command specific documentation.

Command Notes
az group create Creates a resource group in which all resources are stored
az mysql flexible-server create Creates a Flexible Server that hosts the databases.
az network vnet subnet create Creates a subnet within the VNet.
az vm create Creates an Azure Virtual Machine.
az vm open-port Opens a VM to inbound traffic on specified ports.
az group delete Deletes a resource group including all nested resources.

Next steps