Note: �汾����
�����Ǵ� 1.2.0-alpha�汾����ģ�mysqlnd_ms_set_qos() �� PHP 5.4.0 �汾��ʼ����ʹ�á�
��ͬ���͵� MySQL Ⱥ���ṩ�ˣ���ͬ�ķ��������һ���Լ����첽�� MySQL ����ͬ�� �ṩ���յ�����һ���ԣ�һ���������Ƿ��ܹ��õ���ǰ�����ݡ�״̬��һ���� slave �Ƿ��Ѿ��� master ��ȡ�����ĸ��¡�
ʹ�� MySQL ����ͬ���������������Ч�ԣ����ջ������ݵ�һ���ԡ�Ȼ���� ״̬�����Dz���ͬ���ġ�������ֻ��ָ���� slave ���� master ���Ӳ��ܵõ��������ݡ�
�� 1.2.0 �汾��ʼ������ܹ��Զ��Ľ��� MySQL ����ͬ���Ľڵ㣬����� session һ���� ������ɺ�ǿ��һ����Ҫ��session һ������ָһ���ͻ��˿��Զ�ȡ����д�����ݣ� �����ͻ��˿��ܲ��ܿ�������д�����ݡ���ǿ��һ����Ҫ����ָ���пͻ��˶��ܹ����� �������пͻ��˵�д�����ݡ�
Example #1 session һ���ԣ���ȡд������
{ "myapp": { "master": { "master_0": { "host": "localhost", "socket": "\/tmp\/mysql.sock" } }, "slave": { "slave_0": { "host": "127.0.0.1", "port": "3306" } } } }
Example #2 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()));
/* ʹ�� master ��ɶ�д���� */
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));
}
/* Ҫ�� session һ���ԣ���ȡд������ */
if (!mysqlnd_ms_set_qos($mysqli, MYSQLND_MS_QOS_CONSISTENCY_SESSION))
die(sprintf("[%d] %s\n", $mysqli->errno, $mysqli->error));
/* ���ѡ��һ���ı����ݵĽڵ㣬������ 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());
/* ���ص���������һ����״̬������¾����� */
if (!mysqlnd_ms_set_qos($mysqli, MYSQLND_MS_QOS_CONSISTENCY_EVENTUAL))
die(sprintf("[%d] %s\n", $mysqli->errno, $mysqli->error));
/* ���ѡ���κ�һ�� slaver �������¾����ݵĶ�ȡ */
if (!$res = $mysqli->query("SELECT item, price FROM specials"))
die(sprintf("[%d] %s\n", $mysqli->errno, $mysqli->error));
?>
������Ա�д�ڲ���������ļ��У�Ҳ����������ʱʹ�� mysqlnd_ms_set_qos() �趨�� �ڷ����У�ʹ���������ǿ�� session һ���ԣ�ֱ���ٴ�֪ͨ�ı䡣 orders ���е� SELECT �����ǰ��д��ʹ�õ� ������ִ�С���д��������������Ըı䡣
�ڴ� orders ����ȡ�����Ժָ���Ĭ�ϵķ��� (��������һ����)�� ��ʱ�����ִ��ѡ��ķ����������ٱ����ƣ������ specials �������� SELECT ��ѯ����һ�� slave �������н��С�
һ���µ���� SQL hint�Ĺ��ܣ�master_on_write �����趨�� �ھ�������� mysqlnd_ms_set_qos() ������ʹ�ã� ʹ������ֲ�Ը��á�
Example #3 Maximum age/slave lag
{ "myapp": { "master": { "master_0": { "host": "localhost", "socket": "\/tmp\/mysql.sock" } }, "slave": { "slave_0": { "host": "127.0.0.1", "port": "3306" } }, "failover" : "master" } }
Example #4 ���� 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()));
/* �� slave �ӳٲ����� 4 �룬��� Slave ��ȡ */
$ret = mysqlnd_ms_set_qos($mysqli,
MYSQLND_MS_QOS_CONSISTENCY_EVENTUAL,
MYSQLND_MS_QOS_OPTION_AGE, 4);
if (!$ret)
die(sprintf("[%d] %s\n", $mysqli->errno, $mysqli->error));
/* ѡ��һ�� slave��������û�иı� */
if (!$res = $mysqli->query("SELECT item, price FROM daytrade"))
die(sprintf("[%d] %s\n", $mysqli->errno, $mysqli->error));
/* �ָ�Ĭ��״̬��ʹ�����е� slave �� master */
if (!mysqlnd_ms_set_qos($mysqli, MYSQLND_MS_QOS_CONSISTENCY_EVENTUAL))
die(sprintf("[%d] %s\n", $mysqli->errno, $mysqli->error));
?>
����һ���Է������ʹ��һ����ѡ�IJ����趨���������ӳ٣�����ѡ�� slave�� ����趨���ֵ������������� slave �� SHOW SLAVE STATUS�� �ڷ����У�ֻ������ Slave_IO_Running=Yes, Slave_SQL_Running=Yes �� Seconds_Behind_Master <= 4 �� slave �ᱻִ����� SELECT item, price FROM daytrade��
��Ӧ������ʱ��������ִ�� SHOW SLAVE STATUS ��� �κδ������ warning �ķ�ʽ���������Ǵ�����Ϣ���ᱻ�����������С� ��ʹ���е� SHOW SLAVE STATUS ��ʧ���ˣ��û���ִ������Ҳ���ᱻ��ֹ�� ������ master ��Ϊ����ѡ��Ȼ��Ӧ�ò���Ҫ���κε�����
Note: ��ʱ�ͻ����IJ���
���κγ���Ŀ�ʼ�������е� slave ���� SHOW SLAVE STATUS ��ѯ����һ���dz���ʱ�ͻ����IJ�������Ҫ��������������MySQL ����ͬ����Ⱥ��û�� �ṩһ���ͻ��˴�һ�����Ŀ�������ȡ��ѡ������������ Ȼ����û�и�����Ч�ķ�ʽ��ȡ slave �ӳ١�
��ע�⣬���� SHOW SLAVE STATUS �ĸ������ƺͲ���˵���� ��ο� MySQl �IJο��ֲᡣ
��Ҫ��ֹ�������û���ҵ������ӳ������� slave ʱ������������Ҫ�������ļ� �����趨 master ��Ϊ���ϴ������û�� slave ������������ô���ϴ���ʼ������ �����ʹ�� master ȥִ����䡣
���û�� slave ��������������û���������ϴ���������ᱨ���� ��ʱ����䲻�ᱻִ�У����Ҵ�����Ϣ�ᱻд�����ӵ��С�
Example #5 �����ù��ϴ���
{ "myapp": { "master": { "master_0": { "host": "localhost", "socket": "\/tmp\/mysql.sock" } }, "slave": { "slave_0": { "host": "127.0.0.1", "port": "3306" } } } }
Example #6 No slave within time limit
<?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()));
/* �� slave �ӳٲ����� 4 �룬��� slave ִ�� */
$ret = mysqlnd_ms_set_qos($mysqli,
MYSQLND_MS_QOS_CONSISTENCY_EVENTUAL,
MYSQLND_MS_QOS_OPTION_AGE, 4);
if (!$ret)
die(sprintf("[%d] %s\n", $mysqli->errno, $mysqli->error));
/* Plugin picks any slave, which may or may not have the changes */
if (!$res = $mysqli->query("SELECT item, price FROM daytrade"))
die(sprintf("[%d] %s\n", $mysqli->errno, $mysqli->error));
/* Back to default: use of all slaves and masters permitted */
if (!mysqlnd_ms_set_qos($mysqli, MYSQLND_MS_QOS_CONSISTENCY_EVENTUAL))
die(sprintf("[%d] %s\n", $mysqli->errno, $mysqli->error));
?>
�������̻������
PHP Warning: mysqli::query(): (mysqlnd_ms) Couldn't find the appropriate slave connection. 0 slaves to choose from. Something is wrong in %s on line %d PHP Warning: mysqli::query(): (mysqlnd_ms) No connection selected by the last filter in %s on line %d [2000] (mysqlnd_ms) No connection selected by the last filter