PDA

View Full Version : localhost vs. whatever.powweb.com in mysql: a quick fix


netaustin
3-31-03, 10:43 PM
<?
if(!($link = mysql_connect("yourserver.powweb.com", "user", "pass"))){
$error_one = mysql_error();
if(!($link2 = mysql_connect("localhost", "user", "pass"))){
$error_two = mysql_error();
echo "Could not complete MySQL Connection<br>$error_one<br>$error_two<br>";
}
}
?>


So much traffic in this part of the forum is all about MySQL troubles - so I thought I'd have a go at coding a solution. All you need to do is replace your current call to mysql_connect() with this chunk, wherever it falls, and it will try both methods before failing.

No more rotating from yourserver.powweb.com to localhost!

This way, when the mysql server stops accepting unix socket connections or tcp connections, the other will work. That makes this script useful, because you can never predict which of the two will fail. The only case in which this script won't work is if they both fail.

Note: This will not work with mysql**.powweb.com - you have to use a tcp connection.

Jeff321
4-1-03, 01:12 AM
Nice, but I'm on the new architecture (mysql01). :)

Couldn't this also be used to automatically switch to a backup username when the first one fails due to exceeding your 3,600 connections/hour?

<?
if(!($link = mysql_connect("mysql01.powweb.com", "user", "pass"))){
$error_one = mysql_error();
if(!($link2 = mysql_connect("mysql01.powweb.com", "backup_user", "pass"))){
$error_two = mysql_error();
echo "Could not complete MySQL Connection<br>$error_one<br>$error_two<br>";
}
}
?>

netaustin
4-1-03, 01:57 AM
Actually, you could take it a step further, assuming you're not on the MySQL dedicated servers... just call the function dbConnector() every time you want to connect to your server, and it will try everything it can before it gives up... any number of users (just increase aUser and aPass, making sure that the elements line up), on localhost and whatever.powweb.com


<?
function dbConnector(){
$aUser = array("userOne","userTwo","userThree");
$aPass = array("passOne","passTwo","passThree");
for($i=0; $i<sizeof($aUser); $i++){
if(!($oConn = mysql_connect("whatever.powweb.com",$aUser[$i],$aPass[$i]))){
$errorlist .= mysql_error()."<br>";
if(!($oConn = mysql_connect("localhost",$aUser[$i],$aPass[$i]))){
$errorlist .= mysql_error()."<br>";
}
else{
return $oConn;
}
}
else{
return $oConn;
}
$iCount++;
}
die($errorlist);
}
//sample of a connection call...
//once you've connected and it gives you an object,
//treat it like you would any other MySQL connection object.
$mysql_connection_object = dbConnector();
//do your queries or whatever you want like usual
$mysql_close($mysql_connection_object);
?>


This script shouldn't eat up your resources... you have to make a successful connection before they take from your max_questions or max_connections.

Jeff321
4-1-03, 02:12 AM
Cool!

I have no problems with MySQL server resources myself, but I know many people around here have had issues lately. Maybe somebody will make good use out of your script.

I am new with PHP and MySQL, and have been trying to learn lately by writing my first script (http://forum.powweb.com/showthread.php?s=&postid=106151#post106151). :)