Home > Backend Development > PHP Tutorial > Optimizing Availability with MySQL Built-in Replication (5)_PHP Tutorial

Optimizing Availability with MySQL Built-in Replication (5)_PHP Tutorial

WBOY
Release: 2016-07-13 17:22:03
Original
785 people have browsed it

Step 4: Modify your database connection program
Now that you have established a mutual relationship between machine A and machine B, you need to modify your database connection program to benefit from this approach. The following function first attempts to connect to machine A, and if the connection cannot be established, connects to machine B.
$#@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;
I tested the database connection establishment process using the above technology in two situations. One is that the main MySQL service program is shut down, but the server is still running. The other is that the main server is shut down. If only mysqld is shut down, the connection will be immediately transferred to the backup machine; but if the entire server is shut down, there will be an infinite wait (I gave up tracking after two minutes - a very short attention span) because PHP is looking for a server that does not exist. server. Unfortunately, unlike the fsockopen function, the mysql_connect function does not have a timeout parameter, however we can use fsockopen to simulate a timeout.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/532383.htmlTechArticleStep 4: Modify your database connection program. Now that you have established a connection between machine A and machine B Mutual relationship, you need to modify the database connection program to get from this way...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template