|
| 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