Using YII2 to implement master-slave database setup _php instance

Source: Internet
Author: User
Tags dsn php code

Objective

MySQL master-slave synchronization is currently using a more extensive database architecture, technology is more mature, configuration is not complex, especially for the load is relatively large site, master-Slave synchronization can effectively alleviate the pressure of database reading and writing. Previously in the use of YII1, the support of the master-slave database is not so convenient, can only write a number of DB components, and then in the AR getdb return the corresponding db. This can also be used against master-slave databases

Implementation methods

YII2 has solved this problem, directly in the code to deal with it:

PHP code

' DB ' =>[' 
   class ' => ' yii\db\connection ', 
 
  //Configure Master server 
  ' DSN ' => ' DSN for master server ', 
  ' Username ' => ' master ', ' Password ' => ', ' CharSet ' => ' utf8 ', ' tableprefix ' => ' php_ ', 
  //default NULL 
 
  //configuration from Server 
  ' Slaveconfig ' => [ 
    ' username ' => ' slave ', 
    ' password ' => ', ' charset ' 
    = > ' UTF8 ', 
   ' tableprefix ' => ' php_ ', 
    ' attributes ' => [ 
      //Use a smaller 
      connection-timeout Pdo::attr_timeout =>, 
    ], 
   
  ], 

Isn't it super convenient, and more than that, you can also configure from the server group:

PHP code

' DB ' =>[ 
  //... Above are some standard configuration 
  ' slaves ' => [ 
    [' DSN ' => ' DSN for slave server 1 '], 
    [' DSN ' => ' DSN for slave server 2 '],
   
    [' DSN ' => ' DSN for slave server 3 ', 
    [' DSN ' => ' DSN for slave server 4 '], 
    

   

More commendable is that the primary server is also the configuration of multiple primary servers is the following, where the character encoding set, table prefix settings reference above.

PHP code

' DB ' =>[ 
  //config master server 
  ' masterconfig ' => [ 
    ' username ' => ' master ', 
    ' password 
    ' => '], ' Attributes ' => [/ 
      /Use a smaller connection timeout pdo::attr_timeout => 
      , 
    ], 
  ], 
 
  // Configure the primary server group 
  ' masters ' => [ 
    [' DSN ' => ' DSN for master server 1 '], 
    [' DSN ' => ' DSN for master server 2 '], 
  ], 
  //other ... slaves 

It's easy to relax.

Of course, if you want to use more easily, these, in fact, is to use YII2 ar. You don't have to change the code.

Summarize

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.

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.