How to setup mysqln_ms extension for php 7.x

Posted by on March 9, 2019

Mysqlnd_ms is a handy php extension to automaticaly send the read queries to slave mysql servers and writes to the master.

Download repo from here

In order to compile it you must download some packages

sudo apt-get install php-dev php-json libxml2-dev libmemcached-dev
cd Documents/mysqlnd_ms-master
sudo make install

Add these lines in php.ini

sudo gedit /etc/php/7.0/php.ini
mysqlnd_ms.enable = On

And add the module

sudo nano /etc/php/7.0/mods-available/mysqlnd_ms.ini
extension =

And a link inside /etc/php/7.0/apache2/conf.d/ with name 20-mysqlnd_ms.ini (Lines extension = and extension = must run before mysqlnd).

For the case of one master and one slave read we create this file on a path we chose:

    "myapp": {
        "master": {
            "master_0": {
                "host": "localhost"
        "slave": [
            "slave_0": {
                "host": "",

After restartung the server your queries will run either on master or slaves. By default the SELECT queries run on slave and everything else on the master.

You connect with mysqli like that:

$mysqli = new mysqli("myapp", "username", "password", "database");