数据库
首页 > 数据库> > [Laravel系列] 配置mysql数据库读写分离

[Laravel系列] 配置mysql数据库读写分离

作者:互联网

项目为了提升性能

需要select查询的时候,读取从库mysql数据

插入修改的时候,操作主库mysql数据

 

在laravel中只需要配置一下就可以实现

read就是从库的连接信息,write就是主库的连接信息,在.env里可以进行配置

        'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_OA_URL'),
            //配置数据库读写分离
            'read' => [
                'host' => env('DB_OA_READ_HOST', '127.0.0.1'),
                'port' => env('DB_OA_READ_PORT', '3306'),
                'database' => env('DB_OA_READ_DATABASE', 'forge'),
                'username' => env('DB_OA_READ_USERNAME', 'forge'),
                'password' => env('DB_OA_READ_PASSWORD', ''),
            ],
            'write' => [
                'host' => env('DB_OA_HOST', '127.0.0.1'),
                'port' => env('DB_OA_PORT', '3306'),
                'database' => env('DB_OA_DATABASE', 'forge'),
                'username' => env('DB_OA_USERNAME', 'forge'),
                'password' => env('DB_OA_PASSWORD', ''),
            ],
            'unix_socket' => env('DB_OA_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],

 

标签:Laravel,READ,读写,DB,OA,env,mysql,forge
来源: https://www.cnblogs.com/taoshihan/p/15475912.html