0
0
Azurecloud~5 mins

Azure Database for MySQL - Commands & Configuration

Choose your learning style9 modes available
Introduction
Azure Database for MySQL is a managed service that lets you run MySQL databases in the cloud without handling the hardware or software setup. It solves the problem of managing database servers by taking care of backups, updates, and scaling automatically.
When you want to host a MySQL database for your web application without managing the server.
When you need automatic backups and easy scaling for your database.
When you want to connect your app to a secure, managed MySQL database in Azure.
When you want to avoid the hassle of installing and maintaining MySQL software.
When you need high availability and security features for your MySQL database.
Config File - azuredeploy.json
azuredeploy.json
{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "serverName": {
      "type": "string",
      "defaultValue": "my-mysql-server",
      "metadata": {
        "description": "Name of the Azure Database for MySQL server"
      }
    },
    "administratorLogin": {
      "type": "string",
      "defaultValue": "mysqladmin",
      "metadata": {
        "description": "Admin username for MySQL server"
      }
    },
    "administratorLoginPassword": {
      "type": "securestring",
      "metadata": {
        "description": "Admin password for MySQL server"
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "eastus",
      "metadata": {
        "description": "Azure region for the server"
      }
    }
  },
  "resources": [
    {
      "type": "Microsoft.DBforMySQL/servers",
      "apiVersion": "2021-05-01",
      "name": "[parameters('serverName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "B_Gen5_1",
        "tier": "Basic",
        "capacity": 1,
        "family": "Gen5"
      },
      "properties": {
        "version": "5.7",
        "sslEnforcement": "Enabled",
        "administratorLogin": "[parameters('administratorLogin')]",
        "administratorLoginPassword": "[parameters('administratorLoginPassword')]",
        "storageProfile": {
          "storageMB": 5120,
          "backupRetentionDays": 7,
          "geoRedundantBackup": "Disabled"
        }
      }
    }
  ]
}

This ARM template creates an Azure Database for MySQL server with these settings:

  • serverName: The unique name of your MySQL server.
  • administratorLogin and administratorLoginPassword: Credentials to manage the server.
  • location: Azure region where the server runs.
  • sku: Defines the pricing tier and compute size (Basic, Gen5, 1 vCore).
  • version: MySQL version 5.7.
  • sslEnforcement: Requires secure connections.
  • storageProfile: Storage size, backup retention, and backup redundancy settings.

This file can be deployed to Azure to create a ready-to-use MySQL server.

Commands
Log in to your Azure account to allow commands to manage resources.
Terminal
az login
Expected OutputExpected
To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code ABCD1234 to authenticate. You have logged in. Now let us find all the subscriptions to which you have access... [{ "cloudName": "AzureCloud", "homeTenantId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", "id": "yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy", "isDefault": true, "managedByTenants": [], "name": "My Azure Subscription", "state": "Enabled", "tenantId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", "user": { "name": "user@example.com", "type": "user" } }]
Create a resource group named 'myResourceGroup' in the East US region to hold your MySQL server.
Terminal
az group create --name myResourceGroup --location eastus
Expected OutputExpected
{ "id": "/subscriptions/yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy/resourceGroups/myResourceGroup", "location": "eastus", "managedBy": null, "name": "myResourceGroup", "properties": { "provisioningState": "Succeeded" }, "tags": {}, "type": "Microsoft.Resources/resourceGroups" }
--name - Specifies the resource group name
--location - Specifies the Azure region
Deploy the ARM template to create the Azure Database for MySQL server with the specified admin password.
Terminal
az deployment group create --resource-group myResourceGroup --template-file azuredeploy.json --parameters administratorLoginPassword=MyS3cureP@ssw0rd
Expected OutputExpected
{ "id": "/subscriptions/yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy/resourceGroups/myResourceGroup/providers/Microsoft.Resources/deployments/myDeployment", "name": "myDeployment", "properties": { "provisioningState": "Succeeded", "outputs": {} }, "type": "Microsoft.Resources/deployments" }
--resource-group - Specifies the resource group to deploy into
--template-file - Specifies the ARM template file
--parameters - Passes parameters to the template
Check the details of the created MySQL server to confirm it is deployed and running.
Terminal
az mysql server show --resource-group myResourceGroup --name my-mysql-server
Expected OutputExpected
{ "administratorLogin": "mysqladmin", "fullyQualifiedDomainName": "my-mysql-server.mysql.database.azure.com", "id": "/subscriptions/yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy/resourceGroups/myResourceGroup/providers/Microsoft.DBforMySQL/servers/my-mysql-server", "location": "eastus", "name": "my-mysql-server", "resourceGroup": "myResourceGroup", "sku": { "capacity": 1, "family": "Gen5", "name": "B_Gen5_1", "tier": "Basic" }, "state": "Ready", "version": "5.7" }
--resource-group - Specifies the resource group of the server
--name - Specifies the MySQL server name
Key Concept

If you remember nothing else from this pattern, remember: Azure Database for MySQL lets you run a secure, managed MySQL server in the cloud without handling hardware or software setup.

Common Mistakes
Using a weak or simple password for the administratorLoginPassword parameter.
Azure requires strong passwords for security; weak passwords cause deployment failures.
Use a complex password with uppercase, lowercase, numbers, and symbols when deploying.
Not creating a resource group before deploying the ARM template.
Deployment fails because the target resource group does not exist.
Always create the resource group first using 'az group create' before deployment.
Trying to connect to the MySQL server before it is fully provisioned and in 'Ready' state.
Connection attempts fail because the server is not yet available.
Check server status with 'az mysql server show' and wait until state is 'Ready'.
Summary
Log in to Azure and create a resource group to organize your resources.
Deploy an ARM template to create a managed MySQL server with secure settings.
Verify the server is ready before connecting your applications.