The MySQL native driver for PHP (mysqlnd) is a drop-in replacement for the MySQL Client Library (libmysql) for the PHP script language.
PHP MySQL extensions
The PHP MySQL extensions are lightweight wrappers on top of a C client library. There are 3 PHP MySQL extensions:
- ext/mysql (not recommended)
- ext/mysqli
- PDO_MySQL
Extension Feature Comparison »
It is recommended to use either the mysqli or PDO_MySQL extensions. It is not recommended to use the old mysql extension for new development.
Choosing a Library
The extensions can either use the mysqlnd or libmysql library to connect from PHP to MySQL. Choosing one or the other library is a compile time decision. Both libraries are supported and constantly being improved.
Library Feature Comparison »
MySQL recommends using the MySQL native driver for PHP (mysqlnd) together with ext/mysqli or PDO_MySQL.
Download Source Code & Binaries
All 3 PHP MySQL extensions and the mysqlnd library are part of the source code of PHP. The source code and binaries are available from: //php.net/downloads.php
PHP 5.4 has mysqlnd as default
As of PHP 5.4, the mysqlnd library is a php.net compile time default to all PHP MySQL extensions. Also, the php.net Windows team is using mysqlnd for the official PHP Windows distribution since mysqlnd became available in PHP 5.3.
Advantages of using mysqlnd
The mysqlnd library is highly optimized for and tightly integrated into PHP. The MySQL Client Library cannot offer the same optimizations because it is a general-purpose client library.
The mysqlnd library is using PHP internal C infrastructure for seamless integration into PHP. In addition, it is using PHP memory management, PHP Streams (I/O abstraction) and PHP string handling routines. The use of PHP memory management by mysqlnd allows, for example, memory savings by using read-only variables (copy on write) and makes mysqlnd apply to PHP memory limits. Additional advantages include:
- Powerful plugin API to extend feature set
- Asynchronous, non-blocking queries
- 150+ performance statistics
- Ships together with the PHP 5.3, and later, source
- No need to install MySQL Client Library
- Powerful plugins. See Below.
Mysqlnd Plugins
A wide range of mysqlnd plugins, providing additional features are available from PECL. Popular mysqlnd plugins include:
- Replication and Load Balancing plugin - mysqlnd_ms
- Query result cache plugin - mysqlnd_qc
- User handler plugin (write plugins with PHP not C) - mysqlnd_uh
- Mysqlnd connection multiplexing plugin mysqlnd_mux
- Mysqlnd Memcache plugin
- Multi Connect plugin - mysqlnd_mc
- Prepared Statement Handle Cache plugin - PECL/mysqlnd_pscache
- SQL Injection Protection plugin - PECL/mysqlnd_sip
Additional Resources
- MySQL PHP API
- MySQL Native Driver (Mysqlnd)
- PHP Manual: MySQL Drivers & Plugins
See some update at the end: 23 Dec 2019
MySQL and PHP is a love story that started long time ago. However the love story with MySQL 8.0 was a bit slower to start… but don’t worry it rules now !
The support of MySQL 8.0’s new default authentication method in PHP took some time and was added in PHP 7.2.8 but removed in PHP 7.2.11.
Now it’s fully supported in PHP 7.4 !
If you have installed PHP 7.4, you can see that the new plugin auth_plugin_caching_sha2_passwordis now available:
So no need to create a user with mysql_native_passwordas authentication method in MySQL 8.0 as explained in the following posts:
- //lefred.be/content/mysql-8-0-17-and-drupal-8-7/
- //lefred.be/content/migrating-to-mysql-8-0-for-wordpress-episode-1/
- //lefred.be/content/drupal-and-mysql-8-0-11-are-we-there-yet/
- //lefred.be/content/joomla-and-mysql-8-0-12/
- //lefred.be/content/mysql-8-0-and-magento/
In summary, if you want to use a more secure method to connect to your MySQL 8.0 form your PHP application, make sure you upgrade to PHP 7.4
Update:
The drawback of this new mysqli.so is that if you don’t modify the php.ini and by adding a value to mysqli.default_socket, when you try to connect to MySQL on localhost without specifying the socket path, the connection will fail with the following message:
PHP Warning: mysqli::__construct(): (HY000/2002): No such file or directory in <name of your file>.php on line 45 An error occurred when trying to establish a connection to the database: Error #2002You have then 2 solutions:
- add in php.ini a default value for mysqli.default_socket
- or specify the socket path when you initiate the connection to MySQL using mysqli in your code
This is also the default behavior on Ubuntu when using PHP 7.4 from ppa:ondrej/php repository.