1.配置文件设置

$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', '');

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

/* 如果配置了从服务器,则初始化从库类  */
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);
}

3.编写从库类文件

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);
} }

4.cls_mysql类库修改

<?php
if (!defined('IN_ECS'))
{
die('Hacking attempt');
}
class cls_mysql
{
var $link_id = NULL;
var $settings = array();
var $queryCount = 0;
var $linkCount = 0;
var $queryTime = '';
var $queryLog = array();
var $max_cache_time = 300; // 最大的缓存时间,以秒为单位
var $cache_data_dir = 'temp/query_caches/';
var $root_path = '';
var $error_message = array();
var $platform = '';
var $version = '';
var $dbhash = '';
var $starttime = 0;
var $timeline = 0;
var $timezone = 0;
var $mysql_config_cache_file_time = 0;
var $mysql_disable_cache_tables = array(); // 不允许被缓存的表,遇到将不会进行缓存
var $config = array();
function __construct($config, $charset = 'utf8', $pconnect = 0, $quiet = 0)
{
$this->cls_mysql($config, $charset, $pconnect, $quiet);
}
function cls_mysql($config, $charset = 'utf8', $pconnect = 0, $quiet = 0)
{
if(!empty($config)) {
$config['charset'] = $charset;
$config['pconnect'] = $pconnect;
$this->config = $config;
} if (defined('EC_CHARSET'))
{
$charset = strtolower(str_replace('-', '', EC_CHARSET));
}
if (defined('ROOT_PATH') && !$this->root_path)
{
$this->root_path = ROOT_PATH;
}
$this->set_config($this->config);
if ($quiet)
{
$dbhost = $this->settings['dbhost'];
$dbuser = $this->settings['dbuser'];
$dbpw = $this->settings['dbpw'];
$dbname = $this->settings['dbname'];
$this->connect($dbhost, $dbuser, $dbpw, $dbname, $charset, $pconnect, $quiet);
}
}
//随机分配数据库连接
function set_config($config) {
$sid = array_rand($config['master']);
$settings = $config['master'][$sid];
$settings['sid'] = $sid;
$settings['charset'] = $this->config['charset'];
$settings['pconnect'] = $this->config['pconnect'];
$this->settings = $settings;
}
function connect($dbhost, $dbuser, $dbpw, $dbname = '', $charset = 'utf8', $pconnect = 0, $quiet = 0)
{
if ($pconnect)
{
if (!($this->link_id = @mysql_pconnect($dbhost, $dbuser, $dbpw)))
{
if (!$quiet)
{
$this->ErrorMsg("Can't pConnect MySQL Server!");
}
return false;
}
}
else
{
if (PHP_VERSION >= '4.2')
{
$this->link_id = @mysql_connect($dbhost, $dbuser, $dbpw, true);
}
else
{
$this->link_id = @mysql_connect($dbhost, $dbuser, $dbpw);
mt_srand((double)microtime() * 1000000); // 对 PHP 4.2 以下的版本进行随机数函数的初始化工作
}
if (!$this->link_id)
{
if (!$quiet)
{
//连接超过10次,中断连接,抛出错误
if($this->linkCount>9){
$this->ErrorMsg("Can't Connect MySQL Server!");
}
$this->linkCount++;
$this->del_error_link();
}
return false;
}
}
$this->dbhash = md5($this->root_path . $dbhost . $dbuser . $dbpw . $dbname);
$this->version = mysql_get_server_info($this->link_id);
/* 如果mysql 版本是 4.1+ 以上,需要对字符集进行初始化 */
if ($this->version > '4.1')
{
if ($charset != 'latin1')
{
mysql_query("SET character_set_connection=$charset, character_set_results=$charset, character_set_client=binary", $this->link_id);
}
if ($this->version > '5.0.1')
{
mysql_query("SET sql_mode=''", $this->link_id);
}
}
$sqlcache_config_file = $this->root_path . $this->cache_data_dir . 'sqlcache_config_file_' . $this->dbhash . '.php';
@include($sqlcache_config_file);
$this->starttime = time();
if ($this->max_cache_time && $this->starttime > $this->mysql_config_cache_file_time + $this->max_cache_time)
{
if ($dbhost != '.')
{
$result = mysql_query("SHOW VARIABLES LIKE 'basedir'", $this->link_id);
$row = mysql_fetch_assoc($result);
if (!empty($row['Value']{1}) && $row['Value']{1} == ':' && !empty($row['Value']{2}) && $row['Value']{2} == "\\")
{
$this->platform = 'WINDOWS';
}
else
{
$this->platform = 'OTHER';
}
}
else
{
$this->platform = 'WINDOWS';
}
if ($this->platform == 'OTHER' &&
($dbhost != '.' && strtolower($dbhost) != 'localhost:3306' && $dbhost != '127.0.0.1:3306') ||
(PHP_VERSION >= '5.1' && date_default_timezone_get() == 'UTC'))
{
$result = mysql_query("SELECT UNIX_TIMESTAMP() AS timeline, UNIX_TIMESTAMP('" . date('Y-m-d H:i:s', $this->starttime) . "') AS timezone", $this->link_id);
$row = mysql_fetch_assoc($result);
if ($dbhost != '.' && strtolower($dbhost) != 'localhost:3306' && $dbhost != '127.0.0.1:3306')
{
$this->timeline = $this->starttime - $row['timeline'];
}
if (PHP_VERSION >= '5.1' && date_default_timezone_get() == 'UTC')
{
$this->timezone = $this->starttime - $row['timezone'];
}
}
$content = '<' . "?php\r\n" .
'$this->mysql_config_cache_file_time = ' . $this->starttime . ";\r\n" .
'$this->timeline = ' . $this->timeline . ";\r\n" .
'$this->timezone = ' . $this->timezone . ";\r\n" .
'$this->platform = ' . "'" . $this->platform . "';\r\n?" . '>';
@file_put_contents($sqlcache_config_file, $content);
}
/* 选择数据库 */
if ($dbname)
{
if (mysql_select_db($dbname, $this->link_id) === false )
{
if (!$quiet)
{
$this->ErrorMsg("Can't select MySQL database!");
}
return false;
}
else
{
return true;
}
}
else
{
return true;
}
}
......
/* 删除失败连接*/
function del_error_link(){
unset($this->config['master'][$this->settings['sid']]);
$this->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);
}
}

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. jquery总结(1)

    jquery是一种js对象.里面封装了一些方法,但是jquery对象不能直接使用js方法,js对象不能直接使用jquery方法. jquery对象类似于js对象的集合,就是存在形式是以特殊数组的形式: ...

  2. BZOJ 2151 种树

    贪心+priority_queue. #include<iostream> #include<cstdio> #include<cstring> #include& ...

  3. 【C#学习笔记】浏览目录得到路径

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  4. ECSHOP 商品评论条件修改——购买过该商品且只能评价一次(购买多少次能评价多少次)

    下文转自http://bbs.ecshop.com/thread-1131529-1-1.html ECSHOP 商品评论条件修改,修改为购买过该商品多少次,就只能评价多少次.不需要修改数据库,原理简 ...

  5. 《Unix网络编程》卷2 读书笔记 第3章- System V IPC

    1. 概述 三种类型的System V IPC:System V 消息队列.System V 信号量.System V 共享内存区 System V IPC在访问它们的函数和内核为它们维护的信息上共享 ...

  6. IOS OC声明变量在@interface括号中与使用@property的区别(转载)

    刚开始接触OC再看别人写的代码的时候,常常困惑于人家在声明属性时的写法,总结出来有三中方式,不知道哪一种比较规范化,现在我把三种方式贴出来,然后再一一探讨每个方式声明属性的区别. 方式一:直接在@in ...

  7. 用DzzOffice管理阿里云OSS

    在DzzOffice分两种方式管理阿里云OSS 1.把阿里云oss作为多人或企业的共享网盘使用. 2.接入个人的阿里云oss管理,可同时管理多个bucket,多个bucket之间可以互传文件. 下面先 ...

  8. memset()实现及细节

    memset是计算机中C/C++语言函数.将s所指向的某一块内存中的前n个 字节的内容全部设置为ch指定的ASCII值, 块的大小由第三个参数指定,这个函数通常为新申请的内存做初始化工作, 其返回值为 ...

  9. 第一个自定义HTML网页

    前言 已经好几天没玩LOL了,实在手痒,下载了游戏,又卸载了,坦言:毛爷爷说的”好好天天向上“,真不容易.但还是回到学习个状态了,开始写,就“根本停不下来”,我也慢慢感受到代码的快乐了,并且想总结出一 ...

  10. 移动端的日期插件 mobiscroll 2.14.4 破解版

    官方报价695美元 http://mobiscroll.com/pricing 这个 mobiscroll 2.14.4 破解版 包括datetime和calendar组件,包括mobiscroll和 ...