Optimizing availability with MySQL built-in replication (6) _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Use MySQL built-in replication to optimize availability (6 ). Step 5: an improved Database Connection program $ #@ 60 ;? Php *************************************** * **************** functiondb_connect_plus () returnsalinkidentifieronsucces Step 5: an improved Database Connection program
$ #@ 60 ;? Php
/*************************************** *****************
Function db_connect_plus ()
Returns a link identifier on success, or false on error
**************************************** ****************/
Function db_connect_plus (){
$ Username = "username ";
$ Password = "password ";
$ Primary = "10.1.1.1 ";
$ Backup = "10.1.1.2 ";
$ Timeout = 15; // timeout in seconds
If ($ fp = fsockopen ($ primary, 3306, & $ errno, & $ errstr, $ timeout )){
Fclose ($ fp );
Return $ link = mysql_connect ($ primary, $ username, $ password );
}
If ($ fp = fsockopen ($ secondary, 3306, & $ errno, & $ errstr, $ timeout )){
Fclose ($ fp );
Return $ link = mysql_connect ($ secondary, $ username, $ password );
}
Return 0;
}
? $ #@ 62;
This new improved function provides us with an adjustable timeout feature, which is exactly what mysql_connect function lacks. If the connection fails immediately, this is the case if the machine is "active", but mysqld "drops, the function is immediately moved to the second server. The above function is quite robust. test it before trying to connect and check whether the service program listens on the specified port so that your script times out after an acceptable period of time, allow you to handle errors as appropriate. If you have modified the default port 3306, make sure to modify the port number.

Else $ # @ 60 ;? Php /************************************** * ***************** function db_connect_plus () returns a link identifier on succes...

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.