matthew_ellis24 Posted November 23, 2007 Share Posted November 23, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/78574-use-alternate-database-if-one-connect-fails/ Share on other sites More sharing options...
NiteCloak Posted November 23, 2007 Share Posted November 23, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/78574-use-alternate-database-if-one-connect-fails/#findComment-397584 Share on other sites More sharing options...
revraz Posted November 23, 2007 Share Posted November 23, 2007 I know you posted this in the MySQL forum, but if you are using a language like PHP, there should be no reason why you can't use another DB if your connect fails for one. Quote Link to comment https://forums.phpfreaks.com/topic/78574-use-alternate-database-if-one-connect-fails/#findComment-397613 Share on other sites More sharing options...
matthew_ellis24 Posted November 24, 2007 Author Share Posted November 24, 2007 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"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/78574-use-alternate-database-if-one-connect-fails/#findComment-398051 Share on other sites More sharing options...
matthew_ellis24 Posted November 24, 2007 Author Share Posted November 24, 2007 Obviously when I said "but when the primary server goes down I get can't connect errors for both of them" I meant I get connect errors from both and can't connect to either. Sorry, couldn't see how to edit my original post! Quote Link to comment https://forums.phpfreaks.com/topic/78574-use-alternate-database-if-one-connect-fails/#findComment-398215 Share on other sites More sharing options...
fenway Posted November 24, 2007 Share Posted November 24, 2007 Sounds like you might actually be on a different box/ip... Quote Link to comment https://forums.phpfreaks.com/topic/78574-use-alternate-database-if-one-connect-fails/#findComment-398244 Share on other sites More sharing options...
matthew_ellis24 Posted November 29, 2007 Author Share Posted November 29, 2007 the secondary database is on a different host to the website - is that a problem? Quote Link to comment https://forums.phpfreaks.com/topic/78574-use-alternate-database-if-one-connect-fails/#findComment-401988 Share on other sites More sharing options...
fenway Posted November 29, 2007 Share Posted November 29, 2007 the secondary database is on a different host to the website - is that a problem? Not a problem per se, but it's likely that it's been ste up to only allow localhost connections, either in the IP tables and/or in the mysql grant tables. Quote Link to comment https://forums.phpfreaks.com/topic/78574-use-alternate-database-if-one-connect-fails/#findComment-402062 Share on other sites More sharing options...
matthew_ellis24 Posted November 29, 2007 Author Share Posted November 29, 2007 I have access to these, but don't know which to change. Can you give me a push in the right direction? is that in the config.inc file? Quote Link to comment https://forums.phpfreaks.com/topic/78574-use-alternate-database-if-one-connect-fails/#findComment-402077 Share on other sites More sharing options...
fenway Posted November 29, 2007 Share Posted November 29, 2007 Check the grant table first... that's more likely to be the issue, and it's easier to fix -- your user probably doesn't have remote access. Quote Link to comment https://forums.phpfreaks.com/topic/78574-use-alternate-database-if-one-connect-fails/#findComment-402108 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.