首页 > 文章 > 杂文

ECSHOP读写分离配置与改造(转载)
时间:2013-07-24 12:03:27  点击: 来源:  作者:

因为前几天群里一个哥们使用ECSHOP遇到mysql访问瓶颈,简单看了下ECSHOP的主从分离 下面是转载的以下代码仅供学习参考,不成熟的地方,还需完善。

config.php<?php$db_name = "ecshop";$prefix = "ecs_";$t ...

因为前几天群里一个哥们使用ECSHOP遇到mysql访问瓶颈,简单看了下ECSHOP的主从分离 下面是转载的

以下代码仅供学习参考,不成熟的地方,还需完善。

 

 

config.php

复制代码
<?php$db_name   = "ecshop";$prefix    = "ecs_";$timezone    = "Europe/Berlin";$cookie_path    = "/";$cookie_domain    = "";$session = "1440";$_config = array();//数据库主服务器设置, 支持多组服务器设置, 当设置多组服务器时, 则会随机使用某个服务器$_config['master'][1]['dbhost'] = "192.168.2.175:3306";$_config['master'][1]['dbname'] = "ecshop";$_config['master'][1]['dbuser'] = "dragon";$_config['master'][1]['dbpw'] = "loong";/* *$_config['master'][2]['dbhost'] = ""; *... *///数据库从服务器设置( slave, 只读 ), 支持多组服务器设置, 当设置多组服务器时, 系统每次随机使用$_config['slave'][1]['dbhost'] = "192.168.2.176:3306";$_config['slave'][1]['dbname'] = "ecshop";$_config['slave'][1]['dbuser'] = "ivan";$_config['slave'][1]['dbpw'] = "loong";$_config['slave'][2]['dbhost'] = "192.168.2.177:3306";$_config['slave'][2]['dbname'] = "ecshop";$_config['slave'][2]['dbuser'] = "ivan";$_config['slave'][2]['dbpw'] = "loong";define('EC_CHARSET','utf-8');define('ADMIN_PATH','admin');define('AUTH_KEY', 'this is a key');define('OLD_AUTH_KEY', '');define('API_TIME', '');?>
复制代码

 


初始化数据连接类

复制代码
    /* 初始化数据库类       * 如果配置了从服务器,则初始化从库类     */      if(count($_config['slave'])) {          require(ROOT_PATH . 'includes/cls_mysql_slave.php');          $db = new cls_mysql_slave($_config);      }else{          require(ROOT_PATH . 'includes/cls_mysql.php');          $db = new cls_mysql($_config);      }  
复制代码

 增加cls_mysql_slave.php从库类

[php]
复制代码
<?phprequire(ROOT_PATH . 'includes/cls_mysql.php');class cls_mysql_slave extends cls_mysql{    var $slaveid = null;    function set_config($config){        if(!empty($this->config['slave'])) {            $this->slaveid = array_rand($this->config['slave']);        }        parent::set_config($config);    }    /* 随机分配从库连接 */    function set_slave_config() {                 $this->settings = $this->config['slave'][$this->slaveid];        $this->settings['charset'] = $this->config['charset'];        $this->settings['pconnect'] = $this->config['pconnect'];    }    function slave_connect() {        $this->set_slave_config();        $dbhost = $this->settings['dbhost'];        $dbuser = $this->settings['dbuser'];        $dbpw = $this->settings['dbpw'];        $dbname = $this->settings['dbname'];        $this->connect($dbhost, $dbuser, $dbpw, $dbname);            }    function query($sql, $type = '') {        // 如果执行查询操作,则执行从库连接        if($this->slaveid && strtoupper(substr($sql, 0 , 6)) == 'SELECT') {            $this->slave_connect();        }else{            parent::set_config($this->config);            $dbhost = $this->settings['dbhost'];            $dbuser = $this->settings['dbuser'];            $dbpw = $this->settings['dbpw'];            $dbname = $this->settings['dbname'];            $this->connect($dbhost, $dbuser, $dbpw, $dbname);        }        return parent::query($sql, $type);    }    /* 删除失败连接*/    function del_error_link(){        unset($this->config['slave'][$this->slaveid]);        $this->set_config($this->config);        $this->set_slave_config();        $dbhost = $this->settings['dbhost'];        $dbuser = $this->settings['dbuser'];        $dbpw = $this->settings['dbpw'];        $dbname = $this->settings['dbname'];        $this->connect($dbhost, $dbuser, $dbpw, $dbname);    }         }
复制代码
”您可通过以下微信二维码,赞赏作者“
 
 
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
推荐资讯
windows如何挂载webdav?
windows如何挂载webda
linux服务器同步互联网时间
linux服务器同步互联
亚马逊云lightsail服务器使用教程 | 亚马逊云lightsail使用指南
亚马逊云lightsail服
hostdare 4折优惠,直接降价60% 美国洛杉矶低至 $10.4/年 768M内存/1核/10gNVMe/500g流量
hostdare 4折优惠,直接
相关文章
栏目更新
栏目热门

关于我们 | 广告服务 | 联系我们 | 网站地图 | 免责声明 | WAP | RSS


Copyright © 运维之家 2013-2024