CakePHP --- Load Balancing and MySQL Master and Slaves
2011-09-15 22:55
1256 查看
http://bakery.cakephp.org/articles/eagerterrier/2007/05/26/load-balancing-and-mysql-master-and-slaves-2
If you are currently using MySql master/slave replication for load balancing and wish to transport to cakePHP, it really couldn't be easier.
2 simple steps to master/slave replication in cake..
Firstly in app/config/database.php write the following:-
Then, in app/app_model.php, create 4 new methods:
It really is as simple as that.
However, there are some instance where you may want to use a query in your controller where you update/insert information. In this case, use the following:
I use this where I want information to be updated, but don't want cake to flush the cache. But then again, that may be like crossing the streams in Ghostbusters, so don't listen to me..
With thanks to Nate and gwoo
http://www.howtoforge.com/mysql_master_master_replication
If you are currently using MySql master/slave replication for load balancing and wish to transport to cakePHP, it really couldn't be easier.
2 simple steps to master/slave replication in cake..
Firstly in app/config/database.php write the following:-
class DATABASE_CONFIG { var $default = array( 'driver' => 'mysql', 'host' => 'slave.host.ip', 'login' => '....', 'password' => '.....', 'database' => 'my_db' ); var $master = array( 'driver' => 'mysql', 'host' => 'master.host.ip', 'login' => '....', 'password' => '.....', 'database' => 'my_db' ); }
Then, in app/app_model.php, create 4 new methods:
function beforeSave() { $this->useDbConfig = 'master'; return true; } function afterSave() { $this->useDbConfig = 'default'; return true; } function beforeDelete() { $this->useDbConfig = 'master'; return true; } function afterDelete() { $this->useDbConfig = 'default'; return true; }
It really is as simple as that.
However, there are some instance where you may want to use a query in your controller where you update/insert information. In this case, use the following:
$this->ModelName->setDataSource('master'); $this->ModelName->query("UPDATE post_views SET views=views+1 WHERE id=1234"); $this->ModelName->setDataSource('default');
I use this where I want information to be updated, but don't want cake to flush the cache. But then again, that may be like crossing the streams in Ghostbusters, so don't listen to me..
With thanks to Nate and gwoo
http://www.howtoforge.com/mysql_master_master_replication
相关文章推荐
- CakePHP: tips on ssl, mysql load balance, master/slave, xhprof benchmark tool
- Mysql 报错:master and slave have equal MySQL server UUIDs
- 10g & 11g Configuration of TAF(Transparent Application Failover) and Load Balancing [ID 453293.1]
- All about TAF - 10g&11g :Configuration of TAF(Transparent Application Failover) and Load Balancing
- Free and Open Source Load-Balancing Software and Projects--转
- mysql 主从配置以及 slave I/O 启动失败解决 Fatal error: The slave I/O thread stops because master and slave have
- 10g & 11g Configuration of TAF(Transparent Application Failover) and Load Balancing [ID 453293.1]
- 17.1.1.7 Setting Up Replication with New Master and Slaves 设置复制使用新的master和slaves:
- 10g & 11g Configuration of TAF(Transparent Application Failover) and Load Balancing [ID 453293.1]
- 【架构】How To Use HAProxy to Set Up MySQL Load Balancing
- 金融信息系统中Data Integrity、Support HA and Load Balancing及Multi-locations的技术实现举例
- 17.1.1.7 Setting Up Replication with New Master and Slaves 设置复制使用新的master和slaves:
- 10g & 11g Configuration of TAF(Transparent Application Failover) and Load Balancing [ID 453293.1]
- mysql OUTFILE and LOAD DATA
- linux配置mysql主从--master and slave
- How to Configure Tomcat/JBoss and Apache HTTPD for Load Balancing and Failover
- How to configure Apache and Tomcat to realize the load balancing?
- MySQL.master and slave 一主多从,切库传输
- MySQL CSV file ( load and export ) 笔记
- MySQL复制错误 The slave I/O thread stopsbecause master and slave have equal MySQL server UUIDs; these UUIDs must bedifferent for replication to work 解析