Yii2 for read-write separation (MySQL master-slave database)

Source: Internet
Author: User

Read-Write separation (Read/write splitting).

1. Principle:

Allows the primary database (master) to handle transactional increment, change, delete operations (INSERT, UPDATE, delete), and select query operations from the database (slave).

2. Advantages:

Back up the database, share the database pressure, and withstand greater traffic.

Yii implementation of Read and write separation is very simple, only need to write a few configuration parameters in the configuration file can be implemented.

2.1. Configuring the Db.php File

2.2. Create two databases in order to demonstrate that the IP is pointing to the local MySQL database, but the database name is different, and at the same time inserting a data into the database, the username of the slave database is changed to slave.

2.3. View Query Results

Query for the data with primary key ID 1

Here are the results of the query

Original address: http://www.linuxidc.com/Linux/2015-07/120134.htm

Yii2 for read-write separation (MySQL master-slave database)

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.