Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Azure Database for MySQL Tutorial

Introduction

Azure Database for MySQL is a fully managed database service designed for app developers. It manages and maintains database services like backups, high availability, scalability, and security.

Creating an Azure Database for MySQL Instance

To create an Azure Database for MySQL instance, follow these steps:

  1. Navigate to the Azure Portal.
  2. In the left-hand menu, select Create a resource.
  3. Search for "Azure Database for MySQL" and select it.
  4. Click Create to start the setup wizard.
Example:

After clicking Create, you will be prompted to fill in the following details:

  • Subscription: Select your Azure subscription.
  • Resource Group: Create a new resource group or select an existing one.
  • Server Name: Enter a unique server name.
  • Location: Choose the data center location.
  • Version: Select the MySQL version.
  • Compute + Storage: Configure the number of vCores and storage size.
  • Admin Username and Password: Set the admin username and password.

Connecting to the MySQL Server

After creating the MySQL instance, you can connect to it using various tools. Here, we will use the MySQL command-line tool.

Example:

Open your terminal and run the following command:

mysql -h your-server-name.mysql.database.azure.com -u your-username@your-server-name -p

You will be prompted to enter the password you set during the creation of the MySQL instance.

Creating a Database

Once connected, you can create a new database. Use the following SQL command:

CREATE DATABASE mydatabase;
Query OK, 1 row affected (0.01 sec)

Creating Tables

To create a table, first select the database you created and then use the CREATE TABLE command:

USE mydatabase;
CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100),
    email VARCHAR(100)
);
Query OK, 0 rows affected (0.02 sec)

Inserting Data

To insert data into the table, use the INSERT INTO command:

INSERT INTO users (name, email) VALUES ('John Doe', 'john.doe@example.com');
Query OK, 1 row affected (0.01 sec)

Querying Data

To query data from the table, use the SELECT statement:

SELECT * FROM users;
+----+----------+----------------------+ | id | name | email | +----+----------+----------------------+ | 1 | John Doe | john.doe@example.com | +----+----------+----------------------+

Scaling the Database

Azure Database for MySQL allows you to scale your database by increasing the number of vCores and storage. This can be done through the Azure Portal by navigating to your MySQL server and selecting Compute + Storage to adjust the settings.

Backup and Restore

Azure Database for MySQL automatically performs backups of your database. You can restore your database from these backups via the Azure Portal:

  1. Navigate to your MySQL server.
  2. Select Backups in the left-hand menu.
  3. Choose the backup you want to restore from and follow the prompts.

Security Features

Azure Database for MySQL offers various security features including:

  • Data Encryption: Data is encrypted at rest and in transit.
  • Firewall Rules: You can configure IP firewall rules to restrict access to your database.
  • SSL Connections: Ensure that your applications use SSL connections to the MySQL server.

Monitoring and Alerts

Azure provides built-in monitoring and alerting capabilities. You can set up monitoring and alerts via the Azure Portal:

  1. Navigate to your MySQL server.
  2. Select Monitoring in the left-hand menu.
  3. Set up metrics and alerts to monitor the performance and health of your MySQL instance.