ecshop改造读写分离

转自http://blog.csdn.net/very_loong/article/details/7999895

config.php

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

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

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

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


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值