mysqlnd is a PHP extension that is the default driver for working with MySQL in PHP 5.4. It works directly with the MySQL server, which means that the MySQL client, as well as the overhead to work with it, is no longer required!



    Despite the fact that the API for working with MySQL has not changed (mysqli and PDO extensions), new functions have appeared in the mysqli extension, among which it is worth highlighting the mysqli_fetch_all function .

    Previously, in order to get the full sample result, we had to write a similar construction:

    $result = mysqli_query($link, $query);
    $data = array();
    while($row = mysqli_fetch_assoc($result)) {
        $data[] = $row;

    If you use mysqli_fetch_all, you can avoid using such constructions:

    $result = mysqli_query($link, $query);
    $data = mysqli_fetch_all($result, MYSQLI_ASSOC);

    Native memory management

    Due to the fact that mysqlnd is an extension of PHP, we now have the opportunity to control (as well as view statistics on memory allocation through memory_get_usage ) memory allocation when working with MySQL.

    In other words, earlier, the following script could fall out of memory, only if the size of the $ data variable exceeded the memory allocation limit specified in the script, while the amount of data received from the database could be infinitely large:

    ini_set('memory_limit', '1M');
    $result = mysqli_query($link, $query);
    $data = array();
    while($row = mysqli_fetch_assoc($result)) {
        $data[] = $row;

    In the case of using mysqlnd, such a script will fall out of memory when the amount of data from the database exceeds the memory allocation limit specified in the script.

    By the way, in case of using mysqlnd, if you send a request that weighs more than max_allowed_packet on the MySQL server in Linux, you will get an error indicating this: "1153: got a packet bigger than max_allowed_packet bytes", but in Windows or in case of use the old driver will get the error: "2006: Server has gone away".

    Asynchronous requests

    mysqlnd provides a way to execute asynchronous queries in MySQL, that is, there is no need to wait for the results of query execution to continue the script.

    To execute an asynchronous query, it is necessary to specify the MYSQLI_ASYNC flag in mysqli_query.

    The query results are checked via mysqli_poll , if the query worked, the results can be obtained through mysqli_reap_async_query (please pay attention to the note to this function).

    Alas, until I fully understood all the nuances of using asynchronous queries and try to provide a separate review with cases in the very near future, if anyone is interested.

    UPD: my post about asynchronous queries in mysqlnd can be viewedhere


    mysqlnd provides an API for writing plugins, thanks to which very useful extensions were

    born : Mysqlnd replication and load balancing plugin The

    extension allows you to automatically distribute requests to MySQL using a simple config by sending all read requests to slaves, and write requests to the master.

    Mysqlnd query result cache plugin The

    extension allows you to cache query results at the driver level in the most popular caching engines (APC, Memcache, SQLite, you can add your own)

    Mysqlnd user handler plugin The

    extension provides a number of hooks that are executed while the driver is running. Handlers for these hooks can be described in PHP, inheriting extension classes.

    Also popular now: