Example of Thinkphp Realizing MySQL Read Write Separation Operation

  • 2021-07-06 10:14:46
  • OfStack

Compared with other methods to realize the separation of reading and writing of MySQL, it is easy to use to realize the separation of reading and writing of MySQL with Thinkphp framework. The sample code of its configuration file is as follows:


'DB_TYPE'=> 'mysql',
'DB_DEPLOY_TYPE' => 1, // Open to support multiple servers         
'DB_RW_SEPARATE'=>true,// Database read-write separation 
'DB_HOST'=> '192.168.11.101,192.168.11.102', 
'DB_NAME'=>'test',      
'DB_USER'=>'admin',      
'DB_PWD'=>'admin101',    
'DB_PREFIX' => 'fav_',

Precautions:

1. When using the original sql statement for writing operation, execute should be used, and query should be used for reading operation.
2. MySQL data master-slave synchronization still depends on MySQL mechanism, so the delay problem of MySQL master-slave synchronization needs to be optimized at this time. Too long delay time not only affects business, but also affects user experience.


Related articles: