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
Initialize database connection class
[php]
<?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); } }
