Skip to content

Private Network Access for Azure Database for MySQL - Flexible Server

Notifications You must be signed in to change notification settings

miztiik/azure-mysql-private-access

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Private Network Access for Azure Database for MySQL - Flexible Server

A simple demonstration to securely and privately access the Azure DB for MySQL.

🎯 Solution

Miztiik Automation - Private Network Access for Azure Database for MySQL - Flexible Server

  1. 🧰 Prerequisites

    This demo, along with its instructions, scripts, and Bicep template, has been specifically designed to be executed in the northeurope region. However, with minimal modifications, you can also try running it in other regions of your choice (the specific steps for doing so are not covered in this context)

  2. ⚙️ Setting up the environment

    • Get the application code

      git clone https://github.com/miztiik/azure-web-server-to-mysql.git
      cd azure-web-server-to-mysql
  3. 🚀 Prepare the local environment

    Ensure you have jq, Azure Cli and bicep working

    jq --version
    func --version
    bicep --version
    bash --version
    az account show
  4. 🚀 Deploying the Solution

    • Stack: Main Bicep The params required for the modules are in params.json. Modify them as needed. The helper deployment script deploy.sh will deploy the main.bicep file. This will create the following resoureces

      • Resource Group(RG)
      • VNet, Subnet & Virtual Machine
        • One Subnet dedicated for the Flexible MySQL Server
      • Virtual Machine(Ubuntu) - 1 - Defined in params.json named vmCount
        • Bootstrapped with custom libs using userData script - Installs Nginx
        • Install Azure Monitoring Agent
      • User Managed Identity - Identity attached to the VM with the following permissions,
        • Monitoring Metrics Publisher
      • Azure Database for MySQL Server - Flexible
        • Admin User
        • Admin Password
        • SQL Version - 8.0
        • Network Subnet - The dedicated subnet created earlier.
        • Apparently Northeurope doesn't support high availability for Flexible servers as of Q2-2023

      Note - I hacked the template from another repo of mine, so you will see some unused resources(log analytics workspace/queues etc.,). You can safely ignore them, But do remember to clean up your Resource Group to avoid unnecessary costs.

    # make deploy
    sh deployment_scripts/deploy.sh

    After successfully deploying the stack, Check the Resource Groups/Deployments section for the resources.

  5. 🔬 Testing the solution

    • Login to your VM, You can find the public IP address in the resource blade. You can also get it from the Azure Portal. You may also try connecting to the database from local machine if you have mysql client, In this case, lets use the Azure VM as it is already bootstrapped with the mysql client

      ssh miztiik@<PUBLIC_IP_ADDRESS>
    • You can find the Primary Database Server name from the resource blade.

      Miztiik Automation - Private Network Access for Azure Database for MySQL - Flexible Server Miztiik Automation - Private Network Access for Azure Database for MySQL - Flexible Server Miztiik Automation - Private Network Access for Azure Database for MySQL - Flexible Server Miztiik Automation - Private Network Access for Azure Database for MySQL - Flexible Server

      #!/bin/bash
      
      HOST="store-backend-mysql-replication-db-003.mysql.database.azure.com"
      PORT=3306
      USERNAME='miztiik'
      PASSWORD=''
      
      # Database and Table Details
      DATABASE="miztiik_store_backend_db"
      TABLE="store_events"
      
      # Maximum record count for the for loop
      RECORD_COUNT=10
      
      # Connect to MySQL and create database
      mysql -h "$HOST" -P "$PORT" -u "$USERNAME" -p"$PASSWORD" <<EOF
      CREATE DATABASE $DATABASE;
      EOF
      
      # Switch to the newly created database
      mysql -h "$HOST" -P "$PORT" -u "$USERNAME" -p"$PASSWORD" -e "USE $DATABASE;"
      
      # Create table
      mysql -h "$HOST" -P "$PORT" -u "$USERNAME" -p"$PASSWORD" -e "CREATE TABLE IF NOT EXISTS $DATABASE.$TABLE (id INT PRIMARY KEY, ts TIMESTAMP, msg VARCHAR(255));"
      
      # Insert records using for loop
      for ((i=1; i<=RECORD_COUNT; i++))
      do
      RANDOM_INCREMENT=$((RANDOM % 10 + 1))
      MESSAGE="Hello World from Miztiiik $RANDOM_INCREMENT"
      mysql -h "$HOST" -P "$PORT" -u "$USERNAME" -p"$PASSWORD" -e "INSERT INTO $DATABASE.$TABLE (id, ts, msg) VALUES ($i, CURRENT_TIMESTAMP, '$MESSAGE');"
      done
      
      # Verify inserted records
      mysql -h "$HOST" -P "$PORT" -u "$USERNAME" -p"$PASSWORD" -e "SELECT * FROM $DATABASE.$TABLE;"
    • You should see the records in the database & the output should look like this,

      +----+---------------------+------------------------------+
      | id | ts                  | msg                          |
      +----+---------------------+------------------------------+
      |  1 | 2023-05-28 20:45:57 | Hello World from Miztiiik 2  |
      |  2 | 2023-05-28 20:45:57 | Hello World from Miztiiik 2  |
      |  3 | 2023-05-28 20:45:57 | Hello World from Miztiiik 10 |
      |  4 | 2023-05-28 20:45:57 | Hello World from Miztiiik 9  |
      |  5 | 2023-05-28 20:45:57 | Hello World from Miztiiik 10 |
      |  6 | 2023-05-28 20:45:57 | Hello World from Miztiiik 8  |
      |  7 | 2023-05-28 20:45:57 | Hello World from Miztiiik 7  |
      |  8 | 2023-05-28 20:45:57 | Hello World from Miztiiik 9  |
      |  9 | 2023-05-28 20:45:57 | Hello World from Miztiiik 3  |
      | 10 | 2023-05-28 20:45:57 | Hello World from Miztiiik 8  |
      +----+---------------------+------------------------------+
  6. 📒 Conclusion

    In this demonstration, we have shown how to securely connect to Azure Database for MySQL - Flexible Server from a VM in the same VNet.

  7. 🧹 CleanUp

    If you want to destroy all the resources created by the stack, Execute the below command to delete the stack, or you can delete the stack from console as well

    # Delete from resource group
    az group delete --name Miztiik_Enterprises_xxx --yes
    # Follow any on-screen prompt

    This is not an exhaustive list, please carry out other necessary steps as maybe applicable to your needs.

📌 Who is using this

This repository aims to show how to Bicep to new developers, Solution Architects & Ops Engineers in Azure.

💡 Help/Suggestions or 🐛 Bugs

Thank you for your interest in contributing to our project. Whether it is a bug report, new feature, correction, or additional documentation or solutions, we greatly value feedback and contributions from our community. Start here

👋 Buy me a coffee

ko-fi Buy me a coffee ☕.

📚 References

  1. Azure Docs: Just In Time Access

🏷️ Metadata

miztiik-success-green

Level: 100

About

Private Network Access for Azure Database for MySQL - Flexible Server

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

Packages

No packages published