-2

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..

Syamsoul Azrien
  • 2,534
  • 4
  • 32
  • 55

2 Answers2

0

You can do multiple DB Connection on your .env without touching your database.php

This is how I connect my multiple DB connection

 DB_CONNECTION=mysql
 DB_HOST=localhost
 DB_PORT=3306
 DB_DATABASE=sample
 DB_USERNAME=root
 DB_PASSWORD=123456


 DB_CONNECTION=mysql1
 DB_HOST=localhost
 DB_PORT=3306
 DB_DATABASE=sample
 DB_USERNAME=root
 DB_PASSWORD=123456

and when you want to call it on your codes as stated by protrafree

Something like this

  DB::connection('mysql')

or

   DB::connection('mysql1')
Pablo
  • 1,357
  • 1
  • 11
  • 40
-2

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');

Syamsoul Azrien
  • 2,534
  • 4
  • 32
  • 55
protrafree
  • 96
  • 10
  • i thought there's an easier way without copy/paste the same configuration.... but if there's no easier way, then i think i will mark your answer as the CORRECT one... – Syamsoul Azrien Mar 03 '20 at 03:50
  • @SyamsoulAzrien There's no easier way than [copy/paste](https://i.imgur.com/uoRcizT.jpg). :P – Tpojka Mar 03 '20 at 11:39