<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Failover</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mysqlnd-ms.quickstart.cache.html">Cache integration</a></div> <div class="next" style="text-align: right; float: right;"><a href="mysqlnd-ms.quickstart.partitioning.html">Partitioning and Sharding</a></div> <div class="up"><a href="mysqlnd-ms.quickstart.html">Quickstart and Examples</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="mysqlnd-ms.quickstart.failover" class="section"> <h2 class="title">Failover</h2> <p class="para"> By default, the plugin does not attempt to fail over if connecting to a host fails. This prevents pitfalls related to <a href="mysqlnd-ms.quickstart.connectionpooling.html" class="link">connection state</a>. It is recommended to manually handle connection errors in a way similar to a failed transaction. You should catch the error, rebuild the connection state and rerun your query as shown below. </p> <p class="para"> If connection state is no issue to you, you can alternatively enable automatic and silent failover. Depending on the configuration, the automatic and silent failover will either attempt to fail over to the master before issuing and error or, try to connect to other slaves, given the query allowes for it, before attempting to connect to a master. Because <a href="mysqlnd-ms.failover.html" class="link">automatic failover</a> is not fool-proof, it is not discussed in the quickstart. Instead, details are given in the concepts section below. </p> <p class="para"> <div class="example" id="example-1788"> <p><strong>Example #1 Manual failover, automatic optional</strong></p> <div class="example-contents"> <div class="inicode"><pre class="inicode">{ "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": [] } } }</pre> </div> </div> </div> </p> <p class="para"> <div class="example" id="example-1789"> <p><strong>Example #2 Manual failover</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$mysqli </span><span style="color: #007700">= new </span><span style="color: #0000BB">mysqli</span><span style="color: #007700">(</span><span style="color: #DD0000">"myapp"</span><span style="color: #007700">, </span><span style="color: #DD0000">"username"</span><span style="color: #007700">, </span><span style="color: #DD0000">"password"</span><span style="color: #007700">, </span><span style="color: #DD0000">"database"</span><span style="color: #007700">);<br />if (!</span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">)<br /> </span><span style="color: #FF8000">/* Of course, your error handling is nicer... */<br /> </span><span style="color: #007700">die(</span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #DD0000">"[%d] %s\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">mysqli_connect_errno</span><span style="color: #007700">(), </span><span style="color: #0000BB">mysqli_connect_error</span><span style="color: #007700">()));<br /><br /></span><span style="color: #0000BB">$sql </span><span style="color: #007700">= </span><span style="color: #DD0000">"SELECT 1 FROM DUAL"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/* error handling as it should be done regardless of the plugin */<br /></span><span style="color: #007700">if (!(</span><span style="color: #0000BB">$res </span><span style="color: #007700">= </span><span style="color: #0000BB">$link</span><span style="color: #007700">-></span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #0000BB">$sql</span><span style="color: #007700">))) {<br /> </span><span style="color: #FF8000">/* plugin specific: check for connection error */<br /> </span><span style="color: #007700">switch (</span><span style="color: #0000BB">$link</span><span style="color: #007700">-></span><span style="color: #0000BB">errno</span><span style="color: #007700">) {<br /> case </span><span style="color: #0000BB">2002</span><span style="color: #007700">:<br /> case </span><span style="color: #0000BB">2003</span><span style="color: #007700">:<br /> case </span><span style="color: #0000BB">2005</span><span style="color: #007700">:<br /> </span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"Connection error - trying next slave!\n"</span><span style="color: #007700">);<br /> </span><span style="color: #FF8000">/* load balancer will pick next slave */<br /> </span><span style="color: #0000BB">$res </span><span style="color: #007700">= </span><span style="color: #0000BB">$link</span><span style="color: #007700">-></span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #0000BB">$sql</span><span style="color: #007700">);<br /> break;<br /> default:<br /> </span><span style="color: #FF8000">/* no connection error, failover is unlikely to help */<br /> </span><span style="color: #007700">die(</span><span style="color: #0000BB">sprintf</span><span style="color: #007700">(</span><span style="color: #DD0000">"SQL error: [%d] %s"</span><span style="color: #007700">, </span><span style="color: #0000BB">$link</span><span style="color: #007700">-></span><span style="color: #0000BB">errno</span><span style="color: #007700">, </span><span style="color: #0000BB">$link</span><span style="color: #007700">-></span><span style="color: #0000BB">error</span><span style="color: #007700">));<br /> break;<br /> }<br />}<br />if (</span><span style="color: #0000BB">$res</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$res</span><span style="color: #007700">-></span><span style="color: #0000BB">fetch_assoc</span><span style="color: #007700">());<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mysqlnd-ms.quickstart.cache.html">Cache integration</a></div> <div class="next" style="text-align: right; float: right;"><a href="mysqlnd-ms.quickstart.partitioning.html">Partitioning and Sharding</a></div> <div class="up"><a href="mysqlnd-ms.quickstart.html">Quickstart and Examples</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>