国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > 建站經驗 > 正文

ecshop改造讀寫主從數據庫分離

2024-04-25 20:38:22
字體:
來源:轉載
供稿:網友

設置ecshop數據庫服務器分離,讓讀與寫分開 ,可大大加快網站速度。也可更好的優化網站。

data/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

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

}

}

cls_mysql.php文件類修改

<?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;//www.zuimoban.com

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

}

}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 化德县| 龙山县| 赤城县| 万安县| 昭觉县| 饶阳县| 黔东| 陆丰市| 营口市| 湄潭县| 阜新| 普陀区| 淮北市| 密山市| 颍上县| 慈利县| 滕州市| 抚松县| 苍南县| 柏乡县| 白水县| 澄江县| 遂川县| 贺州市| 铁岭市| 桐柏县| 青海省| 安泽县| 海丰县| 上蔡县| 宿州市| 永和县| 瑞丽市| 霍城县| 军事| 洱源县| 浪卡子县| 东至县| 兰考县| 大埔区| 外汇|