laravel 的数据库读写分离

配置数据库读写分离连接(需要配合mysql数据库的主从复制)

ps:配合在之前学习的mysql数据库的主从复制

数据库的连接配置文件位于 config/database.php

'mysql' => [
    'driver' => 'mysql',
    'read' => [  // 这里项配置的是「读」连接
        'host' => env('DB_HOST_READ', '127.0.0.1'),
        'username' => env('DB_USERNAME_READ', 'forge'),
        'password' => env('DB_PASSWORD_READ', ''),
        'database' => env('DB_DATABASE_READ', 'db_read'),
    ],
    'write' => [ // 这里项配置的是「写」连接。
        'host' => env('DB_HOST_WRITE', '127.0.0.1'),
        'username' => env('DB_USERNAME_WRITE', 'forge'),
        'password' => env('DB_PASSWORD_WRITE', ''),
        'database' => env('DB_DATABASE_WRITE', 'db_write'),
    ],
    'port' => env('DB_PORT', '3306'),
    'database' => env('DB_DATABASE', 'forge'),
    'unix_socket' => env('DB_SOCKET', ''),
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'prefix' => '',
    'strict' => true,
    'engine' => null,
],

Read More

发表回复