MySQL Native Driver for PHP, mysqlnd

In order to communicate with the MySQL database server from a PHP application, ext/mysql, ext/mysqli and the PDO MYSQL driver rely on the MySQL client library, libmysql - that has the required implementation for the client-server protocol. The MySQL native driver for PHP (will simply be referred as mysqlnd from this point), is an additional, alternative way to connect from PHP 5 and PHP 6 to the MySQL Server 4.1 or later versions. mysqlnd is a replacement for the MySQL client library, libmysql; and it is tightly integrated into PHP starting with the release of PHP 5.3. That is, from PHP 5.3 onwards the developers can choose between libmysql and mysqlnd when using mysql, mysqli or PDO_MySQL extensions to connect to the MySQL server 4.1 or newer. Due to the tight integration into PHP 5.3 (and later), mysqlnd eliminates the dependency on the MySQL client programming support when the database extension(s) and the database driver are built with the support for mysqlnd.

mysqlnd is not another PHP extension like mysqli nor it has an exposed API to the userland. It is a library that provides almost similar functionality as that of the MySQL client library, libmysql. mysqlnd and libmysql libraries implement the MySQL communication protocol - hence both of those libraries can be used to connect to the MySQL Server.

Since mysqlnd is neither a new extension nor a programming API, but just an alternative to libmysql to connect from PHP to the MySQL Server, there is no need to make changes to the existing PHP scripts. The existing scripts which were running properly with the mysql, mysqli and PDO_MySQL extensions built with libmysql support, continue to run with the exact same behavior even when the mysql, mysqli and PDO_MySQL extensions are built with the mysqlnd support.

From the performance perspective, mysqlnd might be as fast as libmysql; and may even outperform libmysql in some cases. The generic recommendation is to try mysqlnd with your PHP application and to decide based on the performance results.

Some of the advantages of using mysqlnd are listed below:

  • Easy to compile: no linking against libmysql, and no dependency on the MySQL client programming support.
  • may outperform libmysql in certain cases
  • persistent connections for ext/mysqli
  • uses PHP memory management, supports PHP memory limit
  • reduced memory footprint -- keeps every row only once in memory, where as with libmysql you have it twice in memory
  • keeps a long list of performance related statistics for bottle-neck analysis
  • client-side result set cache (still in experimental stage)

Installing PHP with the MySQL native driver, mysqlnd

