mysqlnd_azure is a PHP extension implemented using the mysqlnd plugin API, which provides redirection feature support for the Azure Database for MySQL and MariaDB services.
- Extension name:
mysqlnd_azure
- Branch: mysqlnd-azure-php723-1.0.0 or tag v1.0.0.
- Required minimum PHP version: PHP7.2.15+ and PHP7.3.2+
-
Install
phpize
for build using one of the following commands based on your Linux distro- Ubuntu: Run
apt-get install php7.3-dev
- In the above command, update the version to correspond with your PHP version. For example, if you use PHP7.3, use
php7.3-dev
.
- In the above command, update the version to correspond with your PHP version. For example, if you use PHP7.3, use
- RedHat: Run
yum install php72-php-devel
- In the above command, update the version to correspond with your PHP version. For example, if you use PHP7,2. use
php72-php-devel
.
- In the above command, update the version to correspond with your PHP version. For example, if you use PHP7,2. use
- Ubuntu: Run
-
After installing, if you cannot find
phpize
orphp-config
, you need to link them in order to continue. Use the below commands. Update the version of PHP based on your language version.- Run
ln -s /opt/remi/php72/root/bin/phpize /usr/bin/phpize
- Run
ln -s /opt/remi/php72/root/bin/php-config /usr/bin/php-config
- Run
-
Choose your folder to hold the source code, e.g. php-mysqlnd_extension, and change directories into it
-
Run
git clone https://github.com/microsoft/mysqlnd_azure
- Run
git checkout to branch mysqlnd-azure-php723-1.0.0
- Change directories to the
mysqlnd_azure
source code folder - Run
phpize
- Run
./configure
- Run
make
After running these commands, you will find a file named
**mysqlnd_azure.so**
under the./modules
folder. - Run
- Run
make install
to put the**mysqlnd_azure.so**
in your PHP so library.
This may not add the configuration file for you automatically. Alternatively, use the following steps:
- Run
php -i
. You will find two folders extension_dir and Scan this dir for additional .ini files, you can find it usingphp -i | grep "extension_dir"
orgrep "dir for additional .ini files"
. - Put the
mysqlnd_azure.so
in theextension_dir
folder. - Under the directory for
additional .ini files
, you will find the ini files for the commonly used modules (ex.10-mysqlnd.ini
for mysqlnd,20-mysqli.ini
for mysqli). - Create a new ini file for
mysqlnd_azure
here. Make sure the alphabetical order of the name is after that of mysqnld, since the modules are loaded according to the name order of the ini files. (ex. if themysqlnd
ini is with named10-mysqlnd.ini
,then name the ini as20-mysqlnd-azure.ini
. - In the ini file, add the following two lines:
extension=mysqlnd_azure
mysqlnd_azure.enabled = on
- Setting this to OFF disables redirection.
To build on Windows, the php-sdk-binary-tools are required. Additional documentation for building is available in the PHP documentation.
-
Install Visual Studio
- If compiling PHP 7.2+, install Visual Studio 2017 and open either the “VS2017 x64 Native Tools Command Prompt” or the “VS2017 x86 Native Tools Command Prompt”.
- If compiling PHP 7.4+: install Visual Studio 2019
-
Fetch the latest stable SDK tag from https://github.com/Microsoft/php-sdk-binary-tools
- The new PHP SDK is required, when building PHP 7.2+
- Read the PHP SDK specific notes on the GitHub repository page
-
Run the phpsdk_buildtree batch script which will create the desired directory structure:
- Run
phpsdk_buildtree phpdev
-
The phpsdk_buildtree script will create the path according to the currently Visual C++ version used and switch into the newly created directory
C:\php-sdk\phpdev\vX##\x##
, where:vX#
# is the compiler version you are using (ex. vc14 or vs16)x##
is your architecture (x86 or x64)- Example:
C:\php-sdk\phpdev\vc15\x64\
for PHP7.2+
-
Under this folder, clone the PHP source code
- Run
git clone https://github.com/php/php-src
- Fetch the branch based on the related language version (ex. PHP-7.2.20)
- Run
-
Use the PHP SDK tools to fetch the suitable dependencies automatically by calling
phpsdk_deps -u
-
Extract mysqlnd_azure source code to the ext folder
php-source-code-folder\ext\
or create a "pecl" folder parralel to php-src.- Example:
C:\php-sdk\phpdev\vc15\x64\php-src\ext\mysqlnd_azure
orC:\php-sdk\phpdev\vc15\x64\pecl\mysqlnd_azure
- Example:
-
Invoke the starter script. For example if using Visual Studio 2017 64-bit to build, invoke
C:\php-sdk\phpsdk-vc15-x64.bat
-
Run
buildconf
-
Run
configure.bat --disable-all --enable-cli --with-mysqlnd --enable-mysqlnd_azure=shared
-
Run
nmake
-
This creates a DLL under
.\x64\x64\Release_TS\
with the name php_mysqlnd_azure.dll. This is the target library.
-
Use
php -i
to find the ini to find the extension_dir, copy the php_mysqlnd_azure.dll there -
Find the ini file location, modify the ini file with the following extra lines:
extension=mysqlnd_azure
mysqlnd_azure.enabled = on
- Setting this to OFF disables redirection.
Currently, redirection is only possible with SSL-enabled connections and the redirection feature switch is enabled on the Azure Database for MySQL server.
The following is a code sample to test connection with redirection:
echo "mysqlnd_azure.enabled: ", ini_get("mysqlnd_azure.enabled") == true?"On":"Off", "\n";
$db = mysqli_init();
$link = mysqli_real_connect ($db, 'your-hostname-with-redirection-enabled', 'user@host', 'password', "db", 3306, NULL, MYSQLI_CLIENT_SSL);
if (!$link) {
die ('Connect error (' . mysqli_connect_errno() . '): ' . mysqli_connect_error() . "\n");
}
else {
echo $db->host_info, "\n"; //if redrection succeeds, you will find the host_info differ from your-hostname used to connect
$res = $db->query('SHOW TABLES;'); //test query with the connection
print_r ($res);
$db->close();
}