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
- ' Db_type ' = ' mysql ',
- ' Db_deploy_type ' + 1, //Fight support multi-server
- ' Db_rw_separate ' =>true, //read/write <strong> detach </strong> separate
- ' Db_host ' = ' 192.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_ ',
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