Deploy a MySQL Database with Terraform

30 minutes
  • 2 Learning Objectives

About this Hands-on Lab

In this lab, we will deploy a MySQL database instance.

In the previous lab exercise, we deployed a Web application. It’s almost unheard of to find a Web application that doesn’t have a database backend. MySQL and MariaDB are among the most common types of databases for Web apps being robust and open source.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Log into the Azure Portal and set up the Command Line Interface (CLI) for use.
  1. Open the CLI.
  2. Select Bash at the prompt.
  3. Click Show Advanced Settings. Both the Resource Group and Storage Account should be pre-selected with the lab generated values.
  4. In the File share section, choose the Create new radio button and enter console.
  5. Click the Attach Storage button.
  6. Once the command prompt is initialized, proceed to Task 2.
Deploy a MySQL Database Instance

To complete this exercise, please make sure you’ve completed Task 1 before completing the following:

  1. Use the code block found in the Additional Information and Resources section to create a file and upload it to the CLI.
  2. In the (resource "azurerm_storage_account" "lab") declaration, you’ll need to edit the resource_group_name value with the name generated by the lab. Additionally, you’ll need to enter a unique name for the storage account that will be used for the file share. The "provider" statement has been added to the code, so you won’t need to create a file to deploy the storage account.
  3. Once the file has been uploaded, run terraform init.
  4. Run terraform plan and review the output to confirm that Terraform will create the desired resource. Green plus signs will indicate the resources that need to be added.
  5. Run terraform apply, answering yes to the prompt to continue.
  6. Once Terraform completes the deployment, check the Azure Portal to confirm.

You’re done! Go ahead and shut down the lab.

Additional Resources

Deploy a MySQL server instance with a Gen5, 2cpu, standard SKU. Note the settings for version, storage capacity, and database name. As in previous labs, be sure to copy in the lab generated resource group name.

Code for file follows

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "=3.0.0"

# Configure the Microsoft Azure Provider
provider "azurerm" {
  features {}
  skip_provider_registration = true

resource "azurerm_mysql_server" "example" {
  name                = "tflab-mysqlserver-1-[some unique characters]"
  location            = "The location of your lab provided resource group"
  resource_group_name = "Enter Your Lab's Resource Group Name"

  sku_name = "B_Gen5_2"
  # capacity = "2"
  # tier = "Basic"
  # family = "Gen4"

  storage_mb = "5120"
  version = "8.0"
  backup_retention_days = "7"
  ssl_enforcement_enabled = true
  infrastructure_encryption_enabled = false
  auto_grow_enabled = true
  public_network_access_enabled = true 

  administrator_login          = "mysqladminun"
  administrator_login_password = "easytologin4once!"

resource "azurerm_mysql_database" "example" {
  name                = "exampledb"
  resource_group_name = "Enter Your Lab's Resource Group Name"
  server_name         =
  charset             = "utf8"
  collation           = "utf8_unicode_ci"

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?