Introduction

Azure Database for MySQL is a fully managed database service designed to handle mission-critical workloads with predictable performance and dynamic scalability. It provides built-in high availability, security, and compliance, making it an ideal choice for developers and businesses looking to leverage MySQL in the cloud.

Key Concepts

  1. Fully Managed Service

  • Automatic Patching: Azure handles all the patching and maintenance of the database, ensuring that it is always up-to-date with the latest features and security updates.
  • Backups: Automated backups are performed daily, and you can configure the retention period according to your needs.

  1. High Availability

  • Built-in High Availability: Azure Database for MySQL offers built-in high availability with no additional configuration required.
  • Zone Redundant HA: For even higher availability, you can enable zone redundant high availability, which replicates your data across multiple availability zones.

  1. Security

  • Data Encryption: Data is encrypted at rest and in transit using industry-standard encryption protocols.
  • Firewall Rules: You can configure firewall rules to control access to your database, ensuring that only trusted IP addresses can connect.

  1. Scalability

  • Vertical Scaling: You can scale up or down the compute resources (vCores) and storage capacity without any downtime.
  • Horizontal Scaling: For read-heavy workloads, you can add read replicas to distribute the read traffic.

Setting Up Azure Database for MySQL

Step 1: Create a MySQL Database

  1. Navigate to the Azure Portal: Go to Azure Portal.
  2. Create a Resource: Click on "Create a resource" and search for "Azure Database for MySQL".
  3. Configure Basic Settings:
    • Subscription: Select your Azure subscription.
    • Resource Group: Create a new resource group or select an existing one.
    • Server Name: Enter a unique name for your MySQL server.
    • Region: Choose the region closest to your users.
    • Administrator Username and Password: Set the admin username and password.
  4. Configure Pricing Tier: Choose the appropriate pricing tier based on your performance and storage needs.
  5. Review and Create: Review your settings and click "Create".

Step 2: Configure Firewall Rules

  1. Navigate to Your MySQL Server: Once the server is created, go to the server's overview page.
  2. Set Firewall Rules: Under "Settings", click on "Connection security". Add the IP addresses that are allowed to connect to the server.

Step 3: Connect to the MySQL Database

  1. Install MySQL Client: Ensure you have a MySQL client installed on your local machine.
  2. Connect Using Command Line:
    mysql -h <server-name>.mysql.database.azure.com -u <admin-username>@<server-name> -p
    
    Replace <server-name> and <admin-username> with your server name and admin username.

Practical Example

Creating a Database and Table

  1. Create a Database:
    CREATE DATABASE sampledb;
    
  2. Use the Database:
    USE sampledb;
    
  3. Create a Table:
    CREATE TABLE users (
        id INT AUTO_INCREMENT PRIMARY KEY,
        name VARCHAR(100),
        email VARCHAR(100)
    );
    
  4. Insert Data:
    INSERT INTO users (name, email) VALUES ('John Doe', '[email protected]');
    
  5. Query Data:
    SELECT * FROM users;
    

Exercises

Exercise 1: Create and Configure a MySQL Database

  1. Create a new Azure Database for MySQL server.
  2. Configure firewall rules to allow your local IP address.
  3. Connect to the MySQL server using a MySQL client.

Exercise 2: Database Operations

  1. Create a new database named testdb.
  2. Create a table named products with columns id, name, and price.
  3. Insert three records into the products table.
  4. Query the products table to retrieve all records.

Solutions

Solution 1: Create and Configure a MySQL Database

  1. Follow the steps outlined in the "Setting Up Azure Database for MySQL" section.
  2. Add your local IP address to the firewall rules.
  3. Use the MySQL client to connect to the server.

Solution 2: Database Operations

  1. Create Database:
    CREATE DATABASE testdb;
    
  2. Create Table:
    CREATE TABLE products (
        id INT AUTO_INCREMENT PRIMARY KEY,
        name VARCHAR(100),
        price DECIMAL(10, 2)
    );
    
  3. Insert Records:
    INSERT INTO products (name, price) VALUES ('Product1', 10.99);
    INSERT INTO products (name, price) VALUES ('Product2', 20.99);
    INSERT INTO products (name, price) VALUES ('Product3', 30.99);
    
  4. Query Records:
    SELECT * FROM products;
    

Conclusion

In this module, you learned about Azure Database for MySQL, its key features, and how to set it up and manage it. You also practiced creating databases and tables, inserting data, and querying data. This knowledge will help you leverage Azure Database for MySQL for your applications, ensuring high availability, security, and scalability.

© Copyright 2024. All rights reserved