Configure Master-Slave Database Replication

If you're having troubles with your database, due to the number of requests and your server is becoming unresponsive, a good idea may be to switch from a simple database configuration to a master-slave replication.

This way Laravel will use a database just for SELECT, which should be most common, and another one for INSERT, UPDATE, and DELETE statements.

How to setup master-slave database replication

Laravel makes this just a breeze, as you don't need any extra code. The only thing you have to do is change the database configuration, which can be found in app/config/database.php.

Below you can see an example configuration for master-slave replication:

'mysql' => [
    'read' => [
        'host' => '192.168.1.1',
    ],
    'write' => [
        'host' => '196.168.1.2'
    ],
    'driver'    => 'mysql',
    'database'  => 'database',
    'username'  => 'root',
    'password'  => '',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
],

You just have to enter your database credentials and the ip address of each of the two databases. You've probably figured it our already that read will be used for SELECT, while write is for INSERT, UPDATE, and DELETE.