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
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()......
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?
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community