databaselaravelconnectionreplicationslave

Laravel - How to do DB Connection to Slave Database


as the title above, I need to do connection to slave database (for some reason)...

But I cannot find how to do it?

.

Below is my database config:

'mysql' => [
    'read' => [
        'host' => env('DB_SLAVE', '127.0.0.1'),
        'port' => env('DB_SLAVE_PORT', '3306'),
        'username'  => env('DB_SLAVE_USERNAME', 'root'),
        'password'  => env('DB_SLAVE_PASSWORD', 'pwdforslave'),
    ],
    'write' => [
        'host' => env('DB_MASTER', '127.0.0.1'),
        'port' => env('DB_MASTER_PORT', '3308'),
        'username'  => env('DB_MASTER_USERNAME', 'masteruser'),
        'password'  => env('DB_MASTER_PASSWORD', 'pwdformaster'),
    ],
    'driver'    => 'mysql',
    'database'  => 'amazingapp',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => 'aa_',
    'strict' => false,
    'engine' => env('DB_ENGINE', 'InnoDB'),
    'unix_socket' => env('DB_SOCKET', ''),
],  

.

To do connection with slave DB, currently what I'm thinking is DB::connection('mysql.slave');

But it is not working..


Solution

  • you mean create new database connection entry inside database.php and connect with statistic method through DB::connection('mysql2') ?

    maybe you can check this link Laravel Multiple Database

    .

    Can try this configuration

    'mysql' => [
        'read' => [
            'host' => env('DB_SLAVE', '127.0.0.1'),
            'port' => env('DB_SLAVE_PORT', '3306'),
            'username'  => env('DB_SLAVE_USERNAME', 'root'),
            'password'  => env('DB_SLAVE_PASSWORD', 'pwdforslave'),
        ],
        'write' => [
            'host' => env('DB_MASTER', '127.0.0.1'),
            'port' => env('DB_MASTER_PORT', '3308'),
            'username'  => env('DB_MASTER_USERNAME', 'masteruser'),
            'password'  => env('DB_MASTER_PASSWORD', 'pwdformaster'),
        ],
        'driver'    => 'mysql',
        'database'  => 'amazingapp',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => 'aa_',
        'strict' => false,
        'engine' => env('DB_ENGINE', 'InnoDB'),
        'unix_socket' => env('DB_SOCKET', ''),
    ],  
    
    'mysql_master' => [
        'host' => env('DB_MASTER', '127.0.0.1'),
        'port' => env('DB_MASTER_PORT', '3308'),
        'username'  => env('DB_MASTER_USERNAME', 'masteruser'),
        'password'  => env('DB_MASTER_PASSWORD', 'pwdformaster'),
        'driver'    => 'mysql',
        'database'  => 'amazingapp',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => 'aa_',
        'strict' => false,
        'engine' => env('DB_ENGINE', 'InnoDB'),
        'unix_socket' => env('DB_SOCKET', ''),
    ],  
    
    'mysql_slave' => [
        'host' => env('DB_SLAVE', '127.0.0.1'),
        'port' => env('DB_SLAVE_PORT', '3306'),
        'username'  => env('DB_SLAVE_USERNAME', 'slaveuser'),
        'password'  => env('DB_SLAVE_PASSWORD', 'pwdforslave'),
        'driver'    => 'mysql',
        'database'  => 'amazingapp',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => 'aa_',
        'strict' => false,
        'engine' => env('DB_ENGINE', 'InnoDB'),
        'unix_socket' => env('DB_SOCKET', ''),
    ],  
    

    .

    So, if want connect to slave, just use DB::connection('mysql_slave');