Quickstart and Examples
PHP Manual

Running statements

The plugin can be used with any PHP MySQL extension (mysqli, mysql, PDO_MYSQL) compiled to use the mysqlnd library. PECL/mysqlnd_ms plugs into the mysqlnd library. It does not change the PHP MySQL extensions and their API.

Whenever a connection to MySQL is being opened, the plugin compares the host parameter value of the connect call with the section names from the plugin specific configuration file. If, for example, the plugin specific configuration file has a section myapp the section should be referenced by opening a MySQL connection to the host myapp

例1 Plugin specific configuration file (mysqlnd_ms_plugin.ini)

[myapp]
master[]=localhost:/tmp/mysql.sock
slave[]=192.168.2.27:3306

例2 Opening a load balanced connection

<?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");
?>

All of three connections opened in the example will be load balanced. The plugin will send read-only statements to the MySQL slave server with the IP 192.168.2.27 and listening on port 3306 for MySQL client connection. All other statements will be directed to the MySQL master server running on the host localhost accepting MySQL client connection on the Unix domain socket /tmp/mysql.sock. The plugin will use the user name username and the password password to connect to any of the MySQL servers listed in the section myapp of the plugins configuration file. Upon connect, the plugin will select database as the current schemata. The username, password and schema name are taken from the connect API calls and used for all servers. In other words: you must use the same username and password for every MySQL server listed in a plugin configuration file section.

The plugin does not change the API for running statements. Read-write splitting works out of the box. The following example assumes that there is no significant replication lag between the master and the slave.

例3 Executing statements

<?php
/* Load balanced following "myapp" section rules from the plugins config file */
$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()));

/* Statements will be run on the master */
if (!$mysqli->query("DROP TABLE IF EXISTS test")) {
 
printf("[%d] %s\n"$mysqli->errno$mysqli->error);
}
if (!
$mysqli->query("CREATE TABLE test(id INT)")) {
 
printf("[%d] %s\n"$mysqli->errno$mysqli->error);
}
if (!
$mysqli->query("INSERT INTO test(id) VALUES (1)")) {
 
printf("[%d] %s\n"$mysqli->errno$mysqli->error);
}

/* read-only: statement will be run on a slave */
if (!($res $mysqli->query("SELECT id FROM test")) {
 
printf("[%d] %s\n"$mysqli->errno$mysqli->error);
} else {
 
$row $res->fetch_assoc(); 
 
$res->close();
 
printf("Slave returns id = '%s'\n"$row['id'];
}
$mysqli->close();
?>

上の例の出力は以下となります。

Slave returns id = '1'

注意:

The plugin does not support native prepared statements. Prepared statements are not load balanced. Most users of PDO_MYSQL will be unaffected by this restriction because PDO_MYSQL is using a client-side prepared statement emulation by default.


Quickstart and Examples
PHP Manual