0

Question: How can I put 2 or more database connection in one PHP file with a case that one or more servers are not connecting to the network?

The Story Line Sorry if this question is already existed, but I already searched it and still I don't quite understand..

so, I'm trying to make a simple distributed database system using MySQL, and I'm not using any complicated stuffs since I'm a complete beginner... I use a standard connection on MySQL (mysql_connect)...in I put it as an included file... because it's a distributed database, in which in one website there'll be more than one connection attemp to make, if one of the connection failed...

like this, I made two conn file..it's just examples though...

<?php
$server="192.168.0.3";
$user="user";
$pass="pass";
$db="db_1";

$conn = mysql_connect($server,$user,$pass);
if(!$conn) die "connection error";
$sel_db = mysql_select_db($db,$conn);
if(!sel_db) die "wrong db";
?>

so I made the second one with db_2 as the database... and I put the on the website using if..

<?php
require_once "conn_1.php";
if (empty($conn)) {
   require_once "conn_2.php";
}
?>

and I intentionally close/cut the connection on the first server...so if the first connect attemp failed, it will goes to the second one.. but it ended up showing error "trying to connect to a unexisted host", well I forgot about the detail but something like that...and didn't show anything else..

so I tried to close the second server... I thought the result will be different, it has to be showing a normal result since the first connection didn't fail...but the result is, the website still showed a same warning error..

so I just thought that instead of using those connection files, I have to put an IP tracking code to check the other server is online or not, and then called the connection file...

or is it another way to do it...?

Sorry if you don't understand my question, please tell me if my question is a bit confusing.. and thanks..

  • MySQL isn't a distributed database. It's hard to tell what you're asking about. Maybe replication.. maybe connection-pooling. What's the actual problem you're trying to solve? – Mike B Jun 04 '13 at 18:02
  • Well if you have `if(!$conn) die "connection error";` in your header files your attempt to connect to the second one will never be reached as you `die` before it can be reached. the die should be after both tries – Patrick Evans Jun 04 '13 at 18:04
  • @MikeB i think he means a redundant mysql server – Patrick Evans Jun 04 '13 at 18:05
  • @PatrickEvans Then he wants replication: http://stackoverflow.com/questions/189903/scaling-solutions-for-mysql-replication-clustering – Mike B Jun 04 '13 at 18:06

1 Answers1

0

Why are your servers down ? Ideally if you have multiple MySQL databases you will have multiple database servers with some sort of synchronize between the databases.

Each webserver should have 1 database server. and then the datbase servers should all be updated by 1 master database server.

Answering your question more directly

$conn = mysql_connect($server,$user,$pass);
if(!$conn) {

// NOW try and connect to somewhere else
$conn = mysql_connect($server2,$user,$pass);
}

Also dont use mysql_* functions any more - look into mysqli_ (http://php.net/manual/en/book.mysqli.php) or pdo (http://php.net/manual/en/book.pdo.php)

exussum
  • 18,275
  • 8
  • 32
  • 65