PHP Checking slave status without mysql_connect timeouts

I have a web application with a mysql db master and four slave dbs. I want to process all (or almost all) of the read-only (SELECT) queries from subordinates. Our load balancer automatically sends the user to one of the slave machines, since they also use Apache / PHP and serve web pages. I use the include file to configure the database connection, for example:

//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 );
}

$Link $Link_Local SELECT. , . db , $Link_Local = mysql_connect() 30 , script. , (- ).

- , PHP? , mysql_connect 2-3 ?

. mysql_connect, .

+3
1

[MySQL] php.ini :

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

4 db, , db 1 . .

! !!

, . , , . .

+3

Source: https://habr.com/ru/post/1745850/


All Articles