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
今日推荐