Jump to content

Use alternate database if one connect fails


matthew_ellis24

Recommended Posts

Hi, I've had a look for this, but I'm not sure it's possible without specialist software. So what I want to know is, is it possible to set up your queries in such a way that if your connection to one database fails (because your host is naff and keeps crashing) it connects to an alternate server?

 

I use this to connect to my database:

mysql_connect($host,$username,$password);

mysql_select_db($database) or die( "Unable to select database");

 

and I've tried replacing the "or die" command with a new mysql connect, but no luck.

 

Is it possible?

Thanks

Matt

Link to comment
Share on other sites

Not sure if this is what you are looking for but I also have considered this.

 

Article on DBForums about mirroring/replication

 

I also read somewhere another guy asking about realtime redundancy of his MySQL database and the response was thus:

 

Add something like this to the mysqld section your my.ini file

 

[mysqld]

server-id=2

master-host=miniserver

master-user=account

master-password=apass

master-port=3306

report-host=hostname

 

 

Create an account on the master and put the user and password in the section above and the correct servername.

You will also have to enable binary logging in the masters my.ini

Link to comment
Share on other sites

I've been trying to get this working using the code below, but when the primary server goes down I get can't connect errors for both of them. I'm certain the login details for the other server are correct because they work fine from that site, but they don't seem to work from a different site. What did I miss?

 

Many thanks

 

  <?php
$username="";
$password="";
$database="";
$dberror="";

$link= mysql_connect('mysql2.freehostia.com',$username,$password);
mysql_select_db($database);

if (!$link) {
$username1="";
$password1="";
$database1="";
$dberror= "<b>WARNING</b>Our primary database is down. You are currently using our secondary database which may not be as up to date. We apologise for any inconvenience<br>";

$link1= mysql_connect('193.62.81.10',$username1,$password1);
mysql_select_db($database1) or die( "Unable to select database");
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.