1.配置文件设置

  1. $db_name = "ecshop";
  2.  
  3. $prefix = "ecs_";
  4.  
  5. $timezone = "Europe/Berlin";
  6.  
  7. $cookie_path = "/";
  8.  
  9. $cookie_domain = "";
  10.  
  11. $session = "1440";
  12.  
  13. $_config = array();
  14.  
  15. //数据库主服务器设置, 支持多组服务器设置, 当设置多组服务器时, 则会随机使用某个服务器
  16. $_config['master'][1]['dbhost'] = "192.168.2.175:3306";
  17. $_config['master'][1]['dbname'] = "ecshop";
  18. $_config['master'][1]['dbuser'] = "dragon";
  19. $_config['master'][1]['dbpw'] = "loong";
  20.  
  21. /*
  22. *$_config['master'][2]['dbhost'] = "";
  23. *...
  24. */
  25.  
  26. //数据库从服务器设置( slave, 只读 ), 支持多组服务器设置, 当设置多组服务器时, 系统每次随机使用
  27. $_config['slave'][1]['dbhost'] = "192.168.2.176:3306";
  28. $_config['slave'][1]['dbname'] = "ecshop";
  29. $_config['slave'][1]['dbuser'] = "ivan";
  30. $_config['slave'][1]['dbpw'] = "loong";
  31.  
  32. $_config['slave'][2]['dbhost'] = "192.168.2.177:3306";
  33. $_config['slave'][2]['dbname'] = "ecshop";
  34. $_config['slave'][2]['dbuser'] = "ivan";
  35. $_config['slave'][2]['dbpw'] = "loong";
  36.  
  37. define('EC_CHARSET','utf-8');
  38.  
  39. define('ADMIN_PATH','admin');
  40.  
  41. define('AUTH_KEY', 'this is a key');
  42.  
  43. define('OLD_AUTH_KEY', '');
  44.  
  45. define('API_TIME', '');

2.初始化数据连接类 init.php

  1. /* 如果配置了从服务器,则初始化从库类 */
  2. if(count($_config['slave'])) {
  3. require(ROOT_PATH . 'includes/cls_mysql_slave.php');
  4. $db = new cls_mysql_slave($_config);
  5. }else{
  6. require(ROOT_PATH . 'includes/cls_mysql.php');
  7. $db = new cls_mysql($_config);
  8. }

3.编写从库类文件

  1. require(ROOT_PATH . 'includes/cls_mysql.php');
  2. class cls_mysql_slave extends cls_mysql
  3. {
  4. var $slaveid = null;
  5. function set_config($config){
  6. if(!empty($this->config['slave'])) {
  7. $this->slaveid = array_rand($this->config['slave']);
  8. }
  9. parent::set_config($config);
  10. }
  11. /* 随机分配从库连接 */
  12. function set_slave_config() {
  13. $this->settings = $this->config['slave'][$this->slaveid];
  14. $this->settings['charset'] = $this->config['charset'];
  15. $this->settings['pconnect'] = $this->config['pconnect'];
  16. }
  17. function slave_connect() {
  18. $this->set_slave_config();
  19. $dbhost = $this->settings['dbhost'];
  20. $dbuser = $this->settings['dbuser'];
  21. $dbpw = $this->settings['dbpw'];
  22. $dbname = $this->settings['dbname'];
  23. $this->connect($dbhost, $dbuser, $dbpw, $dbname);
  24.  
  25. }
  26. function query($sql, $type = '') {
  27. // 如果执行查询操作,则执行从库连接
  28. if($this->slaveid && strtoupper(substr($sql, 0 , 6)) == 'SELECT') {
  29. $this->slave_connect();
  30. }else{
  31. parent::set_config($this->config);
  32. $dbhost = $this->settings['dbhost'];
  33. $dbuser = $this->settings['dbuser'];
  34. $dbpw = $this->settings['dbpw'];
  35. $dbname = $this->settings['dbname'];
  36. $this->connect($dbhost, $dbuser, $dbpw, $dbname);
  37. }
  38. return parent::query($sql, $type);
  39. }
  40. /* 删除失败连接*/
  41. function del_error_link(){
  42. unset($this->config['slave'][$this->slaveid]);
  43. $this->set_config($this->config);
  44. $this->set_slave_config();
  45. $dbhost = $this->settings['dbhost'];
  46. $dbuser = $this->settings['dbuser'];
  47. $dbpw = $this->settings['dbpw'];
  48. $dbname = $this->settings['dbname'];
  49. $this->connect($dbhost, $dbuser, $dbpw, $dbname);
  50. }
  51.  
  52. }

