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
- 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.
- 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.
- 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.
- 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
- Navigate to the Azure Portal: Go to Azure Portal.
- Create a Resource: Click on "Create a resource" and search for "Azure Database for MySQL".
- 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.
- Configure Pricing Tier: Choose the appropriate pricing tier based on your performance and storage needs.
- Review and Create: Review your settings and click "Create".
Step 2: Configure Firewall Rules
- Navigate to Your MySQL Server: Once the server is created, go to the server's overview page.
- 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
- Install MySQL Client: Ensure you have a MySQL client installed on your local machine.
- Connect Using Command Line:
Replacemysql -h <server-name>.mysql.database.azure.com -u <admin-username>@<server-name> -p
<server-name>
and<admin-username>
with your server name and admin username.
Practical Example
Creating a Database and Table
- Create a Database:
CREATE DATABASE sampledb;
- Use the Database:
USE sampledb;
- Create a Table:
CREATE TABLE users ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100), email VARCHAR(100) );
- Insert Data:
INSERT INTO users (name, email) VALUES ('John Doe', '[email protected]');
- Query Data:
SELECT * FROM users;
Exercises
Exercise 1: Create and Configure a MySQL Database
- Create a new Azure Database for MySQL server.
- Configure firewall rules to allow your local IP address.
- Connect to the MySQL server using a MySQL client.
Exercise 2: Database Operations
- Create a new database named
testdb
. - Create a table named
products
with columnsid
,name
, andprice
. - Insert three records into the
products
table. - Query the
products
table to retrieve all records.
Solutions
Solution 1: Create and Configure a MySQL Database
- Follow the steps outlined in the "Setting Up Azure Database for MySQL" section.
- Add your local IP address to the firewall rules.
- Use the MySQL client to connect to the server.
Solution 2: Database Operations
- Create Database:
CREATE DATABASE testdb;
- Create Table:
CREATE TABLE products ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100), price DECIMAL(10, 2) );
- 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);
- 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.