Alibaba Cloud rds has a question: How can I set up a database connection for one primary rds and multiple read-only rds instances?

Source: Internet
Author: User
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 );}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.