ECSHOP读写分离配置与改造(转载)
2012-12-14 09:19
375 查看
因为前几天群里一个哥们使用ECSHOP遇到mysql访问瓶颈,简单看了下ECSHOP的主从分离 下面是转载的
以下代码仅供学习参考,不成熟的地方,还需完善。
config.php
增加cls_mysql_slave.php从库类
[php]
以下代码仅供学习参考,不成熟的地方,还需完善。
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]
<?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); } }
相关文章推荐
- ecshop改造读写分离配置与改造
- ecshop改造读写分离
- spring MVC、mybatis配置读写分离,ReplicationDriver(转载)
- ecshop改造读写分离
- ecshop改造读写分离
- Mysql主从配置,实现读写分离(转载)
- ecshop改造读写分离
- Keepalived配置与使用--转载
- 转载 解密蓝牙mesh系列 | 第六篇【安全性】【命名法】【启动配置设备】【启动配置协议】【向网络中添加新设备】【从网络中移除设备】【黑名单】【密钥刷新程序】【结论】
- 【转载】ubuntu下Pycharm安装及配置
- Tomcat数据库连接池配置 【转载】
- [转载]nginx负载均衡+keepalived三主(多主)配置
- [转载]CodeIgniter配置之URL
- FastMM配置文件详解(转载 csdn 老师)
- vim 开发配置(转载)
- 【转载】linux下的samba安装及配置
- 转载技术贴IIS6.0配置php
- 配置putty隧道登录远程window远程或远程数据库【转载】
- spring2.5事务配置5种方式(转载)
- Amoeba新版本如何与MYSQL读写分离配置