thinkphp Master-Slave database configuration

Source: Internet
Author: User

Thinkphp framework itself is to support read-write separation, how to do it? The simplest way to do this is to modify it in the configuration file: PHP code
  1. ' Db_type ' = ' mysql ',
  2. ' Db_deploy_type ' + 1, //Fight support multi-server
  3. ' Db_rw_separate ' =>true, //read/write <strong> detach </strong> separate
  4. ' Db_host ' = ' 192.168.100.78,192.168.100.60 ', //Database server address Master (write)/slave (read)
  5. ' Db_name ' = 'test ',
  6. ' Db_user ' =' root ',
  7. ' Db_pwd ' =' root ',
  8. ' Db_prefix ' = ' fav_ ',
The default first database 78 is the primary database, responsible for the write operation, the second 60 is responsible for the read operation, thinkphp will automatically recognize the read and write, and then allocate the corresponding database, to achieve the effect of read and write separation. Precautions: 1. If you are setting up a distributed database, DB_DSN mode configuration is not supported temporarily. 2. If you are currently using a distributed database and have read-write separation, the query method is always read server execution, so the query method corresponds to read operations, regardless of your SQL statement. 3. If you are currently using a distributed database and have read-write separation, the Execute method is always executed at the write server, so the Execute method corresponds to a write operation, regardless of your SQL statement. Therefore, ' db_dsn ' = ' Mysql:host=192.168.100.60;dbname=51fanli_cang ', this way does not support the master-slave separation, need to change. When using the original ecological SQL statement to write operations, to use execute, read operations to use query, remember, otherwise disorderly dropped.

thinkphp Master-Slave database configuration

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.