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: