note 125530 added to function.mysql-pconnect

From: Date: Sat, 21 Nov 2020 09:56:48 +0000
Subject: note 125530 added to function.mysql-pconnect
Groups: php.notes 
Request: Send a blank email to php-notes+get-215874@lists.php.net to get a copy of this message
Here's a nifty little class which will load balance across multiple replicated MySQL server instances, using persistent connections, automatically removing failed MySQL servers from the pool. You would ONLY use this for queries, never inserts/updates/deletes, UNLESS you had a multi-master situation where updates to any database serverautomatically replicate to the other servers (I don't know whether that's possible with MySQL). Using this class, you get a connection to a MySQL server like this: $con = MySQLConnectionFactory::create(); Here is the class (you'll need to customize the $SERVERS array for your configuration -- note that you would probably use the same username, password and database for all of the servers, just changing the host name, but you're not forced to use the same ones): <?php class MySQLConnectionFactory { static $SERVERS = array( array( 'host' => 'myHost1', 'username' => 'myUsername1', 'password' => 'myPassword1', 'database' => 'myDatabase1'), array( 'host' => 'myHost2', 'username' => 'myUsername1', 'password' => 'myPassword2', 'database' => 'myDatabase2') ); public static function create() { // Figure out which connections are open, automatically opening any connections // which are failed or not yet opened but can be (re)established. $cons = array(); for ($i = 0, $n = count(MySQLConnectionFactory::$SERVERS); $i < $n; $i++) { $server = MySQLConnectionFactory::$SERVERS[$i]; $con = mysql_pconnect($server['host'], $server['username'], $server['password']); if (!($con === false)) { if (mysql_select_db($server['database'], $con) === false) { echo('Could not select database: ' . mysql_error()); continue; } $cons[] = $con; } } // If no servers are responding, throw an exception. if (count($cons) == 0) { throw new Exception ('Unable to connect to any database servers - last error: ' . mysql_error()); } // Pick a random connection from the list of live connections. $serverIdx = rand(0, count($cons)-1); $con = $cons[$serverIdx]; // Return the connection. return $con; } } ?> Source: https://www.el7lwa.com ---- Server IP: 45.112.84.4 Probable Submitter: 185.85.3.174 (proxied: 196.156.162.241) ---- Manual Page -- http://php.net/manual/en/function.mysql-pconnect.php Edit -- https://master.php.net/note/edit/125530 Del: integrated -- https://master.php.net/note/delete/125530/integrated Del: useless -- https://master.php.net/note/delete/125530/useless Del: bad code -- https://master.php.net/note/delete/125530/bad+code Del: spam -- https://master.php.net/note/delete/125530/spam Del: non-english -- https://master.php.net/note/delete/125530/non-english Del: in docs -- https://master.php.net/note/delete/125530/in+docs Del: other reasons-- https://master.php.net/note/delete/125530 Reject -- https://master.php.net/note/reject/125530 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#215874) next »