Alibaba Cloud RDS queries, one master RDS, multiple read only RDS, how does the website set up the database connection?

Source: Internet
Author: User
Tags dsn
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); }
  • Related Article

    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.