Home > Article > Backend Development > Yii2 implements read-write separation (MySQL master-slave database), yii2mysql_PHP tutorial
read/write splitting (Read/Write Splitting).
1. Principle:
让主数据库(master)处理事务性增、改、删操作(INSERT、UPDATE、DELETE),而从数据库(slave)处理SELECT查询操作。
2. Advantages:
备份数据库,分担数据库压力,承受更大的访问量。
It is very simple to realize the separation of reading and writing in yii. You only need to write a few configuration parameters in the configuration file to achieve it.
2.1. Configure db.php file
2.2. Create two databases to demonstrate that both IPs point to the local MySQL database, but the database names are different. At the same time, insert a piece of data into the database and change the username of the slave database to slave.
2.3. View query results
Query the data whose primary key id is 1
The following are the query results
Original address: http://www.linuxidc.com/Linux/2015-07/120134.htm