

Connect with redirection - Azure Database for MariaDB
source link: https://docs.microsoft.com/en-us/azure/mariadb/howto-redirection
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Connect to Azure Database for MariaDB with redirection
- 06/08/2020
- 4 minutes to read
In this article
This topic explains how to connect an application your Azure Database for MariaDB server with redirection mode. Redirection aims to reduce network latency between client applications and MariaDB servers by allowing applications to connect directly to backend server nodes.
Before you begin
Sign in to the Azure portal. Create an Azure Database for MariaDB server with engine version 10.2 or 10.3.
For details, refer to how to create an Azure Database for MariaDB server using the Azure portal or Azure CLI.
Enable redirection
On your Azure Database for MariaDB server, configure the redirect_enabled
parameter to ON
to allow connections with redirection mode. To update this server parameter, use the Azure portal or Azure CLI.
Support for redirection in PHP applications is available through the mysqlnd_azure extension, developed by Microsoft.
The mysqlnd_azure extension is available to add to PHP applications through PECL and it is highly recommended to install and configure the extension through the officially published PECL package.
Important
Support for redirection in the PHP mysqlnd_azure extension is currently in preview.
Redirection logic
Important
Redirection logic/behavior beginning version 1.1.0 was updated and it is recommended to use version 1.1.0+.
The redirection behavior is determined by the value of mysqlnd_azure.enableRedirect
. The table below outlines the behavior of redirection based on the value of this parameter beginning in version 1.1.0+.
If you are using an older version of the mysqlnd_azure extension (version 1.0.0-1.0.3), the redirection behavior is determined by the value of mysqlnd_azure.enabled
. The valid values are off
(acts similarly as the behavior outlined in the table below) and on
(acts like preferred
in the table below).
off
or 0
Redirection will not be used.
on
or 1
- If the connection does not use SSL on the driver side, no connection will be made. The following error will be returned: "mysqlnd_azure.enableRedirect is on, but SSL option is not set in connection string. Redirection is only possible with SSL."- If SSL is used on the driver side, but redirection is not supported on the server, the first connection is aborted and the following error is returned: "Connection aborted because redirection is not enabled on the MariaDB server or the network package doesn't meet redirection protocol."
- If the MariaDB server supports redirection, but the redirected connection failed for any reason, also abort the first proxy connection. Return the error of the redirected connection.
preferred
or 2
(default value) - mysqlnd_azure will use redirection if possible.
- If the connection does not use SSL on the driver side, the server does not support redirection, or the redirected connection fails to connect for any non-fatal reason while the proxy connection is still a valid one, it will fall back to the first proxy connection.
The subsequent sections of the document will outline how to install the mysqlnd_azure
extension using PECL and set the value of this parameter.
Ubuntu Linux
Prerequisites
- PHP versions 7.2.15+ and 7.3.2+
- PHP PEAR
- php-mysql
- Azure Database for MariaDB server
Install mysqlnd_azure with PECL. It is recommended to use version 1.1.0+.
sudo pecl install mysqlnd_azure
Locate the extension directory (
extension_dir
) by running the below:php -i | grep "extension_dir"
Change directories to the returned folder and ensure
mysqlnd_azure.so
is located in this folder.Locate the folder for .ini files by running the below:
php -i | grep "dir for additional .ini files"
Change directories to this returned folder.
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, ifmysqlnd
.ini is named10-mysqlnd.ini
, name the mysqlnd ini as20-mysqlnd-azure.ini
.Within the new .ini file, add the following lines to enable redirection.
extension=mysqlnd_azure mysqlnd_azure.enableRedirect = on/off/preferred
Windows
Prerequisites
- PHP versions 7.2.15+ and 7.3.2+
- php-mysql
- Azure Database for MariaDB server
Determine if you are running a x64 or x86 version of PHP by running the following command:
php -i | findstr "Thread"
Download the corresponding x64 or x86 version of the mysqlnd_azure DLL from PECL that matches your version of PHP. It is recommended to use version 1.1.0+.
Extract the zip file and find the DLL named
php_mysqlnd_azure.dll
.Locate the extension directory (
extension_dir
) by running the below command:php -i | find "extension_dir"
Copy the
php_mysqlnd_azure.dll
file into the directory returned in step 4.Locate the PHP folder containing the
php.ini
file using the following command:php -i | find "Loaded Configuration File"
Modify the
php.ini
file and add the following extra lines to enable redirection.Under the Dynamic Extensions section:
extension=mysqlnd_azure
Under the Module Settings section:
[mysqlnd_azure] mysqlnd_azure.enableRedirect = on/off/preferred
Confirm redirection
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.
<?php
$host = '<yourservername>.mariadb.database.azure.com';
$username = '<yourusername>@<yourservername>';
$password = '<yourpassword>';
$db_name = 'testdb';
echo "mysqlnd_azure.enableRedirect: ", ini_get("mysqlnd_azure.enableRedirect"), "\n";
$db = mysqli_init();
//The connection must be configured with SSL for redirection test
$link = mysqli_real_connect ($db, $host, $username, $password, $db_name, 3306, NULL, MYSQLI_CLIENT_SSL);
if (!$link) {
die ('Connect error (' . mysqli_connect_errno() . '): ' . mysqli_connect_error() . "\n");
}
else {
echo $db->host_info, "\n"; //if redirection succeeds, the host_info will differ from the hostname you used used to connect
$res = $db->query('SHOW TABLES;'); //test query with the connection
print_r ($res);
$db->close();
}
?>
Next steps
For more information about connection strings, see Connection Strings.
Is this page helpful?
Recommend
-
17
Efficient Java Matrix Library
-
10
java-version.com: What's new in Java 16? 15? Keep up to date! How to solve the Angular redirection error (404) using Java
-
17
MariaDB – a little hack – Serge Frezefond 's blogAzure Database for MySQL is a PaaS offer. It has a specific architecture that relies on a gateway. This has a huge advantage in the way it handle High availability. If a server fails it will a...
-
8
Automatic redirection on mobile breaks UXotsukare Thoughts after a day of workThis is another interesting case I have been dealing with today. When you try to access the Web site with...
-
8
Set up ProxySQL clustering to connect efficiently to Azure Database for MySQL%3CLINGO-SUB%20id%3D%22lingo-sub-1490357%22%20slang%3D%22en-US%22%3ESet%20up%20ProxySQL%20clustering%20to%20connect%20efficiently%20to%20Azure%20Database%20for%20MySQL%...
-
5
Java Azure Tools Update April 2021: Connect to Azure Database for MySQL Java Azure Tools Update April 2021: Connect to Azure Database for MySQL
-
6
How to connect Azure SQL database from Python Function App using managed identity or access token
-
12
ProxySQL connection redirection experiment with Azure database for MySQL/MariaDB. 3/3 – Serge Frezefond 's blog In the 2 previous posts(1/3,
-
8
How to build a ProxySQL compatible with Azure Database for MySQL In the previous postOpens in a new window I explained how
-
5
Azure Database for MariaDB documentation Azure Database for MariaDB is a relational database service based on the open-source MariaDB Server engine. It's a fully managed database as a service offering that can handle mission-cr...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK