Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 798479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:58:39+00:00 2026-05-14T22:58:39+00:00

I have a web-app that has a master mysql db and four slave dbs.

  • 0

I have a web-app that has a master mysql db and four slave dbs. I want to handle all (or almost all) read-only (SELECT) queries from the slaves. Our load-balancer sends the user to one of the slave machines automatically, since they are also running Apache/PHP and serving webpages. I am using an include file to setup the connection to the databases, such as:

//for master server (i.e. - UPDATE/INSERT/DELETE statements)
$Host = "10.0.0.x";
$User = "xx";
$Password = "xx";
$Link = mysql_connect( $Host, $User, $Password );
if( !$Link ) )
{
   die( "Master database is currently unavailable.  Please try again later." );
}

//this connection can be used for READ-ONLY (i.e. - SELECT statements) on the localhost
$Host_Local = "localhost";
$User_Local = "xx";
$Password_Local = "xx";
$Link_Local = mysql_connect( $Host_Local, $User_Local, $Password_Local );

//fail back to master if slave db is down
if( !$Link_Local ) )
{
   $Link_Local = mysql_connect( $Host, $User, $Password );
}

I then use $Link for all update queries and $Link_Local as the connection for SELECT statements. Everything works fine until the slave server database goes down. If the local db is down, the $Link_Local = mysql_connect() call takes at least 30 seconds before it gives up on trying to connect to the localhost and returns back to the script. This causes a huge backlog of page serves and basically shuts down the system (due to the extremely slow response time).

Does anyone know of a better way to handle connections to slave servers via PHP? Or, is there some kind of timeout function that could be used to stop the mysql_connect call after 2-3 seconds?

Thanks for the help. I searched the other mysql_connect threads, but didn’t see any that addressed this issue.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T22:58:40+00:00Added an answer on May 14, 2026 at 10:58 pm

    Under the [MySQL] section of your php.ini, set this value:

    ; Maximum time (in secondes) for connect timeout. -1 means no limit
    mysql.connect_timeout = 1
    

    If you have 4 db slaves, then you should have a pretty decent network that can respond to a db request in under 1 second. Trust your server build.

    Then fix your slaves going down!! That is a PROBLEM!!

    Finally, not every page request should need a link to the master DB. You should try putting a connection manager class in place that will decide based on the query type which database to use. That will lower the number of connections to your master DB as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a web app that has a list of user comments. I want
I have an existing web app that has a data layer and a bll
I have C# MVC web app that has some textboxes that in IE9 you
I have a very simple Mac Cocoa app that just has a Web View
We have a web app that we update and release almost daily. We use
I have a small web app I wrote that has a Dijit.layout.BorderContainer nested within
I have a Django webapp that has both a front-end, web-accessible component and an
We have an web app that is talking to Netsuite via Netsuite's web services.
I have a web app that I've recently applied a jQuery ThemeRoller theme to.
I have a web app that lets a client define a daily rate for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.