4.cls_mysql类库修改

  1. <?php
  2. if (!defined('IN_ECS'))
  3. {
  4. die('Hacking attempt');
  5. }
  6. class cls_mysql
  7. {
  8. var $link_id = NULL;
  9. var $settings = array();
  10. var $queryCount = 0;
  11. var $linkCount = 0;
  12. var $queryTime = '';
  13. var $queryLog = array();
  14. var $max_cache_time = 300; // 最大的缓存时间,以秒为单位
  15. var $cache_data_dir = 'temp/query_caches/';
  16. var $root_path = '';
  17. var $error_message = array();
  18. var $platform = '';
  19. var $version = '';
  20. var $dbhash = '';
  21. var $starttime = 0;
  22. var $timeline = 0;
  23. var $timezone = 0;
  24. var $mysql_config_cache_file_time = 0;
  25. var $mysql_disable_cache_tables = array(); // 不允许被缓存的表,遇到将不会进行缓存
  26. var $config = array();
  27. function __construct($config, $charset = 'utf8', $pconnect = 0, $quiet = 0)
  28. {
  29. $this->cls_mysql($config, $charset, $pconnect, $quiet);
  30. }
  31. function cls_mysql($config, $charset = 'utf8', $pconnect = 0, $quiet = 0)
  32. {
  33. if(!empty($config)) {
  34. $config['charset'] = $charset;
  35. $config['pconnect'] = $pconnect;
  36. $this->config = $config;
  37. }
  38.  
  39. if (defined('EC_CHARSET'))
  40. {
  41. $charset = strtolower(str_replace('-', '', EC_CHARSET));
  42. }
  43. if (defined('ROOT_PATH') && !$this->root_path)
  44. {
  45. $this->root_path = ROOT_PATH;
  46. }
  47. $this->set_config($this->config);
  48. if ($quiet)
  49. {
  50. $dbhost = $this->settings['dbhost'];
  51. $dbuser = $this->settings['dbuser'];
  52. $dbpw = $this->settings['dbpw'];
  53. $dbname = $this->settings['dbname'];
  54. $this->connect($dbhost, $dbuser, $dbpw, $dbname, $charset, $pconnect, $quiet);
  55. }
  56. }
  57. //随机分配数据库连接
  58. function set_config($config) {
  59. $sid = array_rand($config['master']);
  60. $settings = $config['master'][$sid];
  61. $settings['sid'] = $sid;
  62. $settings['charset'] = $this->config['charset'];
  63. $settings['pconnect'] = $this->config['pconnect'];
  64. $this->settings = $settings;
  65. }
  66. function connect($dbhost, $dbuser, $dbpw, $dbname = '', $charset = 'utf8', $pconnect = 0, $quiet = 0)
  67. {
  68. if ($pconnect)
  69. {
  70. if (!($this->link_id = @mysql_pconnect($dbhost, $dbuser, $dbpw)))
  71. {
  72. if (!$quiet)
  73. {
  74. $this->ErrorMsg("Can't pConnect MySQL Server!");
  75. }
  76. return false;
  77. }
  78. }
  79. else
  80. {
  81. if (PHP_VERSION >= '4.2')
  82. {
  83. $this->link_id = @mysql_connect($dbhost, $dbuser, $dbpw, true);
  84. }
  85. else
  86. {
  87. $this->link_id = @mysql_connect($dbhost, $dbuser, $dbpw);
  88. mt_srand((double)microtime() * 1000000); // 对 PHP 4.2 以下的版本进行随机数函数的初始化工作
  89. }
  90. if (!$this->link_id)
  91. {
  92. if (!$quiet)
  93. {
  94. //连接超过10次,中断连接,抛出错误
  95. if($this->linkCount>9){
  96. $this->ErrorMsg("Can't Connect MySQL Server!");
  97. }
  98. $this->linkCount++;
  99. $this->del_error_link();
  100. }
  101. return false;
  102. }
  103. }
  104. $this->dbhash = md5($this->root_path . $dbhost . $dbuser . $dbpw . $dbname);
  105. $this->version = mysql_get_server_info($this->link_id);
  106. /* 如果mysql 版本是 4.1+ 以上,需要对字符集进行初始化 */
  107. if ($this->version > '4.1')
  108. {
  109. if ($charset != 'latin1')
  110. {
  111. mysql_query("SET character_set_connection=$charset, character_set_results=$charset, character_set_client=binary", $this->link_id);
  112. }
  113. if ($this->version > '5.0.1')
  114. {
  115. mysql_query("SET sql_mode=''", $this->link_id);
  116. }
  117. }
  118. $sqlcache_config_file = $this->root_path . $this->cache_data_dir . 'sqlcache_config_file_' . $this->dbhash . '.php';
  119. @include($sqlcache_config_file);
  120. $this->starttime = time();
  121. if ($this->max_cache_time && $this->starttime > $this->mysql_config_cache_file_time + $this->max_cache_time)
  122. {
  123. if ($dbhost != '.')
  124. {
  125. $result = mysql_query("SHOW VARIABLES LIKE 'basedir'", $this->link_id);
  126. $row = mysql_fetch_assoc($result);
  127. if (!empty($row['Value']{1}) && $row['Value']{1} == ':' && !empty($row['Value']{2}) && $row['Value']{2} == "\\")
  128. {
  129. $this->platform = 'WINDOWS';
  130. }
  131. else
  132. {
  133. $this->platform = 'OTHER';
  134. }
  135. }
  136. else
  137. {
  138. $this->platform = 'WINDOWS';
  139. }
  140. if ($this->platform == 'OTHER' &&
  141. ($dbhost != '.' && strtolower($dbhost) != 'localhost:3306' && $dbhost != '127.0.0.1:3306') ||
  142. (PHP_VERSION >= '5.1' && date_default_timezone_get() == 'UTC'))
  143. {
  144. $result = mysql_query("SELECT UNIX_TIMESTAMP() AS timeline, UNIX_TIMESTAMP('" . date('Y-m-d H:i:s', $this->starttime) . "') AS timezone", $this->link_id);
  145. $row = mysql_fetch_assoc($result);
  146. if ($dbhost != '.' && strtolower($dbhost) != 'localhost:3306' && $dbhost != '127.0.0.1:3306')
  147. {
  148. $this->timeline = $this->starttime - $row['timeline'];
  149. }
  150. if (PHP_VERSION >= '5.1' && date_default_timezone_get() == 'UTC')
  151. {
  152. $this->timezone = $this->starttime - $row['timezone'];
  153. }
  154. }
  155. $content = '<' . "?php\r\n" .
  156. '$this->mysql_config_cache_file_time = ' . $this->starttime . ";\r\n" .
  157. '$this->timeline = ' . $this->timeline . ";\r\n" .
  158. '$this->timezone = ' . $this->timezone . ";\r\n" .
  159. '$this->platform = ' . "'" . $this->platform . "';\r\n?" . '>';
  160. @file_put_contents($sqlcache_config_file, $content);
  161. }
  162. /* 选择数据库 */
  163. if ($dbname)
  164. {
  165. if (mysql_select_db($dbname, $this->link_id) === false )
  166. {
  167. if (!$quiet)
  168. {
  169. $this->ErrorMsg("Can't select MySQL database!");
  170. }
  171. return false;
  172. }
  173. else
  174. {
  175. return true;
  176. }
  177. }
  178. else
  179. {
  180. return true;
  181. }
  182. }
  183. ......
  184. /* 删除失败连接*/
  185. function del_error_link(){
  186. unset($this->config['master'][$this->settings['sid']]);
  187. $this->set_config($this->config);
  188. $dbhost = $this->settings['dbhost'];
  189. $dbuser = $this->settings['dbuser'];
  190. $dbpw = $this->settings['dbpw'];
  191. $dbname = $this->settings['dbname'];
  192. $this->connect($dbhost, $dbuser, $dbpw, $dbname);
  193. }
  194. }

