Skip to content

Commit 29459df

Browse files
authored
Merge pull request #96165 from ajlam/master
Add redirection for mysql
2 parents 8148d87 + f1d6f73 commit 29459df

File tree

3 files changed

+147
-3
lines changed

3 files changed

+147
-3
lines changed

articles/mysql/TOC.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@
168168
href: howto-configure-ssl.md
169169
- name: Web apps
170170
href: howto-connect-webapp.md
171+
- name: Connect with redirection
172+
href: howto-redirection.md
171173
- name: Configure server parameters
172174
items:
173175
- name: Azure portal

articles/mysql/concepts-connectivity-architecture.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ author: kummanish
55
ms.author: manishku
66
ms.service: mysql
77
ms.topic: conceptual
8-
ms.date: 05/22/2019
8+
ms.date: 11/15/2019
99
---
1010

1111
# Connectivity architecture in Azure Database for MySQL
@@ -62,8 +62,15 @@ The following table lists the primary and secondary IPs of the Azure Database fo
6262
> [!NOTE]
6363
> *East US 2* has also a tertiary IP address of `52.167.104.0`.
6464
65+
## Connection redirection
66+
67+
Azure Database for MySQL supports an additional connection policy, **redirection**, that helps to reduce network latency between client applications and MySQL servers. With this feature, after the initial TCP session is established to the Azure Database for MySQL server, the server returns the backend address of the node hosting the MySQL server to the client. Thereafter, all subsequent packets flow directly to the server, bypassing the gateway. As packets flow directly to the server, latency and throughput have improved performance.
68+
69+
This feature is supported in Azure Database for MySQL servers with engine versions 5.6, 5.7, and 8.0.
70+
71+
Preview support for redirection is available in the [PHP mysqlnd_azure](https://github.com/microsoft/mysqlnd_azure) extension, developed by Microsoft, and is available on [PECL](https://pecl.php.net/package/mysqlnd_azure). See the [configuring redirection](./howto-redirection.md) article for more information on how to use redirection in your applications.
72+
6573
## Next steps
6674

6775
* [Create and manage Azure Database for MySQL firewall rules using the Azure portal](./howto-manage-firewall-using-portal.md)
68-
* [Create and manage Azure Database for MySQL firewall rules using Azure CLI](./howto-manage-firewall-using-cli.md)
69-
76+
* [Create and manage Azure Database for MySQL firewall rules using Azure CLI](./howto-manage-firewall-using-cli.md)

articles/mysql/howto-redirection.md

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
---
2+
title: Connect to Azure Database for MySQL with redirection
3+
description: This article describes how you can configure you application to connect to Azure Database for MySQL with redirection.
4+
author: ajlam
5+
ms.author: andrela
6+
ms.service: mysql
7+
ms.topic: conceptual
8+
ms.date: 11/15/2019
9+
---
10+
11+
# Connect to Azure Database for MySQL with redirection
12+
13+
This topic explains how to connect an application your Azure Database for MySQL server with redirection mode. Redirection aims to reduce network latency between client applications and MySQL servers by allowing applications to connect directly to backend server nodes.
14+
15+
> [!IMPORTANT]
16+
> Support for redirection in the PHP [mysqlnd_azure](https://github.com/microsoft/mysqlnd_azure) is currently in preview.
17+
18+
## Before you begin
19+
Sign in to the [Azure portal](https://portal.azure.com). Create an Azure Database for MySQL server with engine version 5.6, 5.7, or 8.0. For details, refer to [How to create Azure Database for MySQL server from Portal](quickstart-create-mysql-server-database-using-azure-portal.md) or [How to create Azure Database for MySQL server using CLI](quickstart-create-mysql-server-database-using-azure-cli.md).
20+
21+
Redirection is currently only supported when SSL is enabled. For details on how to configure SSL, see [Using SSL with Azure Database for MySQL](https://docs.microsoft.com/azure/mysql/howto-configure-ssl#step-3-enforcing-ssl-connections-in-azure).
22+
23+
## PHP
24+
25+
### Ubuntu Linux
26+
27+
#### Prerequisites
28+
- PHP versions 7.2.15+ and 7.3.2+
29+
- PHP PEAR
30+
- php-mysql
31+
- Azure Database for MySQL server with SSL enabled
32+
33+
1. Install [mysqlnd_azure](https://github.com/microsoft/mysqlnd_azure) with [PECL](https://pecl.php.net/package/mysqlnd_azure).
34+
35+
```bash
36+
sudo pecl install mysqlnd_azure
37+
```
38+
39+
2. Locate the extension directory (`extension_dir`) by running the below:
40+
41+
```bash
42+
php -i | grep "extension_dir"
43+
```
44+
45+
3. Change directories to the returned folder and ensure `mysqlnd_azure.so` is located in this folder.
46+
47+
4. Locate the folder for .ini files by running the below:
48+
49+
```bash
50+
php -i | grep "dir for additional .ini files"
51+
```
52+
53+
5. Change directories to this returned folder.
54+
55+
6. Create a new .ini file for `mysqlnd_azure`. Make sure the alphabet order of the name is after that of mysqnld, since the modules are loaded according to the name order of the ini files. For example, if `mysqlnd` .ini is named `10-mysqlnd.ini`, name the mysqlnd ini as `20-mysqlnd-azure.ini`.
56+
57+
7. Within the new .ini file, add the following lines to enable redirection.
58+
59+
```bash
60+
extension=mysqlnd_azure
61+
mysqlnd_azure.enabled=on
62+
```
63+
64+
### Windows
65+
66+
#### Prerequisites
67+
- PHP versions 7.2.15+ and 7.3.2+
68+
- php-mysql
69+
- Azure Database for MySQL server with SSL enabled
70+
71+
1. Determine if you are running a x64 or x86 version of PHP by running the following command:
72+
73+
```cmd
74+
php -i | findstr "Thread"
75+
```
76+
77+
2. Download the corresponding x64 or x86 version of the [mysqlnd_azure](https://github.com/microsoft/mysqlnd_azure) DLL from [PECL](https://pecl.php.net/package/mysqlnd_azure) that matches your version of PHP.
78+
79+
3. Extract the zip file and find the DLL named `php_mysqlnd_azure.dll`.
80+
81+
4. Locate the extension directory (`extension_dir`) by running the below command:
82+
83+
```cmd
84+
php -i | find "extension_dir"s
85+
```
86+
87+
5. Copy the `php_mysqlnd_azure.dll` file into the directory returned in step 4.
88+
89+
6. Locate the PHP folder containing the `php.ini` file using the following command:
90+
91+
```cmd
92+
php -i | find "Loaded Configuration File"
93+
```
94+
95+
7. Modify the `php.ini` file and add the following extra lines to enable redirection.
96+
97+
Under the Dynamic Extensions section:
98+
```cmd
99+
extension=mysqlnd_azure
100+
```
101+
102+
Under the Module Settings section:
103+
```cmd
104+
[mysqlnd_azure]
105+
mysqlnd_azure.enabled=on
106+
```
107+
108+
### Confirm redirection
109+
110+
You can also confirm redirection is configured with the below sample PHP code. Create a PHP file called `mysqlConnect.php` and paste the below code. Update the server name, username, and password with your own.
111+
112+
```php
113+
<?php
114+
$host = '<yourservername>.mysql.database.azure.com';
115+
$username = '<yourusername>@<yourservername>';
116+
$password = '<yourpassword>';
117+
$db_name = 'testdb';
118+
echo "mysqlnd_azure.enabled: ", ini_get("mysqlnd_azure.enabled") == true?"On":"Off", "\n";
119+
$db = mysqli_init();
120+
$link = mysqli_real_connect ($db, $host, $username, $password, $db_name, 3306, NULL, MYSQLI_CLIENT_SSL);
121+
if (!$link) {
122+
die ('Connect error (' . mysqli_connect_errno() . '): ' . mysqli_connect_error() . "\n");
123+
}
124+
else {
125+
echo $db->host_info, "\n"; //if redirection succeeds, the host_info will differ from the hostname you used used to connect
126+
$res = $db->query('SHOW TABLES;'); //test query with the connection
127+
print_r ($res);
128+
$db->close();
129+
}
130+
?>
131+
```
132+
133+
## Next steps
134+
For more information about connection strings, refer to [Connection Strings](howto-connection-string.md).
135+

0 commit comments

Comments
 (0)