ECSHOP Read-Write Splitting Configuration and Modification (Reprinted)

A few days ago, a buddy in the group encountered MySQL access bottlenecks while using ECSHOP. I took a quick look at ECSHOP master-slave separation. The following is a reprinted article.

The code below is for study reference only; the immature parts still need improvement.

 

 

config.php

Copy code
<?php$db_name   = "ecshop";$prefix    = "ecs_";$timezone    = "Europe/Berlin";$cookie_path    = "/";$cookie_domain    = "";$session = "1440";$_config = array();//Database primary server settings, supports multiple server sets; when multiple sets are configured, one server will be randomly selected$_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'] = ""; *... *///Database slave server settings (slave, read-only), supports multiple server sets; when multiple sets are configured, one will be randomly selected each time$_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', '');?>
Copy code

 

Initialize database connection class

Copy code
    /* Initialize database class       * If a slave server is configured, initialize the slave database class     */      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);      }  
Copy code

 Add cls_mysql_slave.php slave database class

[php]

Copy code
<?phprequire(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);    }    /* Randomly assign slave database connection */    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 executing a query operation, use the slave database connection        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);    }    /* Remove failed connection*/    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);    }         }
Copy code

Leave a Comment

Your email address will not be published.