前两天配置好了mysql主从方式,今天就拿ecshop练习读写分离。以下代码仅供学习参考,不成熟的地方,还需完善。

<?php

$db_name   = "ecshop";

$prefix    = "ecs_";

$timezone    = "Europe/Berlin";

$cookie_path    = "/";

$cookie_domain    = "";

$session = "";

$_config = array();

//数据库主服务器设置, 支持多组服务器设置, 当设置多组服务器时, 则会随机使用某个服务器
$_config['master'][]['dbhost'] = "192.168.2.175:3306";
$_config['master'][]['dbname'] = "ecshop";
$_config['master'][]['dbuser'] = "dragon";
$_config['master'][]['dbpw'] = "loong"; /*
*$_config['master'][2]['dbhost'] = "";
*...
*/ //数据库从服务器设置( slave, 只读 ), 支持多组服务器设置, 当设置多组服务器时, 系统每次随机使用
$_config['slave'][]['dbhost'] = "192.168.2.176:3306";
$_config['slave'][]['dbname'] = "ecshop";
$_config['slave'][]['dbuser'] = "ivan";
$_config['slave'][]['dbpw'] = "loong"; $_config['slave'][]['dbhost'] = "192.168.2.177:3306";
$_config['slave'][]['dbname'] = "ecshop";
$_config['slave'][]['dbuser'] = "ivan";
$_config['slave'][]['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  

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

cls_mysql.php文件类修改

    <?php  

    if (!defined('IN_ECS'))
{
die('Hacking attempt');
} class cls_mysql
{
var $link_id = NULL; var $settings = array(); var $queryCount = ;
var $linkCount = ;
var $queryTime = '';
var $queryLog = array(); var $max_cache_time = ; // 最大的缓存时间,以秒为单位 var $cache_data_dir = 'temp/query_caches/';
var $root_path = ''; var $error_message = array();
var $platform = '';
var $version = '';
var $dbhash = '';
var $starttime = ;
var $timeline = ;
var $timezone = ; var $mysql_config_cache_file_time = ; var $mysql_disable_cache_tables = array(); // 不允许被缓存的表,遇到将不会进行缓存
var $config = array(); function __construct($config, $charset = 'utf8', $pconnect = , $quiet = )
{
$this->cls_mysql($config, $charset, $pconnect, $quiet);
} function cls_mysql($config, $charset = 'utf8', $pconnect = , $quiet = )
{
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 = , $quiet = )
{
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() * ); // 对 PHP 4.2 以下的版本进行随机数函数的初始化工作
}
if (!$this->link_id)
{
if (!$quiet)
{
//连接超过10次,中断连接,抛出错误
if($this->linkCount>){
$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']{}) && $row['Value']{} == ':' && !empty($row['Value']{}) && $row['Value']{} == "\\")
{
$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);
}
}

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

ecshop改造读写分离配置与改造的更多相关文章

  1. ecshop改造读写分离

    前两天配置好了mysql主从方式,今天就拿ecshop练习读写分离.以下代码仅供学习参考,不成熟的地方,还需完善. config.php <?php $db_name = "ecsho ...

  2. MySQL5.6 Replication主从复制(读写分离) 配置完整版

    MySQL5.6 Replication主从复制(读写分离) 配置完整版 MySQL5.6主从复制(读写分离)教程 1.MySQL5.6开始主从复制有两种方式: 基于日志(binlog): 基于GTI ...

  3. Mysql一主多从和读写分离配置简记

    近期开发的系统中使用MySQL作为数据库,由于数据涉及到Money,所以不得不慎重.同时,用户对最大访问量也提出了要求.为了避免Mysql成为性能瓶颈并具备很好的容错能力,特此实现主从热备和读写分离. ...

  4. yii2的数据库读写分离配置

    简介 数据库读写分离是在网站遇到性能瓶颈的时候最先考虑优化的步骤,那么yii2是如何做数据库读写分离的呢?本节教程来给大家普及一下yii2的数据库读写分离配置. 两个服务器的数据同步是读写分离的前提条 ...

  5. Mycat入门配置_读写分离配置

    1.Mycat的分片 两台数据库服务器: 192.168.80.11 192.168.80.4 操作系统版本环境:centos6.5 数据库版本:5.6 mycat版本:1.4 release 数据库 ...

  6. mysql读写分离配置(整理)

    mysql读写分离配置 环境:centos7.2 mysql5.7 场景描述: 数据库Master主服务器:192.168.206.100 数据库Slave从服务器:192.168.206.200 M ...

  7. MySQL+MyCat分库分表 读写分离配置

    一. MySQL+MyCat分库分表 1 MyCat简介 java编写的数据库中间件 Mycat运行环境需要JDK. Mycat是中间件.运行在代码应用和MySQL数据库之间的应用. 前身 : cor ...

  8. laravel学习:主从读写分离配置的实现

    本篇文章给大家带来的内容是关于laravel学习:主从读写分离配置的实现,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 在DB的连接工厂中找到以下代码.../vendor/larav ...

  9. MySQL主从同步、读写分离配置步骤、问题解决笔记

    MySQL主从同步.读写分离配置步骤.问题解决笔记 根据要求配置MySQL主从备份.读写分离,结合网上的文档,对搭建的步骤和出现的问题以及解决的过程做了如下笔记:       现在使用的两台服务器已经 ...

随机推荐

  1. document.body.clientHeight与document.documentElement.clientHeight

    当你的网页有: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...

  2. Redis HyperLogLog

      Redis 在 2.8.9 版本添加了 HyperLogLog 结构. Redis HyperLogLog 是用来做基数统计的算法,HyperLogLog 的优点是,在输入元素的数量或者体积非常非 ...

  3. css笔记13:display用法

    1.代码演示: element.html如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...

  4. 使用apache和IIS,共用80端口的一个解决方案【转】

    将apache设为使用80端口,IIS使用其它端口,比如81,然后将apache作为IIS的代理. 在httpd.conf里面,取消下面四行的注释: LoadModule proxy_module m ...

  5. CSS设置行内元素和块级元素的水平居中、垂直居中

    CSS设置行内元素的水平居中 div{text-align:center} /*DIV内的行内元素均会水平居中*/ CSS设置行内元素的垂直居中 div{height:30px; line-heigh ...

  6. centos安装ftp

    yum install -y vsftpd安装vsftpd service vsftpd start 启动vsftpd vim /etc/vsftpd/vsftpd.conf 将anonymous_e ...

  7. 常用html演示模板

    --学习测试新的css,js,经常用到test.html,在这里放一个: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transition ...

  8. 关于Java中的构造方法和set方法()给属性赋值

    对于一个类中的成员变量(属性),属性如果都设置成了private类型,那么对外给属性设置了get和set方法 , 那么外部程序中给这些属性设置值,有两种方式. 第一种就是通过set()方法. 第二种就 ...

  9. Windows 8.1 归档 —— Step 3 软件的选择与安装

    这里我将列举出一些 Windows 8.1 下实用的软件 --运行库-- 用精简版系统或者绿色版软件时,使用软件经常遇见的情况是“由于应用程序配置不正确,应用程序未能启动”,提示缺少dll文件等.大部 ...

  10. JAVA基础之理解JNI原理

    JNI是JAVA标准平台中的一个重要功能,它弥补了JAVA的与平台无关这一重大优点的不足,在JAVA实现跨平台的同时,也能与其它语言(如C.C++)的动态库进行交互,给其它语言发挥优势的机会. 有了J ...