Check if can connect to database |
[eluser]tim peterson[/eluser]
I'd like to backup my read replica(i.e., slave) database with my master database but this simple boolean I did failed: Code: $config['hostname'] = "myReadReplicaDatabase.com"; After terminating my read replica database I expected the boolean to evaluate to `FALSE` and the script to then use my master database. Unfortunately, instead I got the following PHP error: Code: Unable to connect to your database server using the provided settings. All i want is for the connection to return true or false, does anyone know how to do this in Codeigniter?
[eluser]yacman[/eluser]
Try changing this setting in your /application/config/[env]/database.php Code: ...
[eluser]tim peterson[/eluser]
Hi yacman, Setting Code: 'db_debug'=FALSE Might I ask how to change the if clause (below) to accomplish this? Code: if(!$db_obj){ thanks, tim
[eluser]yacman[/eluser]
Since you want to know the response to the intialize, I'd recommend turning off auto initialize to false. Code: $db['xxx']['autoinit'] = FALSE; Then in your code that checks the db state: Code: $db_obj = $this->database->load('xxx',TRUE);
[eluser]tim peterson[/eluser]
I spoke too soon, the `initialize()` function is returning FALSE regardless of whether or not the read replica exists. So that doesn't work. thoughts?
[eluser]yacman[/eluser]
In your config, I'd recommend turning on logging and then tailing that log file. If initialize() is returning false, then the db is failing to initialize. This means there is something incorrect in your configuration. Have you ever successfully connected to the db before using CI?
[eluser]tim peterson[/eluser]
yes, i can connect to the db using CI just fine. My problem is simply that I can't switch the config to go from one database to the other in the event of the first one failing to connect.
[eluser]yacman[/eluser]
Can you post some more of your code? You said that database initialize is false. Where is '$this->CI' being instantiated? Have you tried putting this logic into the model file like: Code: function __construct() { In your library you'd then just use: Code: $this->CI->load->model('a_model'); *edit: In reference to above,I would re-enable autoinit in the DB config.
[eluser]tim peterson[/eluser]
My master database is autoloaded since it is used by many models throughout my application. I'd like to use the read replica database in some of the models for SELECT/read-only scenarios. Here's my entire read replica config which, like my master database, i'm autoloading as a library : https://gist.github.com/3749863. Therefore in a Model I normally have this code: Code: $query=$this->db->query("SELECT * WHERE... And for read-only scenarios I'd like to do this: Code: $query=$this->readreplica->load->query("SELECT * WHERE ... The trick is that if my read replica database fails to connect I'd like to fall back to using $this->db (my master database config). My master database already has failover protection since I'm using Amazon RDS.
[eluser]yacman[/eluser]
Looks good, so in those models that you want to use the replica db for read only, Code: class a_model extends CI_Model |
Welcome Guest, Not a member yet? Register Sign In |