您的位置:首页 > 运维架构

ECSHOP读写分离配置与改造(转载)

2012-12-14 09:19 344 查看
因为前几天群里一个哥们使用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);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: