6

How to configure Laravel 5.5 with master slave MySQL replication ?

I want to make write operations and read operations in master and slave respectively .

Optional: Is there any way to do connection pooling and max / min no of open connections in ideal conditions. ?

Subhajit
  • 876
  • 3
  • 17
  • 37

1 Answers1

14

Just change your config/database.php file to include read (slave) and write (master) hosts like so like the Laravel docs suggest:

'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'    => '',
],
Paras
  • 9,258
  • 31
  • 55
  • 1
    What happened if there isn't internet connection and master would be on live, Will it run automatically when gets an internet connection? – naCheex Jul 15 '18 at 20:34
  • separating read and write mode is not the concept of master-slave – fmchan Nov 27 '18 at 09:48
  • 1
    @fmchan then what is the concept of master-slave? – Paras Nov 27 '18 at 09:49
  • 4
    Pretty sure that separating read and write mode is a major part of the concept of master-slave. You want to read the local data, but write to the master server. If you read and write to the master server only, then why are you replicating the data in the first place? – Skeets May 16 '19 at 10:38