As most of the pre-packaged PHP binary installations may not have the support for mysqlnd enabled by default, it is recommended to build PHP and the required database extensions with mysqlnd from the source code. The rest of this section focuses on the installation of PHP 5.3 from the source code. Check the php-mysqlnd web page at dev.mysql.com for the installation instructions for the prior versions of PHP with mysqlnd support.

  1. Get the source code for PHP 5.3 or later versions from http://www.php.net or from http://snaps.php.net/.

  2. Make sure that Autoconf 2.13 or later and GNU M4 1.4.7 or later are installed on the machine. Adjust the PATH environment variable to include the path to the autoconf and m4 tools.

    eg.,
    
    bash# ls /usr/local/bin/autoconf ; ls /usr/local/bin/m4
    /usr/local/bin/autoconf
    /usr/local/bin/m4
    
    bash# export PATH=.:/usr/local/bin:$PATH
    
    
  3. On Sun Solaris, create a soft link to gmake in the source directory.

    
    bash# ln -s /usr/bin/gmake make
    
    
  4. Navigate to the source directory and run buildconf.

    eg.,
    
    bash# cd php5.3-200811132130
    bash# ./buildconf --force
    
    
  5. For building PHP 5.3 or later with mysqlnd support on UNIX/Linux systems, you can decide for all three MySQL extensions (ext/mysql, ext/mysqli, PDO_mysql) whether they should be built using mysqlnd or libmysql. When choosing mysqlnd, use "mysqlnd" as path to the mysql client library. If you don't specify "mysqlnd" as library location, by default, PHP tries to use libmysql. It is possible to build one extension with one library, and another extension with another library. For example, you can build mysqli extension with mysqlnd support, and PDO_MYSQL with libmysql.

    The configure option shown below builds all the three extensions with mysqlnd support.

    eg.,
    
    bash# ./configure --prefix=/export/home/php53 --enable-mysqlnd \\
           --with-mysqli=shared,mysqlnd --with-mysql=shared,mysqlnd \\
    	--with-pdo-mysql=shared,mysqlnd --with-zlib=shared [other options]
    
    

    configure script in PHP 6.0 and some builds of PHP 5.3 may not recognize the --enable-mysqlnd option, so check the configure options by running ./configure --help before specifying --enable-mysqlnd in the list of configure options when building PHP.

    On Windows platform, mysqli extension uses the MySQL Native Driver by default in PHP versions 5.3 and newer. Hence you don't need to configure libmysql.dll

  6. On Sun Solaris, pass the -z muldefs option to the linker to pro-actively defend against linker errors like ld: fatal: symbol `<symbol>' is multiply-defined.

    
    bash# export LDFLAGS="-z muldefs"
    
    
  7. Build PHP.

    
    bash# make
    
    
  8. Install PHP in the destination location.

    
    bash# make install
    
    
  9. Enable the required database extensions in php.ini

    eg.,
    
    bash# grep extension php.ini | grep -v \\;
    extension_dir=/export/home/php53/lib/php/extensions/no-debug-non-zts-20071006
    extension="mysql.so"
    extension="mysqli.so"
    extension="pdo_mysql.so"
    
    
  10. Finally verify the new PHP installation by checking the list of PHP modules.

    eg.,
    
    bash# cd /export/home/php53/bin
    
    bash# ./php -m | grep mysql
    mysql
    mysqli
    mysqlnd
    pdo_mysql
    
    bash# ./php -i | grep -i mysql
    ..
    mysql
    MySQL Support => enabled
    Client API version => mysqlnd 5.0.5-dev - 081106 - $Revision: 1.3.2.18 $
    ..
    mysqli
    MysqlI Support => enabled
    Client API library version => mysqlnd 5.0.1-beta - 070402 - $Revision: 321 $
    ..
    mysqlnd
    mysqlnd => enabled
    Version => mysqlnd 5.0.5-dev - 081106 - $Revision: 1.3.2.18 $
    ..
    pdo_mysql
    PDO Driver for MySQL => enabled
    Client API version => mysqlnd 5.0.5-dev - 081106 - $Revision: 1.3.2.18 $
    ..
    
    

Acknowledgments

Andrey Hristov & Ulf Wendel, Sun-MySQL

Comments:

Hi Giri,

well done summary: Thanks!

Though, I have a few minor aside notes and corrections for you.

"That is, from PHP 5.x onwards the developers can choose between libmysql and mysqlnd when... " , make it PHP 5.3. We have releases early versions of mysqlnd for 5.2.x but mysqlnd is a 5.3 feature and php.net is for sure not unhappy if people migrate fast to the lastest version. Its not possible to use todays mysqlnd code base with 5.2. The internal Zend Engine and PHP APIs have been modified between 5.2.x and 5.3.

"Some of the advantages of using mysqlnd are listed below: [...] client-side result set cache ", nope, sorry no client-side result set cache. We developed a prototype, we did a proof-of-concept but that is about it. Of course you can use buffered and unbuffered result sets like ever since, but that is different from a client-side result set cache.

PS: Congratulations on mysqlnd should go to Andrey Hristov. He has done 95% of the coding. He is the brain. I am - at best - his ghost writer.

Posted by Ulf Wendel on January 19, 2009 at 05:11 PM PST #

mysql and php 5 my favori Andrey Hristov thank you

Posted by toplist ekle on March 25, 2011 at 10:20 PM PDT #

Hi. Iwould like to install mysql extensions (mysql, mysqli, pdo_mysql) with mysqlnd driver without self compiled php. I tried to compile php as described in your post and replace mysql.so, mysqli.so, pdo_mysql.so. As expected the ubuntu php is missing the mysqlnd headers. Therefor I would like to have a mysqlnd.so to enable mysqlnd support in the package maintaners php version. Is this possible? I found a mysqlnd.so in an redhead rpm. Can you help me?

Best Jan

Posted by guest on September 28, 2011 at 07:13 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Benchmark announcements, HOW-TOs, Tips and Troubleshooting

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today