Master-slave database settings for Yii2

Source: Internet
Author: User
Tags dsn

Master-slave database settings for Yii2

As the project grows, the master and slave databases are still indispensable. Using the Yii framework for development, how do I set the master and slave databases? It is actually very simple.

First, modify the configuration file config/db. php, in which slaveConfig is the public part of the slave server, you can also set it to the configuration of each slave server in slaves.

'Class' => 'yii \ db \ connection', // configure the master server 'dsn '=> 'dsn for master Server', 'username' => 'master ', 'Password' => '', 'charset' => 'utf8', 'tableprefix' => 'php _', // The default value is null. // configure the slave server 'slveconfig' => ['username' => 'slave ', 'Password' => '', 'charset' => 'utf8', 'tableprefix' => 'php _ ', 'bubuckets' => [// use a smaller connection timeout PDO :: ATTR_TIMEOUT => 10,],], // configure the slave server group 'slafs' => [['dsn '=> 'dsn for slave server 1'], ['dsn '=> 'dsn for slave server 2'], ['dsn' => 'dsn for slave server 3'], ['dsn '=> 'dsn for slave server 4'],],

You can also configure the master server as follows for multiple master servers. For more information about character delimiter set and table prefix settings, see the preceding section.

// Configure the master server 'masterconfig' => ['username' => 'master', 'Password' => '', 'bubuckets' => [// use a smaller connection timeout PDO: ATTR_TIMEOUT => 10,],], // configure the master server group 'masters '=> [['dsn' => 'dsn for master server 1'], ['dsn '=> 'dsn for master server 2'],],

After configuration, how can we use Yii? Yii is very good, because you hardly need to modify your code, you don't have to worry about how to use the master server configuration or database configuration for database operations in your code. The framework has already been implemented. By default, the execut () function operates on the master database. In other cases, it operates on the slave database, such as queryAll (). This is also true for AR operations, because it is implemented based on Yii: $ app-> db.

The problem of Yii2 inline Script Loading failure caused by Pjax in Yii2

Yii2 implements password Modification

Yii User Login Mechanism

Introduce js and css files in Yii

Incomplete Yii Solution

Yii CGridView basic usage

Yii framework distributed cache Implementation Scheme

Yii details: click here
Yii: click here

This article permanently updates the link address:

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.