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