ecshop读写分离的更多相关文章

  1. ecshop改造读写分离配置与改造

    前两天配置好了mysql主从方式,今天就拿ecshop练习读写分离.以下代码仅供学习参考,不成熟的地方,还需完善. <?php $db_name = "ecshop"; $p ...

  2. ecshop改造读写分离

    前两天配置好了mysql主从方式,今天就拿ecshop练习读写分离.以下代码仅供学习参考,不成熟的地方,还需完善. config.php <?php $db_name = "ecsho ...

  3. mybatis plugins实现项目【全局】读写分离

    在之前的文章中讲述过数据库主从同步和通过注解来为部分方法切换数据源实现读写分离 注解实现读写分离: http://www.cnblogs.com/xiaochangwei/p/4961807.html ...

  4. Spring aop应用之实现数据库读写分离

    Spring加Mybatis实现MySQL数据库主从读写分离 ,实现的原理是配置了多套数据源,相应的sqlsessionfactory,transactionmanager和事务代理各配置了一套,如果 ...

  5. MySQL+Amoeba实现数据库主从复制和读写分离

    MySQL读写分离是在主从复制的基础上进一步通过在master上执行写操作,在slave上执行读操作来实现的.通过主从复制,master上的数据改动能够同步到slave上,从而保持了数据的一致性.实现 ...

  6. J2EE 项目读写分离

    先回答下 1.为啥要读写分离? 大家都知道最初开始,一个项目对应一个数据库,基本是一对一的,但是由于后来用户及数据还有访问的急剧增多, 系统在数据的读写上出现了瓶颈,为了让提高效率,想读和写不相互影响 ...

  7. mysql+mycat搭建稳定高可用集群,负载均衡,主备复制,读写分离

    数据库性能优化普遍采用集群方式,oracle集群软硬件投入昂贵,今天花了一天时间搭建基于mysql的集群环境. 主要思路 简单说,实现mysql主备复制-->利用mycat实现负载均衡. 比较了 ...

  8. Spring 实现数据库读写分离

    随着互联网的大型网站系统访问量的增高,数据库访问压力方面不断的显现而出,所以许多公司在数据库层面采用读写分离技术,也就是一个master,多个slave.master负责数据的实时更新或实时查询,而s ...

  9. Nginx 反向代理、负载均衡、页面缓存、URL重写及读写分离详解

    转载:http://freeloda.blog.51cto.com/2033581/1288553 大纲 一.前言 二.环境准备 三.安装与配置Nginx 四.Nginx之反向代理 五.Nginx之负 ...

