How to divide read/write database setting in Laravel?

Question

This is recommended regulation for database divide setting in Laravel.

'mysql' => [
    'read' => [
                  'host' => '192.168.1.1',
              ],
   'write' => [
                  'host' => '196.168.1.2'
              ],
      'sticky'    => true,
      'driver'    => 'mysql',
      'database'  => 'database',
      'username'  => 'root',
      'password'  => '',
      'charset'   => 'utf8mb4',
      'collation' => 'utf8mb4_unicode_ci',
      'prefix'    => '',
  ],  

But if I want to use different access information as DB_NAME, USER_ID, PASS etc to each read/write databases, then how could I make it? Thank you.


Show source
| php   | database   | mysql   | laravel   | setting   2017-09-05 20:09 2 Answers

Answers to How to divide read/write database setting in Laravel? ( 2 )

  1. 2017-09-05 21:09

    You can declare another db conecction like this:

    'mysql' => [
        'read' => [
                      'host' => '192.168.1.1',
                  ],
       'write' => [
                      'host' => '192.168.1.1'
                  ],
          'sticky'    => true,
          'driver'    => 'mysql',
          'database'  => 'database',
          'username'  => 'root',
          'password'  => '',
          'charset'   => 'utf8mb4',
          'collation' => 'utf8mb4_unicode_ci',
          'prefix'    => '',
    ], 
    
    'writecon' => [
        'read' => [
            'host' => '196.168.1.2',
        ],
        'write' => [
            'host' => '196.168.1.2'
        ],
        'driver' => 'mysql',
        'port' => env('DB2_PORT', '3306'),
        'database' => env('DB2_DATABASE', 'db2'),
        'username' => env('DB2_USERNAME', 'somename'),
        'password' => env('DB2_PASSWORD', 'somepass'),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
    ],
    

    Then in your .env add:

    DB2_PORT=3306
    DB2_DATABASE=db2
    DB2_USERNAME=somename
    DB2_PASSWORD=somepass
    

    And you can use it like this:

    $someModel->setConnection('writecon');
    $someModel->save();
    

    not tested but you can try it.

  2. 2017-09-08 10:09

    You can create two connections then specify the connection with Eloquent using the on method:

    'mysql' => [
                'driver' => 'mysql',
                'host' => env('DB_HOST', '127.0.0.1'),
                'port' => env('DB_PORT', '3306'),
                'database' => env('DB_DATABASE', 'forge'),
                'username' => env('DB_USERNAME', 'forge'),
                'password' => env('DB_PASSWORD', ''),
                'unix_socket' => env('DB_SOCKET', ''),
                'charset' => 'utf8mb4',
                'collation' => 'utf8mb4_unicode_ci',
                'prefix' => '',
                'strict' => true,
                'engine' => null,
            ],
    'mysql2' => [
                'driver' => 'mysql',
                'host' => env('DB_HOST2', '127.0.0.1'),
                'port' => env('DB_PORT2', '3306'),
                'database' => env('DB_DATABASE2', 'forge'),
                'username' => env('DB_USERNAME2', 'forge'),
                'password' => env('DB_PASSWORD2', ''),
                'unix_socket' => env('DB_SOCKET2', ''),
                'charset' => 'utf8mb4',
                'collation' => 'utf8mb4_unicode_ci',
                'prefix' => '',
                'strict' => true,
                'engine' => null,
            ],
    


    User::on('mysql2')->where('id', $id)->update($data);
    


    Or using Query Builder:

    DB::connection('mysql2')->table('users')->where('id', $id)->update($data);
    

Leave a reply to - How to divide read/write database setting in Laravel?

◀ Go back