���ݿ�Ⱥ�������ڸ��ָ�����ԭ�����ģ������������������������̴����� ��������������ͬʱ�����ĵ����ܡ�Ⱥ����ʱ����Ϸ��������ܣ������������ӵ����� �ֲ�ɸ��Ӽ������ӿɿصĵ�Ԫ��
�������֧�ָ��ָ����� MySQL Ⱥ�飬һЩȺ�������һЩ��������ķ��������ǿ��� ������ʹ�á����֧����õ� MySQL ����ͬ������˺��� (Ӧ�ü�����)��
MySQL ����ͬ��֧�ֹ��˷�ʽ�ķ��������������㴴���������ݿ�ͬ�������߲������ݿ�ͬ���� ������Ҫ��Ӧ���ܹ�ӵ��ͬ���IJ��ԣ������ͨ�� node_groups �ֶ���֧��������ԣ�����ʹ��ʵ�����ʵı��������
�� 1.5.0 �汾��ʼ������ͨ���ڵ�����˺� SQL hints ����ֶ��ķ������� �ڵ���������������㽫 master �� slave ������һ�����š� �����У� master_0 �� slave_0 ������һ������Ϊ Partition_A �����С������ܹ���ȫ�����һ��Ⱥ�顣 ���磬�����ʹ��һ���ڵ�Ⱥ�����ڹ�������ʹ��ʹ��Ⱥ��������Ϊһ����ַ���� ���� Shard_A_Range_0_100
Example #1 Cluster node groups
{ "myapp": { "master": { "master_0": { "host": "localhost", "socket": "\/tmp\/mysql.sock" } }, "slave": { "slave_0": { "host": "simulate_slave_failure", "port": "0" }, "slave_1": { "host": "127.0.0.1", "port": 3311 } }, "filters": { "node_groups": { "Partition_A" : { "master": ["master_0"], "slave": ["slave_0"] } }, "roundrobin": [] } } }
Example #2 ͨ�� SQL hints ����ֶ�����
<?php
function select($mysqli, $msg, $hint = '') {
/* Note: weak test, two connections to two servers may have the same thread id */
$sql = sprintf("SELECT CONNECTION_ID() AS _thread, '%s' AS _hint FROM DUAL", $msg);
if ($hint) {
$sql = $hint . $sql;
}
if (!($res = $mysqli->query($sql))) {
printf("[%d] %s", $mysqli->errno, $mysqli->error);
return false;
}
$row = $res->fetch_assoc();
printf("%d - %s - %s\n", $row['_thread'], $row['_hint'], $sql);
return true;
}
$mysqli = new mysqli("myapp", "user", "password", "database");
if (!$mysqli)
/* Of course, your error handling is nicer... */
die(sprintf("[%d] %s\n", mysqli_connect_errno(), mysqli_connect_error()));
/* All slaves allowed */
select($mysqli, "slave_0");
select($mysqli, "slave_1");
/* only servers of node group "Partition_A" allowed */
select($mysqli, "slave_1", "/*Partition_A*/");
select($mysqli, "slave_1", "/*Partition_A*/");
?>
6804 - slave_0 - SELECT CONNECTION_ID() AS _thread, 'slave1' AS _hint FROM DUAL 2442 - slave_1 - SELECT CONNECTION_ID() AS _thread, 'slave2' AS _hint FROM DUAL 6804 - slave_0 - /*Partition_A*/SELECT CONNECTION_ID() AS _thread, 'slave1' AS _hint FROM DUAL 6804 - slave_0 - /*Partition_A*/SELECT CONNECTION_ID() AS _thread, 'slave1' AS _hint FROM DUAL
Ĭ�ϵģ����ʹ���������õ� master �� slave ���в�ѯ�������������һ����ѯ��ʹ�� /*node_group*/ �� SQL hint����ô�����ֻʹ���� node_group �г��ķ��������в�ѯ���������ԣ�SELECT ��ѯ������ֻ���� /*Partition_A*/ �г��� slave_0 �н��С