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.