Yii2 database read/write splitting configuration, yii2 database read/write

Source: Internet
Author: User

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.

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.