Hi, I did something similar,
if "add" in request.function:
db = DAL(['mysql://Database connection string'])
else:
print "Using Slave Database"
db = DAL(shuffle(['database connection string'])
That would and uses the slave database (readonly) but, I get this
error message.
InternalError: (1290, u'The MySQL server is running with the --read-
only option so it cannot execute this statement')
bar this function "add" and "auth" all other functions read data.
I looked around at the DAL code and could not find a way to tell the
DAL that mysql is read only.
any ideas ?
Regards,
Nils
On Jul 22, 1:58 pm, Anthony <[email protected]> wrote:
> You could define 'read_only_actions' anywhere it makes sense, for example:
>
> read_only_actions = ['list','another_read_action','yet_another_read_action']
> if request.function in read_only_actions:
> db =... ...
> else
> db = ....
>
> Of course, if that one 'if' statement is the only place you refer to
> 'read_only_actions', then there's no need to define it as a separate
> variable -- you could just put the list right in the 'if' statement. I think
> 'read_only_actions' was used in the book example just to indicate the idea
> that you would use the slave databases for actions that don't need to write
> to the db -- it's just a stand-in for an actual list of such actions.
>
> Anthony
>
>
>
>
>
>
>
> On Friday, July 22, 2011 6:50:31 AM UTC-4, Nils Olofsson wrote:
> > Hi,
> > I'm still not sure as to how to go about using this.
>
> > Say, I have a controller with the function list: and a function called
> > write:
>
> > if the function is list then read from slaves, if the function is
> > write, write to master.
>
> > So the correct code should be:
>
> > if request.function in read_only_action:
> > db =... ...
> > else
> > db = ....
> > and this is in the model/db.py file.
>
> > I just don't understand where to define read_only_action or it is
> > used ?
>
> > Regards,
>
> > Nils
>
> > On Jul 21, 9:16 pm, Anthony <[email protected]> wrote:
> > > That's just some (incomplete) example code. You have to define
> > > 'read_only_actions' yourself. In that example, it would be a list
> > functions
> > > that only need to read (but not write) the database and can therefore be
> > > given access to one of the slave databases.
>
> > > Actually, it looks like the code has an error -- it should say
> > > request.function, not request.action -- I'll make the change.
>
> > > Anthony
>
> > > On Thursday, July 21, 2011 3:54:20 PM UTC-4, Nils Olofsson wrote:
> > > > Hi,
>
> > > > I did this but i got :
>
> > > > Traceback (most recent call last):
> > > > File "/var/www/web2py/gluon/restricted.py", line 192, in restricted
> > > > exec ccode in environment
> > > > File "/var/www/web2py/applications/Event/models/db.py", line 18, in
> > > > <module>
> > > > if request.action in read_only_actions:
> > > > NameError: name 'read_only_actions' is not defined
>
> > > > <type 'exceptions.NameError'>(name 'read_only_actions' is not defined)
>
> > > > This error is the reason i asked where it should go.
>
> > > > Maybe some one could shed some light on it ?
>
> > > > Regards,
> > > > Nils
>
> > > > On Jul 21, 7:44 pm, Anthony <[email protected]> wrote:
> > > > > It would go in your model file -- the same place where you would
> > normally
>
> > > > > define the db connection.
>
> > > > > Anthony
>
> > > > > On Thursday, July 21, 2011 2:29:45 PM UTC-4, Nils Olofsson wrote:
> > > > > > Hi Massimo,
>
> > > > > > I'm testing amazon's RDS and EC2 , 1 master many slaves.
>
> > > > > > I could not find out where exactly I am suppose to be putting this
> > > > > > code.
>
> > > > > > Regards,
>
> > > > > > Nils
>
> > > > > > On Jul 21, 6:48 pm, Massimo Di Pierro <[email protected]>
> > > > > > wrote:
> > > > > > > You would only use this if you have a replicated database. I.e.
> > you
> > > > > > > are running many database servers synced with each other. For
> > > > example:
> > > > > >http://en.wikipedia.org/wiki/Multi-master_replication
>
> > > > > > > On Jul 21, 12:44 pm, Nils Olofsson <[email protected]> wrote:
>
> > > > > > > > Hi,
>
> > > > > > > > I see this in the Documentation:
>
> > > > > > > > if request.action in read_only_actions:
> > > > > > > > db =
> > > > DAL(shuffle(['mysql://...1','mysql://...2','mysql://...3']))
> > > > > > > > else:
> > > > > > > > db =
> > > > DAL(shuffle(['mysql://...3','mysql://...4','mysql://...5']))
>
> > > > > > > > I'm not sure where exactly I should be using this ?
>
> > > > > > > > And does anyone have some sample code as to how it should be
> > used ?
>
> > > > > > > > Nils