ECSHOP读写分离配置与改造(转载)
2013-07-24 12:03:27   来源:http://www.cnblogs.com/flashicp/archive/2012/12/14/ECSHOP%E4%BC%98%E5%8C%96.html   评论:0 点击: 收藏

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

因为前几天群里一个哥们使用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]
复制代码
<?php

require(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);
    }
         
}
复制代码

相关热词搜索:ECSHOP 读写 分离

上一篇:ecshop后台商品-详细描述批量上传图片FCKeditor换成KindEditor
下一篇:大数据 phpcms V9 数据表分离的应用