Mandalika's scratchpad [ Work blog @Oracle | My Music Compositions ]

Old Posts: 09.04  10.04  11.04  12.04  01.05  02.05  03.05  04.05  05.05  06.05  07.05  08.05  09.05  10.05  11.05  12.05  01.06  02.06  03.06  04.06  05.06  06.06  07.06  08.06  09.06  10.06  11.06  12.06  01.07  02.07  03.07  04.07  05.07  06.07  08.07  09.07  10.07  11.07  12.07  01.08  02.08  03.08  04.08  05.08  06.08  07.08  08.08  09.08  10.08  11.08  12.08  01.09  02.09  03.09  04.09  05.09  06.09  07.09  08.09  09.09  10.09  11.09  12.09  01.10  02.10  03.10  04.10  05.10  06.10  07.10  08.10  09.10  10.10  11.10  12.10  01.11  02.11  03.11  04.11  05.11  07.11  08.11  09.11  10.11  11.11  12.11  01.12  02.12  03.12  04.12  05.12  06.12  07.12  08.12  09.12  10.12  11.12  12.12  01.13  02.13  03.13  04.13  05.13  06.13  07.13  08.13  09.13  10.13  11.13  12.13  01.14  02.14  03.14  04.14  05.14  06.14  07.14  09.14  10.14  11.14  12.14  01.15  02.15  03.15  04.15  06.15  09.15  12.15  01.16  03.16  04.16  05.16  06.16  07.16  08.16  09.16  12.16  01.17  02.17  03.17  04.17  06.17  07.17  08.17  09.17  10.17  12.17  01.18  02.18  03.18  04.18  05.18  06.18  07.18  08.18  09.18  11.18  12.18  01.19  02.19  05.19  06.19  08.19  10.19  11.19  05.20  10.20  11.20  12.20  09.21  11.21  12.22 


Monday, January 19, 2009
 
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:

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

(Originally posted on blogs.sun.com at:
http://blogs.sun.com/mandalika/entry/mysql_native_driver_for_php
)
_______________
Technorati Tags:
| |


Comments: Post a Comment



<< Home


2004-2019 

This page is powered by Blogger. Isn't yours?