php - Laravel 4 - mirror database? -
we have created mirror database replica of main database of our application. goal jump between 2 databases in case of unavailability.
i wondering how can implement such thing in laravel 4 ? lets have
'mysql' => array( 'driver' => 'mysql', 'host' => '172.22.22.22', 'port' => '3306', 'database' => 'db', 'username' => 'username', 'password' => 'password', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', ),
and
'mysql' => array( 'driver' => 'mysql', 'host' => '172.22.22.23', 'port' => '3306', 'database' => 'db', 'username' => 'username', 'password' => 'password', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', ),
basically different host same credentials. how can config switching between databases in case 1 of them unavailable? source or doc appreciated.
you can write helper function sets connection depending on connectivity or not. way would this:
config file:
# our primary database connection 'mysql' => array( 'driver' => 'mysql', 'host' => 'host1', 'database' => 'database1', 'username' => 'user1', 'password' => 'pass1' 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', ), # our secondary database connection 'mysql2' => array( 'driver' => 'mysql', 'host' => 'host2', 'database' => 'database2', 'username' => 'user2', 'password' => 'pass2' 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', ),
helper function file:
function db($modelname){ try { //get underlaying pdo connection. db::connection()->getpdo(); $m = new $modelname; $m->setconnection('mysql'); } catch(\exception $e) { $m = new $modelname; $m->setconnection('mysql2'); } }
and in every model file set this:
protected $connection = helperclass::db('mymodelname');
i presume kind of approach work.
Comments
Post a Comment