Do you need to configure multiple connections or just configure primary RDS?
Reply content:
Do you need to configure multiple connections or just configure primary RDS?
Usually write off the main RDS, read-only RDS.
In the case of PHP, the MVC framework is usually just a configuration.
such as thinkphp, you can specify the use of an RDS when reading, or a random RDS
Here is the code (read the note If you don't understand it):
/** * Connection to distributed server * @access protected * @param boolean $master primary server * @return void */protected Fu Nction Multiconnect ($master =false) {foreach ($this->config as $key = + $val) {$_config[$key] = Explode (', ', $val); }//database read and write whether to detach if (C (' db_rw_separate ')) {//master-slave with read-write separation if ($master) The primary server writes $R = Floor (Mt_rand (0,c (' Db_master_num ')-1)); else{if (Is_numeric (C (' Db_slave_no '))) {//specify server Read $r = C (' db_slave_no '); }else{//Read operation connection from server $r = Floor (Mt_rand (C (' Db_master_num '), COUNT ($_config[' hostname ' ])-1); Each randomly connected database}}}else{//read-write operation does not differentiate between server $r = Floor (Mt_rand ($ 0,count ($ _config[' hostname ')-1); Each randomly connected database} $db _config = Array (' username ' = = isset ($_config[' username '] [$r])? $_config[' username ' [$r]:$_config[' username '][0], ' password ' = isset ($_config[' password ' [$r])? $_config[' PASSW Ord '] [$r]:$_config[' password '][0], ' hostname ' + isset ($_config[' hostname ' [$r])? $_config[' hostname ' [$r]: $_config[' hostname '][0], ' hostport ' = isset ($_config[' hostport ' [$r])? $_config[' Hostport ' [$r]:$_config[' Hostport '][0], ' database ' = isset ($_config[' database '] [$r])? $_config[' database ' [$r]:$_config[' database '] [0], ' dsn ' = isset ($_config[' DSN '] [$r])? $_config[' DSN '] [$r]:$_config[' DSN '][0], ' params ' = Isset ($_config[' params '] [$r])? $_config[' params '] [$r]:$_config[' params '][0], ' charset ' = Isset ( $_config[' CharSet '] [$r])? $_config[' CharSet '] [$r]:$_config[' CharSet '][0],); return $this->connect ($db _config, $r); }