gpt4 book ai didi

php - Yii2如何主从连接

转载 作者:行者123 更新时间:2023-11-29 01:20:56 24 4
gpt4 key购买 nike

我配置了 MySQL 主从复制。

如何配置 Yii2 Active Record 在主数据库上插入、更新和删除查询以及在从数据库上读取查询?

最佳答案

Yii2 会自动处理这个问题,允许您配置多个从站甚至多个主站。

[
'class' => 'yii\db\Connection',

// configuration for the master
'dsn' => 'dsn for master server',
'username' => 'master',
'password' => '',

// common configuration for slaves
'slaveConfig' => [
'username' => 'slave',
'password' => '',
'attributes' => [
// use a smaller connection timeout
PDO::ATTR_TIMEOUT => 10,
],
],

// list of slave configurations
'slaves' => [
['dsn' => 'dsn for slave server 1'],
['dsn' => 'dsn for slave server 2'],
['dsn' => 'dsn for slave server 3'],
['dsn' => 'dsn for slave server 4'],
],
]

链接到 Yii2 指南中的部分以获取更多信息:http://www.yiiframework.com/doc-2.0/guide-db-dao.html#read-write-splitting

关于php - Yii2如何主从连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30170814/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com