- URL: https://www.laruence.com/en/2011/10/05/2192.html
- Please include attribution when republishing.
Starting with PHP 5.3, mysqlnd (the MySQL native driver for PHP), the MySQL connection library developed by the MySQL team specifically for PHP, is finally released together with PHP. The main purpose of mysqlnd is to resolve the long-standing licensing conflict between mysql and php. It will be released as part of the PHP source code.
Today, I'd like to introduce a mysqlnd plugin: mysqlnd_ms. This plugin was developed jointly by the mysqlnd developers Andrey Hristov, Ulf Wendel and johannes, and is currently released on PECL: mysqlnd_ms
On the extension's intro page we can see its feature description:
The replication and load balancing plugin is a plugin for the mysqlnd library. It can be used with PHP MySQL extensions (ext/mysql, ext/mysqli, PDO_MySQL) if they are compiled to use mysqlnd. The plugin inspects queries to do read-write splitting. Read-only queries are sent to configured MySQL replication slave servers; all other queries are redirected to the MySQL replication master server. Very little, if any, application changes required, dependent on the usage scenario required.
This extension mainly implements connection persistence and switching, load balancing, and read-write splitting. In other words, the extension inspects the queries PHP sends to MySQL: if it's a "read" query, the query is sent to the slave (as specified in the config), and load balancing is supported; if it's a "write" query, the query is sent to the master.
However, this extension needs to be used together with mysqlnd (starting from PHP 5.4 beta1, we have made mysqlnd the default link target for mysql, mysqli, and pdo; of course, you can also use --with-mysql=*** to specify that you want to link to libmysql).
The way to use this extension is also simple. First, define the configuration in php.ini:
mysqlnd_ms.enable=1 mysqlnd_ms.ini_file=/path/to/mysqlnd_ms_plugin.ini
Then, configure the MySQL master and slave in the mysqlnd_ms_plugin.ini you specified:
[myapp] master[]=localhost:/tmp/mysql.sock slave[]=192.168.2.27:3306
After this blog was published, Ulf reminded me that starting from 1.1.0, the config file format has been changed to JSON:
Ulf_Wendel: @laruence >Thx for the blog. Please note, mysqlnd_ms config format was changed in 1.1.0. Now JSON based
So the new configuration should look like:
{
"myapp": {
"master": {
"master_0": {
"host": "localhost",
"socket": "\/tmp\/mysql.sock"
}
},
"slave": {
"slave_0": {
"host": "192.168.2.27",
"port": "3306"
}
}
}
}
The connection method is modified as follows:
<?php
/* Load balanced following "myapp" section rules from the plugins config file */
$mysqli = new mysqli("myapp", "username", "password", "database");
$pdo = new PDO('mysql:host=myapp;dbname=database', 'username', 'password');
$mysql = mysql_connect("myapp", "username", "password");
?>
After that, you develop just as before.
For more information, see: http://php.net/mysqlnd_ms
Be First to Comment