Created
September 16, 2013 13:21
-
-
Save nntsugu/6580612 to your computer and use it in GitHub Desktop.
MySQL Master-Slave(複数) 構成への接続設定。 ref: http://qiita.com/nntsugu@github/items/b6e5b7951ede5664578d
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Base extends Phalcon\Mvc\Model | |
{ | |
public function initialize() | |
{ | |
$this->setWriteConnectionService('dbMaster'); | |
$this->setReadConnectionService('dbSlave'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
(中略) | |
// masterの設定だよ | |
$settings = array_merge( | |
$settings, | |
array( | |
'mysql_master' => array( | |
'adapter' => 'Mysql', | |
'host' => '[hostname(master)]:[port]', // ex localhost:3306 | |
'username' => 'user', | |
'password' => 'pass', | |
'dbname' => 'database', | |
), | |
) | |
); | |
// slave x3の設定だよ | |
$slave1 = '[hostname(slave1)]:[port]'; // ex localhost:3316 | |
$slave2 = '[hostname(slave2)]:[port]'; // ex localhost:3326 | |
$slave3 = '[hostname(slave3)]:[port]'; // ex localhost:3336 | |
$mysqlSlaves = array( | |
'mysql_slave1' => array( | |
'adapter' => 'Mysql', | |
'host' => $slave1, | |
'username' => 'user', | |
'password' => 'pass', | |
'dbname' => 'database', | |
), | |
'mysql_slave2' => array( | |
'adapter' => 'Mysql', | |
'host' => $slave2, | |
'username' => 'user', | |
'password' => 'pass', | |
'dbname' => 'database', | |
), | |
'mysql_slave3' => array( | |
'adapter' => 'Mysql', | |
'host' => $slave3, | |
'username' => 'user', | |
'password' => 'pass', | |
'dbname' => 'database', | |
), | |
); | |
// ちょっと回りくどい書き方しているのはここでslaveの台数をカウントしたいから。 | |
$settings = array_merge( | |
$settings, | |
$mysqlSlaves, | |
array('mysql_slave_count' => count($mysqlSlaves)) | |
); | |
(以下略) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$this->setWriteConnectionService('dbMaster'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$this->setReadConnectionService('dbSlave'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$robots = new Robots(); | |
$robots->find(); // dbSlaveへ接続 | |
(略) | |
$robots->save(); // dbMasterへ接続 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Robots extends Base | |
{ | |
(略) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
(中略) | |
// MySQL master | |
$di->set('dbMaster', function () use ($config) { | |
return new DbAdapter(array( | |
'host' => $config->mysql_master->host, | |
'username' => $config->mysql_master->username, | |
'password' => $config->mysql_master->password, | |
'dbname' => $config->mysql_master->dbname | |
)); | |
}); | |
// MySQL slave | |
// slaveの中から1台ランダムに選択。randの上限指定にさっきのslave台数を使ってます。 | |
$di->set('dbSlave', function () use ($config) { | |
$slaveName = 'mysql_slave' . (String)rand(1, $config->mysql_slave_count); | |
return new DbAdapter(array( | |
'host' => $config[$slaveName]['host'], | |
'username' => $config[$slaveName]['username'], | |
'password' => $config[$slaveName]['password'], | |
'dbname' => $config[$slaveName]['dbname'] | |
)); | |
}); | |
(以下略) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/app | |
/config | |
config.php | |
service.php | |
/models | |
Base.php // 名前は好きにしてOK |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment