Using MySQL built-in replication to optimize usability (v) _php Tutorial

Source: Internet
Author: User
Fourth step: Modify your database connection program
Now that you have established a relationship between A and B machines, you need to modify the database connection program to get the benefit from this approach. The following function first attempts to connect with a machine, and if the connection cannot be established, it is connected to the B machine.
$#@60;? Php
/********************************************************
function Db_connect ()
Returns a link identifier on success, or false On error
********************************************************/
function Db_connect () {
$username = "Repluser";
$password = "password";
$primary = "10.1.1.1";
$backup = "10.1.1.2";
# attempt connection to Primary
if (! $link _id = @mysql_connect ($primary, $username, $password))
# Attempt connection to secondary
$link _id = @mysql_connect ($secondary, $username, $password)
return $link _id;
}
? $#@62;
In both cases, I tested the database connection setup process using the above technology, one of which was the main MySQL service was closed, but the server was still running, and the other was the primary server was down. If the mysqld is turned off, the connection will immediately turn to the standby machine, but if the entire server shuts down, there is an infinite wait (two minutes later I give up the trace-a short attention span) because PHP is looking for a nonexistent server. Unfortunately, unlike the Fsockopen function, the mysql_connect function does not have a timeout parameter, but we can use Fsockopen to simulate a timeout processing.

http://www.bkjia.com/PHPjc/532383.html www.bkjia.com true http://www.bkjia.com/PHPjc/532383.html techarticle Fourth Step: Modify your database connection program now that you have established a relationship between A and B machines, you need to modify the database connection program so that you can get it in this way ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.