Ӧ�ó���ʹ�ñ������Ӧ������û��� API ����ʵ��һЩ�������������ڲ�� �ı������ӿ��ƣ�API ���ÿ��ܷ��ز���Ԥ�ڵĴ������������������ӿ����У� ��ô���ӽ����ڴ���һ���������������ӣ�����һ�����ӳء���ôһ�������ź� ������Ϣ�������������ӿ����У���ô�����ܱ��������κ�һ��ʵ�ʵ������С�
���ʹ��Ĭ�ϵı������ӣ���ô������ʵ�ʲ�ѯִ����ǰ�Dz�����ʵ�����ӵġ� ����һ��ִ������ API ���û����һ�����Ӵ�������ķ����У��������� slave ��ִ��һ������ʱ����һ���������Ӵ���IJ�������Ϊ�������ļ��� û���趨һ����Ч�� slave��
Example #1 Provoking a connection error
{ "myapp": { "master": { "master_0": { "host": "localhost", "socket": "\/tmp\/mysql.sock" } }, "slave": { "slave_0": { "host": "invalid_host_name", } }, "lazy_connections": 1 } }
��ȷ��ָ�����������趨ֻ��Ϊ�˷���Ŀ��ʹ�á�
Example #2 �ڲ�ѯִ��ʱ�������Ӵ���
<?php
$mysqli = new mysqli("myapp", "username", "password", "database");
if (mysqli_connect_errno())
/* Of course, your error handling is nicer... */
die(sprintf("[%d] %s\n", mysqli_connect_errno(), mysqli_connect_error()));
/* ���� 1���������а� SQL �û����������û�� SELECT ������ master ������ */
if (!$mysqli->query("SET @myrole='master'")) {
printf("[%d] %s\n", $mysqli->errno, $mysqli->error);
}
/* ���� 2������ SELECT �� slave������һ�����Ӵ��� */
if (!($res = $mysqli->query("SELECT @myrole AS _role"))) {
printf("[%d] %s\n", $mysqli->errno, $mysqli->error);
} else {
$row = $res->fetch_assoc();
$res->close();
printf("@myrole = '%s'\n", $row['_role']);
}
$mysqli->close();
?>
�������̵���������ڣ�
PHP Warning: mysqli::query(): php_network_getaddresses: getaddrinfo failed: Name or service not known in %s on line %d PHP Warning: mysqli::query(): [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known (trying to connect via tcp://invalid_host_name:3306) in %s on line %d [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known
Ӧ�ó���ϣ��ͨ����Ч�Ĵ���������������Ӵ���
����ʹ�õ�Ŀ�ģ�Ӧ�ó���Ӧ��ϣ��������Щ�����͵Ĵ������� 2002 (CR_CONNECTION_ERROR) - Can't connect to local MySQL server through socket '%s' (%d), 2003 (CR_CONN_HOST_ERROR) - Can't connect to MySQL server on '%s' (%d) �� 2005 (CR_UNKNOWN_HOST) - Unknown MySQL server host '%s' (%d)�� ���磬Ӧ�ó�������ж���Щ�����ţ������ֶ���ִ��ijЩ����������������Զ��Ľ��� ��Щ���������� master ��������Ϊ��Щ���������IJ�����
Example #3 ����һ�����Ӵ���
{ "myapp": { "master": { "master_0": { "host": "localhost" } }, "slave": { "slave_0": { "host": "invalid_host_name" }, "slave_1": { "host": "192.168.78.136" } }, "lazy_connections": 1, "filters": { "roundrobin": [ ] } } }
��ȷ��ָ����������ֻΪ�˽��з���ʹ�ã�����ʹ��Ĭ�ϵĸ��ؾ������ random once��
Example #4 �dz������Ĵ�����
<?php
$mysqli = new mysqli("myapp", "username", "password", "database");
if (mysqli_connect_errno())
/* Of course, your error handling is nicer... */
die(sprintf("[%d] %s\n", mysqli_connect_errno(), mysqli_connect_error()));
/* Connection 1, connection bound SQL user variable, no SELECT thus run on master */
if (!$mysqli->query("SET @myrole='master'")) {
printf("[%d] %s\n", $mysqli->errno, $mysqli->error);
}
/* ���� 2����һ�� Slave ִ�� */
$res = $mysqli->query("SELECT VERSION() AS _version");
/* ǿӲ�Ľ��д����� */
if (2002 == $mysqli->errno || 2003 == $mysqli->errno || 2004 == $mysqli->errno) {
/* ���� 3����һ�� slave ����ʧ�ܣ�������һ�� slave ���� */
$res = $mysqli->query("SELECT VERSION() AS _version");
}
if (!$res) {
printf("ERROR, [%d] '%s'\n", $mysqli->errno, $mysqli->error);
} else {
/* ������ 3 �л�ȡ������Ϣ������û�д��� */
printf("SUCCESS, [%d] '%s'\n", $mysqli->errno, $mysqli->error);
$row = $res->fetch_assoc();
$res->close();
printf("version = %s\n", $row['_version']);
}
$mysqli->close();
?>
�������̵���������ڣ�
[1045] Access denied for user 'username'@'localhost' (using password: YES) PHP Warning: mysqli::query(): php_network_getaddresses: getaddrinfo failed: Name or service not known in %s on line %d PHP Warning: mysqli::query(): [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known (trying to connect via tcp://invalid_host_name:3306) in %s on line %d SUCCESS, [0] '' version = 5.6.2-m5-log
����һЩԭ�����ӿ��Ʋ������ܺ��������������������м�����еĴ��� ���磬���Ǽ��������ӳ����� 3 ���������ӣ�һ�������� master ���ӣ�2���� slave ���ӡ� Ӧ�ó���ͨ�� mysqli_select_db() API ���øı䵱ǰ�������ݿ⣬ �����ص�������������ҳ��Ը��ĵ�ǰ������������ͳһ���ǵ�����״̬����� master �Ѿ��ɹ��ĸ��������ݿ⣬���� slave ���Ӹ���ʧ�ܣ���ô��ʹ�õ�һ�� slave ���ӵ�ʱ�� ���л����һ�����Ӵ����ڵڶ��� slave ����Ҳ��һ���ģ���ô�ڵ�һ�� slave �ϵ� ������Ϣ������ʧ��
��������£�����ͨ����� E_WARNING ���͵Ĵ������ mysqlnd_ms debug and trace log��