barton 2020-09-19 22:26:13 2476次浏览 1条回复 0 0 0

Yii2 配置了主从,用 findOne 查询出数据,save 修改数据。修改数据没有走主表,从表修改了数据。是不能用 findOne 查询吗? 代码如下:

$sdlm_model = new ReceiveSdlm();
$list_one = $sdlm_model->findOne($id);
$list_one->is_push = $is_push;
if ($list_one->save()) {
    return "1001";
} else {
    $error = $sdlm_model->getErrors();
    $this->actionLogs(json_encode($error),'add_table.log');
    return "1002";
}
  • 回复于 2020-09-23 09:50 举报

    做个读写分离,配置好只往主库写数据

    1 条回复
    回复于 2020-09-23 10:17 回复

    除了配置db.php 文件,还需要配置其他文件吗?
    db.php 文件代码:
    <?php

    return

    [

    'class' => 'yii\db\Connection',
    
    //主库配置
    'dsn' => 'mysql:host=192.1.47.20;port=13306;dbname=zgfdzsd1',
    'username' => 'dzsd',
    'password' => 'zgy_dzsd123..',
    'charset' => 'utf8',
    
    //配置从服务器
    'slaveConfig' => [
        'dsn' => 'mysql:host=192.1.32.21;port=3306;dbname=zgfdzsd1',
        'username' => 'dzsd',
        'password' => 'zgy_dzsd123..',
        'attributes' =>[
            PDO::ATTR_TIMEOUT => 10,
        ],
        'charset' => 'utf8',
    ],
    

    //

    // 从库的配置列表
    'slaves' => [
        ['dsn' => 'mysql:host=192.1.32.160;port=3306;dbname=zgfdzsd',],
        ['dsn' => 'dsn for slave server 1'],
    ],
    

    ];

    , 觉得很赞
您需要登录后才可以回复。登录 | 立即注册