Welcome Guest, Not a member yet? Register   Sign In
DB failover settings in .env file?
#1

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
Designer, developer and Diet Dr. Pepper addict. Messing up PHP since <?= $when['year';] ?>
Reply
#2

(This post was last modified: 06-04-2020, 10:56 AM by dave friend.)

(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.
Reply
#3

(This post was last modified: 06-04-2020, 06:50 PM by jameslittle.)

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?
Designer, developer and Diet Dr. Pepper addict. Messing up PHP since <?= $when['year';] ?>
Reply
#4

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 
Reply
#5

Thanks so much for looking at that Michalsn! And good info about the .env needing to override existing settings.
Designer, developer and Diet Dr. Pepper addict. Messing up PHP since <?= $when['year';] ?>
Reply




Theme © iAndrew 2016 - Forum software by © MyBB