CodeIgniter Forums
DB failover settings in .env file? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Installation & Setup (https://forum.codeigniter.com/forumdisplay.php?fid=9)
+--- Thread: DB failover settings in .env file? (/showthread.php?tid=76629)



DB failover settings in .env file? - jameslittle - 06-04-2020

The docs talk about a database 'failover' parameter, which is defined as an array within the main DB connection settings.

Is there a way to define failovers in an .env file? If so, what would that syntax be?

Is it as simple as doing it like?
database.default.failover.hostname = myserver.com


RE: DB failover settings in .env file? - dave friend - 06-04-2020

(06-04-2020, 06:59 AM)jameslittle Wrote: Is it as simple as doing it like?
database.default.failover.hostname = myserver.com

Looks to me like that should work.


RE: DB failover settings in .env file? - jameslittle - 06-04-2020

Did some testing today and sadly it doesn't seem to work. The browser spins for a few seconds and then says "Connection timed out" instead of looking for the failover connection. 

Here's what I've got:

Code:
database.default.hostname = primaryserver.com
database.default.database = mydatabse
database.default.username = myuser
database.default.password = mypass
database.default.DBDriver = MySQLi

database.default.failover.hostname = secondaryserver.com
database.default.failover.database = mydatabase
database.default.failover.username = myuser
database.default.failover.password = mypass
database.default.failover.DBDriver = MySQLi


Any ideas?


RE: DB failover settings in .env file? - michalsn - 06-08-2020

Failover functionality is currently broken because we always throw an exception when there is something wrong with the connection. I prepared a fix for this: https://github.com/codeigniter4/CodeIgniter4/pull/3074 - hopefully it will be accepted soon.

One thing you have to remember when it comes do .env file. You can only override values that are already defined in the original config file - in this case in database.php, so you need to prepare empty variables in failover array.

The other thing is that failover array can have multiple entries so in your .env file it should look something like this:
PHP Code:
database.default.failover.0.hostname secondaryserver.com
database
.default.failover.0.database mydatabase
database
.default.failover.0.username myuser
database
.default.failover.0.password mypass
database
.default.failover.0.DBDriver MySQLi 



RE: DB failover settings in .env file? - jameslittle - 06-09-2020

Thanks so much for looking at that Michalsn! And good info about the .env needing to override existing settings.