Master-slave Database Configuration of ThinkPHP

Source: Internet
Author: User
Thinkphp has been used in recent projects. I have never touched thinkphp for N years. But now we still need to use it. The wxcms that can be downloaded can also be used to optimize the performance .. In fact, I only have one server. Forced to install it, so let's turn to other people's articles, because he wrote a special sentence: as long as it is exec, it must go from the master database,

Thinkphp has been used in recent projects. I have never touched thinkphp for N years. But now we still need to use it. The wxcms that can be downloaded can also be used to optimize the performance .. In fact, I only have one server. Forced to install it, so let's turn to other people's articles, because he wrote a special sentence: as long as it is exec, it must go from the master database,

Thinkphp has been used in recent projects. I have never touched thinkphp for N years. But now we still need to use it. The wxcms that can be downloaded can also be used to optimize the performance .. In fact, I only have one server. Force Installation

So let's take a look at other people's articles, because he specifically wrote: as long as it is exec, it must go from the master database, and query must go from the database. Therefore, we recommend that you do not perform update and insert operations in the query. Just in case

Address: http://www.verydemo.com/demo_c327_i782.html

Preface:

The Thinkphp framework supports read/write splitting. How can this problem be solved?

The simplest way is to modify it in the configuration file:

'Db _ type' =>? 'Mysql ',????? 'Db _ DEPLOY_TYPE '? =>? 1 ,?? // Multi-server ?????????????????? 'Db _ RW_SEPARATE '=> true ,? // Read/write splitting separated ???? 'Db _ host' =>? '1970. 168.100.78, 192.168.100.60 ',? //? Database server address? Master (write)/slave (read )???? 'Db _ name' => 'test ',??????????????????? 'Db _ user' => 'root ',????????????????? 'Db _ pwd' => 'root ',??????????? 'Db _ prefix '? =>? 'Fav _',?????

By default, the first database 78 is the primary database, which is responsible for the write operation, and the second database 60 is responsible for the read operation. Thinkphp automatically recognizes the read and write operations, and then allocates the corresponding database to the link, to achieve read/write splitting.

Note:

1. If you want to set up a distributed database, DB_DSN is not supported for the time being.

2. if you currently use a distributed database and set read/write splitting, the query method is always executed on the read server. Therefore, the query method corresponds to read operations, no matter what your SQL statement is.

3. if you currently use a distributed database and set read/write splitting, the execute method is always executed on the write server. Therefore, the execute method corresponds to write operations, no matter what your SQL statement is.

Therefore, 'db _ DSN '=> 'mysql: host = 192.168.100.60; dbname = 51fanli_cang'. This method does not support master-slave separation and needs to be changed.

When you use the original SQL statement for write operations, execute is used, and query is used for read operations. Remember, otherwise it will be out of order.

Done!

--- EOF ---

Original article address: ThinkPHP Master/Slave database configuration. Thank you for sharing it with the original author.

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.