Yii Framework Forum: Database Fail Over - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Database Fail Over Rate Topic: -----

#1 User is offline   DigitalAlien 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 18-December 12

Posted 18 December 2012 - 08:53 AM

Hello all! I am currently working on a project for which we have chosen Yii as our new Framework of choice. I am currently trying to figure out the best way to implement some sort of automatic database fail over in Yii. My thought so far is to over-ride the getDBCOnnection() in CActiveRecord, but I am not sure if this is the correct path to take. Basically what I am looking to do is check a DB connection and if it fails connect to another DB. Simple concept I am just not sure where to put it. I know there are better ways to do this by using mysqlnd_ms but it is not setup on the servers we are using yet so have to come up with a way to do this in Yii. Any help is greatly appreciated. -DA
0

#2 User is offline   DigitalAlien 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 18-December 12

Posted 19 December 2012 - 08:55 AM

Anybody?


View PostDigitalAlien, on 18 December 2012 - 08:53 AM, said:

Hello all! I am currently working on a project for which we have chosen Yii as our new Framework of choice. I am currently trying to figure out the best way to implement some sort of automatic database fail over in Yii. My thought so far is to over-ride the getDBCOnnection() in CActiveRecord, but I am not sure if this is the correct path to take. Basically what I am looking to do is check a DB connection and if it fails connect to another DB. Simple concept I am just not sure where to put it. I know there are better ways to do this by using mysqlnd_ms but it is not setup on the servers we are using yet so have to come up with a way to do this in Yii. Any help is greatly appreciated. -DA

0

#3 User is offline   Da:Sourcerer 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,222
  • Joined: 30-March 11
  • Location:Berlin, Germany

Posted 19 December 2012 - 09:17 AM

Something like HAProxy is not an option? Also I'm pretty sure your approach is a bit off. If I were you, I'd start by extending CDbConnection instead.
programmer /ˈprəʊgramə/, noun: a device that converts ►coffee into ►code
0

#4 User is offline   DigitalAlien 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 18-December 12

Posted 19 December 2012 - 10:27 AM

Hey, thanks so much for the reply.
So, I am not familiar with HAProxy and how it could help me, I will however look into it.

The thing is, we already have servers with stuff on them, I am not in the networking department so load balancing is basically up to them. That being said, our old framework had DB fail over in the code and I have been tasked with doing that same thing in Yii. I need to know how to best approach it from that perspective, but also can recommend better ways if they are out there.
0

#5 User is offline   Da:Sourcerer 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,222
  • Joined: 30-March 11
  • Location:Berlin, Germany

Posted 19 December 2012 - 10:55 AM

HAProxy used to be a tcp/http load balancer. But it is also good for mysql failovers. There's also mysql-proxy, which is operating closer to the MySQL protocol and might be even better (although it's in alpha stage).
programmer /ˈprəʊgramə/, noun: a device that converts ►coffee into ►code
0

#6 User is offline   DigitalAlien 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 18-December 12

Posted 19 December 2012 - 10:58 AM

Thanks! i will check those out.
In the meantime, does anyone have any suggestions for doing this in Yii?
0

#7 User is offline   Da:Sourcerer 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,222
  • Joined: 30-March 11
  • Location:Berlin, Germany

Posted 19 December 2012 - 11:00 AM

View PostDigitalAlien, on 19 December 2012 - 10:58 AM, said:

In the meantime, does anyone have any suggestions for doing this in Yii?

Yes. As I wrote above: Extend CDbConnection so you can specify multiple connection params. I remember somebody implementing split read/write like that. Unfortunately I cannot find that solution again ...
programmer /ˈprəʊgramə/, noun: a device that converts ►coffee into ►code
0

#8 User is offline   DigitalAlien 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 18-December 12

Posted 19 December 2012 - 11:17 AM

I did see something like that somewhere also, but it was changing core Yii stuff and also not exactly what I am looking for, I don't think.
So, if I took the route of extending CDbConnection, I am not sure exactly what methods I would change. I wouldn't know where to begin honestly. Where in CDbConnection does it get the config variable and connect to the DB? I assume that is where I would say "test connection" if it can't connect try the other.
0

#9 User is offline   DigitalAlien 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 18-December 12

Posted 24 December 2012 - 08:16 AM

So this is what I have come up with so far. I'd love to get some thoughts or suggestions on whether this is the correct approach.

class DaDbConnection extends CDbConnection{

public $dbConnectTries = 6;
public $numDatabases = 3;
private $_tries =0;
private $_db = 1;

/*
 * Extends CDbConnection open() method
 * Tries to connect to database connections setup in config/main.php up to 
 * the value of $dbConnectionTries or a connection is successful
 * @throws CException If it can not connect to any DBs
 */
protected function open()
{
    try{
        //try to connect to the default DB
        parent::open();

    }catch(Exception $e){
        if($this->_tries < $this->dbConnectTries){

            //If there aren't anymore DBs to try we must start over from the first
            if($this->_db >= $this->numDatabases){
                $tryDb = 'db';
                $this->_db = 0;
            }else{
               $tryDb = 'db'.$this->_db;
            }

            $this->_db++;
            $this->_tries++;

            $this->connectionString = Yii::app()->$tryDb->connectionString;
            $this->username = Yii::app()->$tryDb->username;
            $this->password = Yii::app()->$tryDb->password;
            $this->open();
        }else{
            throw new CDbException('Could Not Connect to a DB.');
        }
    }    
}

0

#10 User is offline   Da:Sourcerer 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,222
  • Joined: 30-March 11
  • Location:Berlin, Germany

Posted 24 December 2012 - 01:35 PM

You do not happen to have read this blog post recently? Anyway: In the Yii world, this does not look like a sound approach.
programmer /ˈprəʊgramə/, noun: a device that converts ►coffee into ►code
0

#11 User is offline   DigitalAlien 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 18-December 12

Posted 26 December 2012 - 08:58 AM

I am fairly new to Yii, so I am confused on what the Yii approach would look like. I really hope someone could point me in the correct direction more directly. I looked at that blog post, but it doesn't look to be specifically for Yii I will look at it more closely to see if it would be of any help.

Thanks!
0

#12 User is offline   Maxxer 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 75
  • Joined: 23-February 11
  • Location:Lecco (LC) - Italy

Posted 14 February 2013 - 04:17 PM

hi. did you manage to solve your issue? I'm interested too...

thanks
YetOpen S.r.l. - Your Open Source Partner
Lecco - Italy
http://www.yetopen.it
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users