(Yii)实现MySQL多库和读写分离

这篇博客介绍了如何在Yii框架下通过事件驱动模式实现MySQL数据库的读写分离。作者详细展示了如何重写CActiveRecord的getDbConnection方法,以及如何配置数据库连接,包括主从服务器的设置和在beforeSave、beforeDelete、beforeFind事件中切换读写服务器。
摘要由CSDN通过智能技术生成
本文所讲的就是MySQL分库和主从读写分离在Yii的配置和使用。

Yii默认是不支持读写分离的,我们可以利用Yii的事件驱动模式来实现MySQL的读写分离。

Yii提供了一个强大的CActiveRecord数据库操作类,通过重写getDbConnection方法来实现数据库的切换,然后通过事件 beforeSave、beforeDelete、beforeFind 来实现读写服务器的切换,还需要两个配置文件dbconfig和modelconfig分别配置数据库主从服务器和model所对应的数据库名称,附代码

DBConfig.php

  1. <?php  
  2. return array(  
  3. ’passport’ => array(  
  4. ’write’ => array(  
  5. class’ => ‘CDbConnection’,  
  6. ’connectionString’ => ‘mysql:host=10.1.39.2;dbname=db1′,  
  7. ’emulatePrepare’ => true,  
  8. //’enableParamLogging’ => true,  
  9. ’enableProfiling’ => true,  
  10. ’username’ => ‘root’,  
  11. ’password’ => ”,  
  12. ’charset’ => ‘utf8′,  
  13. ‘schemaCachingDuration’=>3600,  
  14. ),  
  15. ’read’ => array(  
  16. array(  
  17. class’ => ‘CDbConnection’,  
  18. ’connectionString’ => ‘mysql:host=10.1.39.3;dbname=db1,  
  19. ’emulatePrepare’ => true,  
  20. //’enableParamLogging’ => true,  
  21. ’enableProfiling’ => true,  
  22. ’username’ => ‘root’,  
  23. ’password’ => ”,  
  24. ’charset’ => ‘utf8′,  
  25. ‘schemaCachingDuration’=>3600,  
  26. ),  
  27. array(  
  28. class’ => ‘CDbConnection’,  
  29. ’connectionString’ => ‘mysql:host=10.1.39.4;dbname=db3′,  
  30. ’emulatePrepare’ => true,  
  31. //’enableParamLogging’ => true,  
  32. ’enableProfiling’ => true,  
  33. ’username’ => ‘root’,  
  34. ’password’ => ”,  
  35. ’charset’ => ‘utf8′,  
  36. ‘schemaCachingDuration’=>3600,  
  37. ),  
  38. ),  
  39. ),  
  40. );  
  1. <?php  
  2. return array(  
  3. ’passport’ => array(  
  4. ’write’ => array(  
  5. class’ => ‘CDbConnection’,  
  6. ’connectionString’ => ‘mysql:host=10.1.39.2;dbname=db1′,  
  7. ’emulatePrepare’ => true,  
  8. //’enableParamLogging’ => true,  
  9. ’enableProfiling’ => true,  
  10. ’username’ => ‘root’,  
  11. ’password’ => ”,  
  12. ’charset’ => ‘utf8′,  
  13. ‘schemaCachingDuration’=>3600,  
  14. ),  
  15. ’read’ => array(  
  16. array(  
  17. class’ => ‘CDbConnection’,  
  18. ’connectionString’ => ‘mysql:host=10.1.39.3;dbname=db1,  
  19. ’emulatePrepare’ => true,  
  20. //’enableParamLogging’ => true,  
  21. ’enableProfiling’ => true,  
  22. ’username’ => ‘root’,  
  23. ’password’ => ”,  
  24. ’charset’ => ‘utf8′,  
  25. ‘schemaCachingDuration’=>3600,  
  26. ),  
  27. array(  
  28. class’ => ‘CDbConnection’,  
  29. ’connectionString’ => ‘mysql:host=10.1.39.4;dbname=db3′,  
  30. ’emulatePrepare’ => true,  
  31. //’enableParamLogging’ => true,  
  32. ’enableProfiling’ => true,  
  33. ’username’ => ‘root’,  
  34. ’password’ => ”,  
  35. ’charset’ => ‘utf8′,  
  36. ‘schemaCachingDuration’=>3600,  
  37. ),  
  38. ),  
  39. ),  
  40. );  

ModelConfig.php


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值