博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ecshop改造读写分离
阅读量:7033 次
发布时间:2019-06-28

本文共 8251 字,大约阅读时间需要 27 分钟。

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

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

增加cls_mysql_slave.php从库类

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文件类修改

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

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

你可能感兴趣的文章
10款AJAX/CSS/HTML的在线表单生成器
查看>>
android之app widget(三)
查看>>
在 .NET 3.5 中序列化和反序列化 JSON
查看>>
flash左导航不显示
查看>>
SAP BW好文章链接
查看>>
C#防止WebBrowser在新窗口中打开链接页面
查看>>
MongoDB 2.4 RC2 (2.3 开发系列) 发行说明
查看>>
php.ini修改php上传文件大小限制(转)
查看>>
office2003 下载地址 及密码
查看>>
C# ORM—Entity Framework 之Code first(代码优先)(二)
查看>>
/etc/vim/vimrc的一个的配置
查看>>
javascript商务通
查看>>
JavaScript Array 对象扩展方法
查看>>
利用ngxtop实时监控nginx的访问情况
查看>>
浏览器中关于事件的那点事儿(转)
查看>>
c#Lamdba表达式与托付
查看>>
VC皮肤库SkinSharp 1.0.6.6的使用
查看>>
php上传图片到server
查看>>
微信公众平台原创声明功能公测 自媒体原创保护的福音
查看>>
想学ps的,这全是精华,拿走不谢!!!
查看>>