In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to achieve database read-write separation in Yii2.0 in php. The article is very detailed and has certain reference value. Interested friends must read it!
Open our database configuration file common\config\main-local.php and configure the db property as follows
One master database, multiple slaves
$db_config = [ 'class' => 'yii\db\Connection', 'dsn' => env('DB_DSN', 'mysql:host=xxx;port=3306;dbname=xl-pp'), 'username' => env('DB_USER', 'xx'), 'password' => env('DB_PASS', 'xxx! '), 'charset' => 'utf8', 'tablePrefix' => env('DB_PREFIX', 'xcxmall_'), //configure slave server 'slaveConfig' => [ 'username' => env('DB_SLAVE_USER',env("DB_USER")), 'password' => env('DB_SLAE_PASS',env("DB_PASS")), 'attributes' => [ PDO::ATTR_TIMEOUT => 10, ], 'charset' => 'utf8', 'tablePrefix' => env('DB_PREFIX', 'xcxmall_'), ], //Configure the slave server group, configure multiple slave libraries need one dns, multiple slave library login user name and password must be the same, //If one slave library is suspended, the second slave library will be automatically linked 'slaves' => [ ['dsn' => env('DB_SLAVE_DSN', env("DB_DSN"))], ['dsn1' => env('DB_SLAVE_DSN', env("DB_DSN"))] ],];
The above configuration can achieve yii2 database read and write separation operation, very simple, as long as a configuration is ok, read and write separation function automatically completed by the background code. The caller does not care.
Here is how to configure multiple master databases,
//configure the primary server 'masterConfig' => [ 'username' => 'master', 'password' => '', 'attributes' => [ PDO::ATTR_TIMEOUT => 10, ], 'charset' => 'utf8', 'tablePrefix' => env('DB_PREFIX', 'xcxmall_'), ], //configure the primary server group 'masters' => [ ['dsn' => 'dsn for master server 1'], ['dsn' => 'dsn for master server 2'], ], The above is "How to achieve database read-write separation in Yii2.0 in php" All the contents of this article, thank you for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.