Oracle 5.0 Reference Manual page 2589

Table of Contents

Advertisement

Mysqlnd replication and load balancing plugin (mysqlnd_ms)
Example 20.232. Session consistency: read your writes
{
"myapp": {
"master": {
"master_0": {
"host": "localhost",
"socket": "\/tmp\/mysql.sock"
}
},
"slave": {
"slave_0": {
"host": "127.0.0.1",
"port": "3306"
}
}
}
}
Example 20.233. Requesting session consistency
<?php
$mysqli = new mysqli("myapp", "username", "password", "database");
if (!$mysqli)
/* Of course, your error handling is nicer... */
die(sprintf("[%d] %s\n", mysqli_connect_errno(), mysqli_connect_error()));
/* read-write splitting: master used */
if (!$mysqli->query("INSERT INTO orders(order_id, item) VALUES (1, 'christmas tree, 1.8m')")) {
/* Please use better error handling in your code */
die(sprintf("[%d] %s\n", $mysqli->errno, $mysqli->error));
}
/* Request session consistency: read your writes */
if (!mysqlnd_ms_set_qos($mysqli, MYSQLND_MS_QOS_CONSISTENCY_SESSION))
die(sprintf("[%d] %s\n", $mysqli->errno, $mysqli->error));
/* Plugin picks a node which has the changes, here: master */
if (!$res = $mysqli->query("SELECT item FROM orders WHERE order_id = 1"))
die(sprintf("[%d] %s\n", $mysqli->errno, $mysqli->error));
var_dump($res->fetch_assoc());
/* Back to eventual consistency: stale data allowed */
if (!mysqlnd_ms_set_qos($mysqli, MYSQLND_MS_QOS_CONSISTENCY_EVENTUAL))
die(sprintf("[%d] %s\n", $mysqli->errno, $mysqli->error));
/* Plugin picks any slave, stale data is allowed */
if (!$res = $mysqli->query("SELECT item, price FROM specials"))
die(sprintf("[%d] %s\n", $mysqli->errno, $mysqli->error));
?>
Service levels can be set in the plugins configuration file and at runtime using mysqlnd_ms_set_qos.
In the example the function is used to enforce session consistency (read your writes) for all future
statements until further notice. The
ensure the previous write can be seen by the client. Read-write splitting logic has been adapted to fulfill
the service level.
After the application has read its changes from the
level, which is eventual consistency. Eventual consistency puts no restrictions on choosing a node for
statement execution. Thus, the
The new functionality supersedes the use of SQL hints and the
option. In many cases
mysqlnd_ms_set_qos
statement on the
SELECT
orders
statement on the
SELECT
is easier to use, more powerful improves portability.
2569
table is run on the master to
orders
table it returns to the default service
table is executed on a slave.
specials
master_on_write
configuration

Advertisement

Table of Contents
loading

This manual is also suitable for:

Mysql 5.0

Table of Contents