锟斤拷锟斤拷转锟斤拷

默锟斤拷锟斤拷锟斤拷拢锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷失锟杰碉拷时锟斤拷锟斤拷泄锟斤拷洗锟斤拷锟斤拷锟斤拷锟轿拷朔锟街� 锟斤拷锟斤拷状态锟叫碉拷锟斤拷锟藉。 锟斤拷锟斤建锟斤拷锟街讹拷锟侥讹拷锟斤拷锟接达拷锟斤拷锟斤拷写锟斤拷锟斤拷锟斤拷锟皆诧拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷陆锟斤拷锟斤拷锟斤拷樱锟街达拷械锟斤拷锟侥诧拷询锟斤拷锟斤拷同锟斤拷锟斤拷展示锟斤拷锟斤拷锟斤拷锟斤拷

锟斤拷锟斤拷锟斤拷锟阶刺拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟侥诧拷锟斤拷锟斤拷锟斤拷模锟斤拷锟斤拷锟斤拷选锟斤拷锟皆讹拷锟斤拷锟叫达拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟� 通锟斤拷锟斤拷锟矫匡拷锟斤拷实锟街o拷锟斤拷 master 锟斤拷锟街癸拷锟较o拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 master 之前锟斤拷锟皆讹拷锟斤拷锟斤拷锟斤拷锟斤拷 一台 slave 去锟皆讹拷锟斤拷锟斤拷锟斤拷希锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷为 锟皆讹拷锟斤拷锟斤拷转锟斤拷 锟斤拷锟斤拷锟斤拷一锟斤拷十锟街帮拷全锟侥凤拷式锟斤拷锟斤拷锟斤拷筒锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷耍锟斤拷锟� concepts section 锟叫伙拷锟叫革拷锟斤拷锟剿碉拷锟斤拷锟�

Example #1 锟街讹拷锟斤拷锟斤拷转锟狡o拷锟斤拷选锟皆讹拷

{
    "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": { "roundrobin": [] }
    }
 }

Example #2 锟街讹拷锟斤拷锟斤拷转锟斤拷

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

$sql "SELECT 1 FROM DUAL";

/* error handling as it should be done regardless of the plugin */
if (!($res $link->query($sql))) {
  
/* plugin specific: check for connection error */
  
switch ($link->errno) {
    case 
2002:
    case 
2003:
    case 
2005:
      
printf("Connection error - trying next slave!\n");
      
/* load balancer will pick next slave */
      
$res $link->query($sql);
      break;
    default:
      
/* no connection error, failover is unlikely to help */
      
die(sprintf("SQL error: [%d] %s"$link->errno$link->error));
      break;
  }
}
if (
$res) {
  
var_dump($res->fetch_assoc());
}
?>