Support the ongoing development of Laravel.io →
Configuration Database

Hello, I have a mysql read replica setup in aws, where I read from one of the databases and write to another, and this works fine in Laravel with the configuration I'll show you next. Now, is there any way to configure Laravel so that when the read database is unavailable it should do the reading from the write database or vice-versa?

My current configuration is something like this:

'mysql' => array(
            'driver'    => 'mysql',
            'read'      => array(
                'host'  => ******.eu-west-1.rds.amazonaws.com'
            ),
            'write'     => array(
                'host'  => '******.eu-west-1.rds.amazonaws.com',
            ),
            'port'      => 3306,
            'database'  => '*****',
            'username'  => '***',
            'password'  => '****',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),

Thanks a lot in advance for your help.

Best regards, Carlos

Last updated 3 years ago.
0

I think you should use multi database connection

'read' => array(
'host'=>'...',
'driver'=>'...'
'port'      => 3306,
'database'  => '*****',
'username'  => '***',
'password'  => '****',
'charset'   => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix'    => ''
),

'write' => array(
'host'=>'...',
'driver'=>'...'
'port'      => 3306,
'database'  => '*****',
'username'  => '***',
'password'  => '****',
'charset'   => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix'    => ''
),

Use for read

DB::connection('read')->table()......

And write

DB::connection('write')->table()......
0

Hello tuyenlaptrinh, thanks for your quick answer. But does this provide a fallback to the other database if one of them is down? For instance, if the read database is down, will it use the write database to do the reading?

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.