DEV Community

mark vachi
mark vachi

Posted on • Updated on

Laravel connect multiple database server

  1. Replication database in mariadb and set Database Read-only | mariadb-replication-easy-step
  2. set up Laravel connect multiple database server | Read & Write Connections

    # config/database.php
    'mysql' => [
        'read' => [
            'host' => [ '192.168.1.1', '196.168.1.2' ],
        ],
        'write' => [
            'host' => ['196.168.1.2'],
        ],
        'sticky'    => true,
        ...
    ],
    
  3. Check, What's Laravel connect server database id.

    Route::get('/mysql-test', function () {
      $places = App\Place::all();
      $results = DB::select( DB::raw("SHOW VARIABLES LIKE 'server_id'") );  
      return "Server ID: " . $results[0]->Value;
    });
    

Discussion (0)