The MySQL replication and load balancing plugin for PHP, PECL/mysqlnd_ms, aims to make using a cluster of MySQL servers instead of a single server as transparent as possible. Should you ever want to migrate your PHP app from a single MySQL server to any kind of MySQL cluster, install PECL/mysqlnd_ms and see how much breaks. A simple app, might need no code changes at all. Your APIs connect call remains unchanged, your query execution calls remain the same: one connection handle transparently switched to an appropriate cluster node (read-write splitting, weighted load balancing, …).
/* Or, use PDO_MySQL... One user handle for an entire cluster */
$link = new mysqli("mycluster", ...);
/* Goes automatically to a slave, if read-write splitting is enabled */
$link->query("SELECT ...");
/* All actual connections to master or slave will use UTF8 from now on */
$link->set_charset("utf8");
/* With read-write splitting on, goes to master */
$link->query("INSERT ...");
Tell PECL/mysqlnd_ms that connect("mycluster", ...)
means you want to connect to a cluster “mycluster” which has, for example, is a MySQL Replication cluster with master A and slaves B, C. Call $link->select_db("mydb")
, $link->autocommit(false)
, $link->set_charset("utf8")
, … and no matter to which node (A, B, C) you actually get automatically connected, the connection has those settings! Unless, you are using the sharding feature of our current, still very limited MySQL Fabric support… A new internal connection pool shall cure this and more. A tour through PECL/mysqlnd_ms internals to follow.
PECL/mysqlnd_ms example configuration for a simple MySQL Replication setup |
---|
|
How a single user handle stands for many actual connections
No matter whether you use PECL/mysqlnd_ms for a simple MySQL Replication cluster or a farm of MySQL servers that shard data one or the other way, it always confronts you with a massive change: a single user conection handle is mapped internally to many actual connections.
PHP script (*.phpt) | Inside PHP (*.c) | |||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
Furthermore, to keep the number of actual connections low, the PECL/mysqlnd_ms plugin for mysqlnd, will – by default – use lazy connections. A lazy connection to a node is not established before a query is to be run on the node. Assume you do $link = mysqli_connect("mycluster"); $link->set_charset("UTF8");
. What happens inside PECL/mysqlnd_ms is very different from the normal case. Normally, without the plugin, $link
would be one actual, established connection to a MySQL server and the server would immediately reply to your set_charset()
command.
PHP script (*.phpt) | Inside PHP (*.c) |
---|
Truncated by Planet PHP, read more at the original (another 17514 bytes)
more
{ 0 comments... » PECL/mysqlnd_ms: summer time, (connection) pool time – Fabric support internals read them below or add one }
Post a Comment