Does PHP have a built-in mechanism for switching to another database server?

I found this:

http://www.evolt.org/failover-database-connection-with-php-mysql

and similar examples. But is there a better way?

I think the lines of the Automatic Failover Client in my own MS SQL client.

+5
source share
4 answers

This is traditional for handling failover strategies at the system level; in this way, all applications can have a reliable environment.

MySQL failover strategy MySQL. MySQL, , .

, .

+8

PHP. .

, .

, :

interface MySQL_Interface {
    public function query($sql);
}

class MySQL_Concrete implements MySQL_Interface {
    public function __construct($host, $user, $pass, $dbname) {
        $this->_mysql = mysql_connect($host, $user, $pass) or throw Exception("Could not connect to server");
        mysql_select_db($db, $this->_mysql) or throw Exception("Could not connect to database");
    }
    public function query($sql) {
        return mysql_query($sql) or throw new Exception("Query failed");
    }
}

class MySQL_Failover implements MySQL_Interface {
    public function __construct(MySQL_Interface $one, MySQL_Interface $two) {
        $this->_one = $one;
        $this->_two = $two;
    }
    public function query($sql) {
        try {
            return $this->_one->query($sql);
        } catch (Exception $e) {
            return $this->_two->query($sql);
        }
    }
}

$db = new MySQL_Failover(
    new MySQL_Concrete('host1', 'user', 'pass', 'db'),
    new MySQL_Concrete('host2', 'user', 'pass', 'db')
);

$db->query('SELECT * FROM Users');

PS: , .

PPS: (Zend_Db, MDB2, ect) ad-hoc-

+3

, . , - MySQL Proxy. , (if (fails) { connect to another }) , .

+2

php - , . .

.

+2

All Articles