Do I need to configure multiple connections or only configure the primary rds? Do I need to configure multiple connections or only configure the primary rds?
Reply content:
Do I need to configure multiple connections or only configure the primary rds?
Generally, data is written to the primary rds and read to the read-only rds.
For php, the mvc Framework usually only needs to be configured.
For example, thinkphp can specify an rds instance when reading data, or randomly select an rds instance.
The following is the code (for more information, see comments ):
/*** Connect to the distributed server * @ access protected * @ param boolean $ master server * @ return void */protected function multiConnect ($ master = false) {foreach ($ this-> config as $ key => $ val) {$ _ config [$ key] = explode (',', $ val );} // database read/write splitting if (C ('db _ RW_SEPARATE ') {// master/Slave Mode read/write splitting 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 the server read $ r = C ('db _ SLAVE_NO ');} Else {// read operation connection slave server $ r = floor (mt_rand (C ('db _ MASTER_NUM '), count ($ _ config ['hostname']) -1); // the database to be randomly connected each time} else {// read/write operations do not distinguish servers $ r = floor (mt_rand (0, count ($ _ config ['hostname'])-1 )); // database connected randomly} $ db_config = array ('username' => isset ($ _ config ['username'] [$ r])? $ _ Config ['username'] [$ r]: $ _ config ['username'] [0], 'Password' => isset ($ _ config ['Password'] [$ r])? $ _ Config ['Password'] [$ 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 );}