Oracle 5.0 Reference Manual page 2585

Table of Contents

Advertisement

Mysqlnd replication and load balancing plugin (mysqlnd_ms)
} else {
$row = $res->fetch_assoc();
$res->close();
printf("@myrole = '%s'\n", $row['_role']);
}
$mysqli->close();
?>
The above example will output:
@myrole = 'master'
In the above example, using
master to a slave when running the
SQL hints can also be used to run
desired if the MySQL slave servers are typically behind the master, but you need current data from the
cluster.
In version 1.2.0 the concept of a service level has been introduced to address cases when current data
is required. Using a service level requires less attention and removes the need of using SQL hints for
this use case. Please, find more information below in the service level and consistency section.
Example 20.226. Fighting replication lag
<?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()));
/* Force use of master, master has always fresh and current data */
if (!$mysqli->query(sprintf("/*%s*/SELECT critical_data FROM important_table", MYSQLND_MS_MASTER_SWITCH
printf("[%d] %s\n", $mysqli->errno, $mysqli->error);
}
?>
A use case may include the creation of tables on a slave. If an SQL hint is not given, then
the plugin will send
CREATE
MYSQLND_MS_SLAVE_SWITCH
temporary reporting tables.
Example 20.227. Table creation on a slave
<?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()));
/* Force use of slave */
if (!$mysqli->query(sprintf("/*%s*/CREATE TABLE slave_reporting(id INT)", MYSQLND_MS_SLAVE_SWITCH))) {
printf("[%d] %s\n", $mysqli->errno, $mysqli->error);
}
/* Continue using this particular slave connection */
if (!$mysqli->query(sprintf("/*%s*/INSERT INTO slave_reporting(id) VALUES (1), (2), (3)", MYSQLND_MS_LA
printf("[%d] %s\n", $mysqli->errno, $mysqli->error);
}
/* Don't use MYSQLND_MS_SLAVE_SWITCH which would allow switching to another slave! */
if ($res = $mysqli->query(sprintf("/*%s*/SELECT COUNT(*) AS _num FROM slave_reporting", MYSQLND_MS_LAST
MYSQLND_MS_LAST_USED_SWITCH
statement.
SELECT
statements on the MySQL master server. This may be
SELECT
and
statements to the master. Use the SQL hint
INSERT
if you want to run any such statement on a slave, for example, to build
2565
prevents session switching from the

Advertisement

Table of Contents
loading

This manual is also suitable for:

Mysql 5.0

Table of Contents