随机推荐

  1. 【英语】Bingo口语笔记(19) - 如何用英语叙旧

  2. 【Python】类和对象、继承、使用文件、存储、异常、标准库(不懂)

    当你调用这个对象的方法MyObject.method(arg1, arg2)的时候,这会由Python自动转为MyClass.method(MyObject, arg1, arg2)——这就是self ...

  3. ORACLE RAC 下非缺省端口监听配置(listener.ora tnsnames.ora)

    不论是单实例还是RAC,对于非缺省端口下(1521)的监听器,pmon进程不会将service/instance注册到监听器,即不会实现动态注册.与单实例相同,RAC非缺省端口的监听器也是通过设置参数 ...

  4. MVC中如何跳过对模型中某个属性的验证

    [HttpPost] public ActionResult Create(Users user) { ModelState.Remove(“Password”); //加上这句就行了 if (Mod ...

  5. Shell教程4-Shell替换

    如果表达式中包含特殊字符,Shell 将会进行替换.例如,在双引号中使用变量就是一种替换,转义字符也是一种替换. 举个例子: 复制纯文本新窗口   #!/bin/bash a=10 echo -e & ...

  6. 使用Nodejs+mongodb开发地图瓦片服务器

    原先地图瓦片服务器采用的是arcgisserver发布的地图服务并进行切片,但ags发布的地图服务很占内存,发布太多的话服务器压力很大.再一个就是ags价太高了. 学习Nodejs之后,发现这是一个可 ...

  7. 为什么从PhoneGap中逃离

    我是一名移动应用的开发者,从JAVA 为主的Android到以Objective-C为主的iOS最后到以HTML5为主的跨平台开发,我已经走过了五年多的时光,而我也从一个底层的码农成长为项目负责人. ...

  8. c++ 一个类使用另外一个类的变量或方法

    如:a.cpp 声明 int a=9; 要在b.cpp文件中使用变量 a extern int a; int b=1; cout<<a+b; 结果为10;

  9. N人报数第M人出列游戏问题(约瑟夫问题)

    这是一道华为的机试题,后来才知道也叫约瑟夫问题,题目是这样的:有n个人围成一圈,玩一个游戏,规则为将该n个人编号为1,2,......n, 从编号为1的人开始依次循环报数,报道第m的时候将第m个人从队 ...

  10. 8、NFC技术:让Android自动打开网页

    创建封装Uri的NdefRecord  public  NdefRecord  createUri(String  uriString);  public  NdefRecord  cre ...