Skip to content

Commit dd931ee

Browse files
authored
Merge pull request #219680 from mksuni/redismysql-tutorial
New MySQL and Redis tutorial
2 parents b49b634 + 316d7f8 commit dd931ee

File tree

2 files changed

+174
-1
lines changed

2 files changed

+174
-1
lines changed

articles/mysql/TOC.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@
135135
href: flexible-server/concepts-customer-managed-key.md
136136
- name: Integrations
137137
items:
138+
- name: Azure Redis
139+
href: flexible-server/tutorial-add-redis-to-mysql.md
138140
- name: Power BI
139141
href: flexible-server/connect-with-powerbi-desktop.md
140142
- name: Azure advisor
@@ -806,4 +808,4 @@
806808
- name: Partners
807809
href: single-server/partners-migration-mysql.md
808810
- name: Database Migration Guide
809-
href: https://datamigration.microsoft.com/
811+
href: https://datamigration.microsoft.com/
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
---
2+
title: 'Quickstart: Boost performance for Azure Database for MySQL Flexible Server with Redis'
3+
description: "This tutorial shows how to add Redis cache to boost performance for your Azure Database for MySQL Flexible Server."
4+
ms.service: mysql
5+
ms.subservice: flexible-server
6+
ms.topic: quickstart
7+
author: mksuni
8+
ms.author: sumuth
9+
ms.date: 08/15/2022
10+
---
11+
12+
# Tutorial: Boost performance of Azure Database for MySQL - Flexible Server with Azure cache for Redis
13+
14+
[!INCLUDE[applies-to-mysql-flexible-server](../includes/applies-to-mysql-flexible-server.md)]
15+
16+
This article demonstrates how to boost the performance of an Azure Database for MySQL using [Azure cache for Redis](../../azure-cache-for-redis/cache-overview.md). Azure cache for Redis is a secure data cache and messaging broker that provides high throughput and low-latency access to data for applications.
17+
18+
## Prerequisites
19+
20+
For this quickstart you need:
21+
22+
- An Azure account with an active subscription.
23+
24+
[!INCLUDE [flexible-server-free-trial-note](../includes/flexible-server-free-trial-note.md)]
25+
- Create an Azure Database for MySQL Flexible server using [Azure portal](./quickstart-create-server-portal.md) <br/> or [Azure CLI](./quickstart-create-server-cli.md) if you don't have one.
26+
- Configure networking setting of Azure Database for MySQL Flexible server to make sure your IP has access to it. If you're using Azure App Service or Azure Kubernetes service, enable **Allow public access from any Azure service within Azure to this server** setting in the Azure portal.
27+
28+
[Having issues? Let us know](https://github.com/MicrosoftDocs/azure-docs/issues)
29+
30+
## Populate the MySQL database
31+
32+
Connect to [MySQL Server using MySQL Workbench](connect-workbench.md) and run the following query to populate the database.
33+
34+
```sql
35+
CREATE DATABASE tododb;
36+
37+
CREATE TABLE tasks
38+
(
39+
id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
40+
title nvarchar(100) NOT NULL,
41+
completed TINYINT(1) NOT NULL
42+
);
43+
44+
INSERT INTO tasks (id,title, completed) VALUES
45+
(1,'Task1', 0),
46+
(2,'Task2', 0),
47+
(3,'Task3', 1),
48+
(4,'Task4', 1),
49+
(5,'Task5', 0);
50+
51+
```
52+
53+
## Create a Redis cache
54+
[!INCLUDE [redis-cache-create](../../azure-cache-for-redis/includes/redis-cache-create.md)]
55+
56+
## Caching result of query using Python
57+
Install the latest version of [Python](https://www.python.org/) on your local environment or on Azure virtual machine or Azure App Service. Use pip to install redis-py.
58+
59+
```python
60+
pip install redis
61+
```
62+
The following code creates a connection to Redis using redis-py, stores the query result into the redis cache and fetch the value from the cache.
63+
64+
```python
65+
import redis
66+
import mysql.connector
67+
68+
r = redis.Redis(
69+
host='your-azure-redis-server-name.redis.cache.windows.net',
70+
port=6379,
71+
password='azure-redis-primary-access-key')
72+
73+
mysqlcnx = mysql.connector.connect(user='your-admin-username', password='db-user-password',
74+
host='database-servername.mysql.database.azure.com',
75+
database='your-databsae-name')
76+
77+
mycursor = mysqlcnx.cursor()
78+
mycursor.execute("SELECT * FROM tasks where completed=1")
79+
myresult = mycursor.fetchall()
80+
81+
#Set the result of query in a key
82+
if result:
83+
cache.hmset(mykey, myresult)
84+
cache.expire(mykey, 3600)
85+
return result
86+
87+
#Get value of mykey
88+
getkeyvalue= cache.hgetall(mykey)
89+
90+
#close mysql connection
91+
mysqlcnx.close()
92+
```
93+
94+
## Using Redis with PHP
95+
Install [PHP](https://www.php.net/manual/en/install.php) on your local environment. Follow the steps below to write a PHP script that caches a SQL query from MySQL database. Here are a few pre-requisites before running the script:
96+
1. Install and enable [Redis PECL extension](https://pecl.php.net/package/redis) to use Redis with your PHP script. See [how to install the extension locally](https://github.com/phpredis/phpredis/blob/develop/INSTALL.md)
97+
2. Install and enable [MySQL PDO extension](https://www.php.net/manual/en/ref.pdo-mysql.php)
98+
99+
```php
100+
<?php
101+
102+
$redis = new Redis();
103+
$redis->connect('azure-redis-servername.redis.cache.windows.net', 6379);
104+
$redis->auth('azure-redis-primary-access-key');
105+
106+
$key = 'tasks';
107+
108+
if (!$redis->get($key)) {
109+
/*Pulling data from MySQL database*/
110+
$database_name = 'database-name';
111+
$database_user = 'your-database-user';
112+
$database_password = 'your-database-password';
113+
$mysql_host = 'database-servername.mysql.database.azure.com';
114+
115+
$pdo = new PDO('mysql:host=' . $mysql_host . '; dbname=' . $database_name, $database_user, $database_password);
116+
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
117+
118+
$sql = "SELECT * FROM tasks";
119+
$stmt = $pdo->prepare($sql);
120+
$stmt->execute();
121+
122+
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
123+
$tasks[] = $row;
124+
}
125+
126+
$redis->set($key, serialize($tasks));
127+
$redis->expire($key, 10);
128+
129+
} else {
130+
/*Pulling data from Redis*/
131+
$tasks = unserialize($redis->get($key));
132+
133+
}
134+
135+
echo $source . ': <br>';
136+
print_r($tasks);
137+
```
138+
139+
## Using Redis with WordPress
140+
141+
The benefit of enabling Redis Cache to your WordPress application will allow you to deliver content faster since all of the WordPress content is stored in the database. You can cache content that is mostly read only from WordPress database to make the query lookups faster. You can use either of these plugins to setup Redis. Install and enable [Redis PECL extension](https://pecl.php.net/package/redis). See [how to install the extension locally](https://github.com/phpredis/phpredis/blob/develop/INSTALL.md) or [how to install the extension in Azure App Service](../../app-service/configure-language-php.md).
142+
143+
1. [Redis Object cache](https://wordpress.org/plugins/redis-cache/): Install and activate this plugin. Now update the wp-config.php file right above the statement */* That's all, stop editing! Happy blogging. */**
144+
145+
```php
146+
define( 'WP_REDIS_HOST', 'azure-redis-servername.redis.cache.windows.net' );
147+
define( 'WP_REDIS_PORT', 6379 );
148+
define( 'WP_REDIS_PASSWORD', 'azure-redis-primary-access-key' );
149+
define( 'WP_REDIS_TIMEOUT', 1 );
150+
define( 'WP_REDIS_READ_TIMEOUT', 1 );
151+
152+
// change the database for each site to avoid cache collisions
153+
// values 0-15 are valid in a default redis config.
154+
define( 'WP_REDIS_DATABASE', 0 );
155+
156+
// automatically delete cache keys after 7 days
157+
define( 'WP_REDIS_MAXTTL', 60 * 60 * 24 * 7 );
158+
159+
// bypass the object cache, useful for debugging
160+
// define( 'WP_REDIS_DISABLED', true );
161+
162+
/* That's all, stop editing! Happy blogging. */
163+
164+
```
165+
Go to wordpress admin dashboard and select the Redis settings page on the menu. Now select **enable Object Cache**. Plugin will read the redis server information from wp-config.php file.
166+
167+
You may also use [W3 Total cache](https://wordpress.org/plugins/w3-total-cache/) to configure Redis cache on your WordPress app. You can evaluate the performance improvements using [Query Monitor plugin](https://wordpress.org/plugins/query-monitor/) which allows you to debug database queries and it also shows total database queries grouped by a plugin.
168+
169+
## Next steps
170+
171+
In this quickstart, you learned how to create an instance of Azure Cache for Redis and use it with Azure database for MySQL. See [performance best practices](../single-server/concept-performance-best-practices.md) for Azure database for MySQL.

0 commit comments

Comments
 (0)