Quickstart: Create an Azure Database for MySQL - Flexible Server using Azure CLI
This quickstart guide walks you through the steps to create an Azure Database for MySQL - Flexible Server instance using the Azure CLI. The flexible server deployment option offers more granular control over server configuration and greater flexibility.
Prerequisites
- An Azure account. If you don't have one, get a free account.
- Install the Azure CLI.
- Log in to your Azure account using
az login.
Steps to Create a Flexible Server
1. Set up variables
Before you begin, set the following variables to easily manage your resources. Replace the placeholder values with your own.
# Log in to your Azure account
az login
# Select your subscription
az account set --subscription ""
# Define variables
RESOURCE_GROUP="myResourceGroup"
LOCATION="eastus"
SERVER_NAME="mydemoserver-$(date +%s)" # Unique server name
ADMIN_USER="demouser"
ADMIN_PASSWORD="demopassword123!"
SKU_TIER="Basic" # Options: Basic, GeneralPurpose, MemoryOptimized
SKU_FAMILY="Gen5"
SKU_CAPACITY="2" # vCores
STORAGE_GB="32"
MYSQL_VERSION="8.0"
2. Create a resource group
A resource group is a logical container in Azure that holds related resources for an Azure solution. If you don't have a resource group, create one.
az group create --name $RESOURCE_GROUP --location $LOCATION
3. Create an Azure Database for MySQL - Flexible Server
Use the az mysql flexible-server create command to create your server. This command provisions a new server with the specified configuration.
az mysql flexible-server create \
--resource-group $RESOURCE_GROUP \
--name $SERVER_NAME \
--location $LOCATION \
--admin-user $ADMIN_USER \
--admin-password $ADMIN_PASSWORD \
--sku-name $SKU_TIER$SKU_FAMILY$SKU_CAPACITY \
--storage-size $STORAGE_GB \
--version $MYSQL_VERSION \
--public-access 0.0.0.0 \
--yes
Explanation of Parameters:
--resource-group: The name of the resource group.--name: The name of your MySQL server. It must be unique within Azure.--location: The Azure region where the server will be deployed.--admin-user: The administrator login name for the server.--admin-password: The password for the administrator.--sku-name: Specifies the compute tier, family, and vCores. E.g.,BasicGen52for Basic tier, Gen5 hardware, 2 vCores.--storage-size: The storage size in GiB.--version: The MySQL server version.--public-access: Set to0.0.0.0to allow public access (firewall rules will be added later to restrict access). Set to a specific IP address or range for more secure access.--yes: Skips the confirmation prompt.
4. Configure Firewall Rules
By default, public access is disabled. You need to configure firewall rules to allow access to your server from your client IP address or specific IP ranges.
# Get your public IP address (replace with your actual IP if known)
MY_IP=$(curl -s ifconfig.me)
az mysql flexible-server firewall-rule create \
--resource-group $RESOURCE_GROUP \
--server-name $SERVER_NAME \
--name AllowMyIP \
--start-ip-address $MY_IP \
--end-ip-address $MY_IP
To allow access from any IP address (use with caution, not recommended for production environments):
az mysql flexible-server firewall-rule create \
--resource-group $RESOURCE_GROUP \
--server-name $SERVER_NAME \
--name AllowAllIPs \
--start-ip-address 0.0.0.0 \
--end-ip-address 0.0.0.0
5. Connect to the Server
You can now connect to your Azure Database for MySQL server using a MySQL client tool like the MySQL Workbench or the mysql command-line client.
Replace <your-server-name>, <your-admin-username>, and <your-admin-password> with your actual values.
mysql -h <your-server-name>.mysql.database.azure.com -u <your-admin-username> -p
You will be prompted to enter your administrator password.
Next Steps
Now that you have successfully created and connected to your Azure Database for MySQL - Flexible Server, you can: