ecshop改造读写分离配置与改造
前两天配置好了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改造读写分离配置与改造的更多相关文章
- ecshop改造读写分离
前两天配置好了mysql主从方式,今天就拿ecshop练习读写分离.以下代码仅供学习参考,不成熟的地方,还需完善. config.php <?php $db_name = "ecsho ...
- MySQL5.6 Replication主从复制(读写分离) 配置完整版
MySQL5.6 Replication主从复制(读写分离) 配置完整版 MySQL5.6主从复制(读写分离)教程 1.MySQL5.6开始主从复制有两种方式: 基于日志(binlog): 基于GTI ...
- Mysql一主多从和读写分离配置简记
近期开发的系统中使用MySQL作为数据库,由于数据涉及到Money,所以不得不慎重.同时,用户对最大访问量也提出了要求.为了避免Mysql成为性能瓶颈并具备很好的容错能力,特此实现主从热备和读写分离. ...
- yii2的数据库读写分离配置
简介 数据库读写分离是在网站遇到性能瓶颈的时候最先考虑优化的步骤,那么yii2是如何做数据库读写分离的呢?本节教程来给大家普及一下yii2的数据库读写分离配置. 两个服务器的数据同步是读写分离的前提条 ...
- Mycat入门配置_读写分离配置
1.Mycat的分片 两台数据库服务器: 192.168.80.11 192.168.80.4 操作系统版本环境:centos6.5 数据库版本:5.6 mycat版本:1.4 release 数据库 ...
- mysql读写分离配置(整理)
mysql读写分离配置 环境:centos7.2 mysql5.7 场景描述: 数据库Master主服务器:192.168.206.100 数据库Slave从服务器:192.168.206.200 M ...
- MySQL+MyCat分库分表 读写分离配置
一. MySQL+MyCat分库分表 1 MyCat简介 java编写的数据库中间件 Mycat运行环境需要JDK. Mycat是中间件.运行在代码应用和MySQL数据库之间的应用. 前身 : cor ...
- laravel学习:主从读写分离配置的实现
本篇文章给大家带来的内容是关于laravel学习:主从读写分离配置的实现,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 在DB的连接工厂中找到以下代码.../vendor/larav ...
- MySQL主从同步、读写分离配置步骤、问题解决笔记
MySQL主从同步.读写分离配置步骤.问题解决笔记 根据要求配置MySQL主从备份.读写分离,结合网上的文档,对搭建的步骤和出现的问题以及解决的过程做了如下笔记: 现在使用的两台服务器已经 ...
随机推荐
- MySQL:ONDUPLICATEKEYUPDATE用法
如果在INSERT语句末尾指定了ON DUPLICATE KEY UPDATE,并且插入行后会导致在一个UNIQUE索引或PRIMARY KEY中出现重复值,则执行旧行UPDATE:如果不会导致唯一值 ...
- 读logback源码系列文章(五)——Appender --转载
原文地址:http://kyfxbl.iteye.com/blog/1173788 明天要带老婆出国旅游几天,所以这段时间暂时都更新不了博客了,临走前再最后发一贴 上一篇我们说到Logger类的inf ...
- java 开源缓存框架--转载
原文地址:http://www.open-open.com/13.htm JBossCache/TreeCache JBossCache是一个复制的事务处理缓存,它允许你缓存企业级应用数据来更好的 ...
- Debian下MySQL配置
1 安装 $ apt-get install mysql-server $ apt-get install mysql-client 2 修改MySQL的口令 一般上一步会让你输入root密码,如果没 ...
- python(4) - 装饰器2
接下来修改一下上一篇的login,将用户名传递给验证函数. def login(func): #接收一个函数作为参数 def inner(name): print("用户验证通过....&q ...
- [转]"Windows Phone 7程序设计”完全版电子书可以免费下载了
本文转自:http://www.cnblogs.com/salam/archive/2010/10/29/1864246.html 现在学习Windows Phone 7开发资料十分有限,除了MSDN ...
- LeetCode 345
Reverse Vowels of a String Write a function that takes a string as input and reverse only the vowels ...
- [改善Java代码]不要在finally块中处理返回值
在finally代码块中处理返回值,这是在面试题中经常出现的题目.但是在项目中绝对不能再finally代码块中出现return语句,这是因为这种处理方式非常容易产生"误解",会严重 ...
- (转载)HashMap工作原理
HashMap是近些年来java面试中常问到的知识点,很多人(包括我在内)都知道HashMap的用法,也知道HashMap与HashTable之间的区别,但是却不知其所以然,于是乎,本人开始查阅相关资 ...
- poj 1185 炮兵阵地 状态压缩dp
思路:定义一个三维数组dp[x][i][j]其中x为now和pre两种状态,now表示当前两行最优解,pre表示出了本行外,前两行的最优解.那么状态转移方程为 dp[now][j][k]=max(dp ...