|
|
Using the MySQL Native Driver for PHP, mysqlnd
This series of tutorials shows the essential steps involved in installing PHP with MySQL extensions mysql, mysqli, and pdo_mysql, and with support for the MySQL native driver for PHP, mysqlnd. The tutorials also provide simple examples to connect, insert, and retrieve data from a MySQL database. Because the focus is on database connectivity from a PHP script, these tutorials assume that some kind of MySQL database is already up and accessible from the client machine.
For general information about accessing MySQL with PHP database extensions, a list of the major PHP API offerings, and instructions on setting up the test database used in these tutorials, see the first sections of the first tutorial in this series, Developing MySQL Database Applications With PHP Part 1: Using the MySQL Improved Extension, mysqli.
Application developers who are new to the development of MySQL database applications with PHP are the target audience of this article.
Contents
Use the MySQL Native Driver for PHP, mysqlnd
To communicate with the MySQL database server, the extensions mysql and mysqli and the pdo_mysql driver rely on the MySQL client library, libmysql. The MySQL native driver for PHP (referred to 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. The mysqlnd driver 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 developer can choose between libmysql and mysqlnd when using ext/mysql, ext/mysqli, or PDO 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 extensions and the database driver are built with the support for mysqlnd.
The mysqlnd driver is not another PHP extension like mysqli, and it does not have an API that is exposed to the user. It is a library that provides similar functionality to that of the MySQL client library, libmysql. The mysqlnd and libmysql libraries implement the MySQL communication protocol, hence both of those libraries can be used to connect to the MySQL Server.
Because 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. Existing scripts that are running properly with ext/mysql, ext/mysqli, and PDO extensions built with libmysql support continue to run with the exact same behavior even when the ext/mysql, ext/mysqli, and PDO extensions are built with mysqlnd support.
From the performance perspective, mysqlnd might be as fast as libmysql and might even outperform libmysql in some cases. The generic recommendation is to try mysqlnd with your PHP application and then 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
- Might outperform libmysql in certain cases
- Persistent connections for ext/mysqli
- Uses PHP memory management, and supports PHP memory limit
- Keeps every row only once in memory, whereas with libmysql you have every row twice in memory
- Keeps a long list of performance-related statistics for bottleneck analysis
Install PHP With the MySQL Native Driver, mysqlnd
 |
Because most of the prepackaged PHP binary installations might 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.
- Get the source code for PHP 5.3 or later versions from the PHP.net site or from the PHP Snapshot build site.
- Make sure that Autoconf 2.13 or later and GNU M4 1.4.7 or later are installed on the build machine. Adjust the PATH environment variable to include the path to the autoconf and m4 tools.
For example:
- bash# ls /usr/local/bin/autoconf /usr/local/bin/m4
- /usr/local/bin/autoconf /usr/local/bin/m4
- bash# export PATH=.:/usr/local/bin:$PATH
复制代码
- On the Sun Solaris OS, create a soft link to gmake in the source directory.
| bash# ln -s /usr/bin/gmake make |
- Navigate to the source directory and run buildconf.
For example:
- bash# cd php5.3-200811132130
- bash# ./buildconf --force
复制代码
- For building PHP 5.3 or later with mysqlnd support on Unix or Linux systems, you can decide for all three MySQL extensions (ext/mysql, ext/mysqli, and PDO_mysql) whether they should be built using mysqlnd or libmysql. When choosing mysqlnd, use the string mysqlnd as the path to the mysql client library. If you don't specify the string mysqlnd as the 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 the mysqli extension with mysqlnd support, and PDO_MYSQL with libmysql.
The configure option shown below builds all the three extensions with mysqlnd support.
For example:
| 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] |
The configure script in PHP 6.0 and some builds of PHP 5.3 might 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 to build PHP.
On the Windows platform, ext/mysqli uses the MySQL native driver by default in PHP versions 5.3 and newer. Hence you don't need to configure libmysql.dll. - On the Sun Solaris OS, pass the -z muldefs option to the linker to proactively defend against linker errors like ld: fatal: symbol `<symbol>' is multiply-defined.
| bash# export LDFLAGS="-z muldefs" |
- Build PHP.
- Install PHP in the destination location.
- Enable the required database extensions in php.ini.
For example:
- 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"
复制代码
- Finally, verify the new PHP installation by checking the list of PHP modules.
For example:
- 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 $
- ..
复制代码
|
|