yii2 프레임 워크 mysql 읽기-쓰기 분리 구성, 몇 초 만에 이해!

1. yii2-app-basic \ config \ db.php 데이터베이스 구성 파일을 찾아이 코드를 추가합니다.

2.db.php 코드

<?php

return [
    'class' => 'yii\db\Connection',
    'dsn' => 'mysql:host=localhost;dbname=tp',
    'username' => 'root',
    'password' => '你的密码',
    'charset' => 'utf8',

    'slaveConfig' => [
        'username' => 'root',
        'password' => '你的密码',
        'attributes' => [
            //use a smaller connection timeout
            PDO::ATTR_TIMEOUT => 10,    // 请求超时时间
        ],
    ],
    // 配置从服务器组
    'slaves' => [
        [
            'dsn' => 'mysql:host=192.168.0.2;dbname=tp'
        ]
    ]

    // Schema cache options (for production environment)
    //'enableSchemaCache' => true,
    //'schemaCacheDuration' => 60,
    //'schemaCache' => 'cache',
];

비밀번호를 자신의 것으로 변경하고 슬레이브 서버의 데이터베이스 정보를 입력하면 yii 프레임 워크가 자동으로 읽기와 쓰기의 분리를 실현하도록 도와줍니다.

추천

출처blog.csdn.net/zhunju0089/article/details/103527482