Possible low db user connection limit solution.

I have a web host that set the user database connection limit way low causing 'unable to connect to database' errors. This is what I used to hopefully overcome this problem, thought I would share this with the community see if this helps someone else or one of the more experienced members could improve on it (or have a better solution).

Note: this was not tested extensively (use at your own risk).

In the wp-config.php file replace lines:

/** MySQL database username */
define('DB_USER', 'wpdbusername');

/** MySQL database password */
define('DB_PASSWORD', 'wpdbpassword');

with the code below:

/* MySQL database connection limit fail-over *\
\* By Wayne D Schulz (CC BY-SA) 2013/04/19 */

$aUser = array( 'user1', 'user2', 'user3', 'user4', 'user5', 'user6', 'user7', 'user8', 'user9' );
$aPass = array( 'pass1', 'pass2', 'pass3', 'pass4', 'pass5', 'pass6', 'pass7', 'pass8', 'pass9' );

for($i=0; $i<sizeof($aUser); $i++) {
$dbLink = mysql_connect("localhost",$aUser[$i],$aPass[$i]);
if (!$dbLink) {
$errorlist .= date("D M j G:i:s T Y", time())." - ".mysql_error()."\r\n";
error_log($errorlist, 3, "/var/tmp/connect-errors.log");
}else{
$dbUser = $aUser[$i];
$dbPass = $aPass[$i];
mysql_close($dbLink);
break;
}
}

/* last dbUser and dbPass */
if (!isset($dbUser)) { $dbUser = "user10"; }
if (!isset($dbPass)) { $dbPass = "pass10"; }

/** MySQL database username */
define('DB_USER', $dbUser);

/** MySQL database password */
define('DB_PASSWORD', $dbPass);

  • Jack Kitterhing
    • Code Norris

    Hi there @netwarden

    I hope you are well today and thank you for posting this solution for our other members.

    One thing I would like to point out is, many shared hosting services set this limit for a reason and it may break their (TOS, Terms Of Service) to try and override and or change it.

    Who are you hosting with?

    Thank you for being a WPMU DEV member and have a great weekend!

    Kind Regards
    Jack.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.