Yii2 database read/write splitting configuration, yii2 database read/write
Yii Chinese Network (yii-china.com)-yii2 database read/write splitting Configuration
Introduction
Database read/write splitting is the first optimization step when a website encounters a performance bottleneck. How does yii2 implement database read/write splitting? This tutorial introduces the configuration of read/write splitting for yii2.
Data synchronization between the two servers is a prerequisite for read/write splitting, but this is not in the yii2 read/write splitting tutorial. The yii2 database read/write splitting configuration only implements read/write in the master database and query in the slave database, first, we need to configure data synchronization between the master and slave servers. For more information, see the master-slave synchronization configuration of linux databases.
Configuration
After the master-slave server database is synchronized, we can start the read/write splitting configuration of yii2. This document is also available on the official website, but it is not clear and there are no actual examples, soy Sauce is perfect here.
1. Open our database configuration file common \ config \ main-local.php and make the following configuration in the db Properties
'Db' => ['class' => 'yii \ db \ connection', // configure the master server 'dsn '=> 'mysql: host = 192.168.0.1; dbname = hyii2', 'username' => 'root', 'Password' => 'root', 'charset' => 'utf8 ', // configure the slave server 'slaveconfig' => ['username' => 'root', 'Password' => 'root ', 'bubuckets' => [// use a smaller connection timeout PDO: ATTR_TIMEOUT => 10,], 'charset' => 'utf8',], // configure the slave server group 'slafs' => [['dsn '=> 'mysql: host = 192.168.0.2; dbname = hyii2'],
The above configuration can implement the read/write Splitting Operation of the yii2 database. It is very simple. As long as one configuration is complete, the read/write splitting function is automatically completed by the background code. The caller does not need to care about it.
The above is just a configuration of one master and one slave. If you want one master and multiple slave, or multiple master and multiple slave, refer to this example and the official documents.