[mod_python] how to use Mysql on mod_python+apache?

Martijn Moeling martijn at xs4us.nu
Thu Mar 29 06:58:55 EST 2007

I don't see the fuzz about mysql either. It is rock solid and I use(d)
it a lot in production environment, together with mysqldb and mod

When using it with mod python there are a few things to be aware of.

First make sure the connection and cursor objects are not created
globally I use:

Def handler(req):
	req.db = MySQLdb.connect( host=Chost, user=Cuser,
passwd=Cpasswd, db=Cdb, use_unicode=True, charset='utf8')
	req.cursor = req.db.cursor()

#next I close the database in a fixup handler



And the closedb function

def closedb(req):
        req.db = None

with this method of using mysqldb, the database connection is closed
properly, even if the handler code runs into an error, if not and an
error occurs and the connection is kept by MySQL and at the end it will
run out of resources (and/or memory whatever comes first).

And to the off topic part:
People seem to blame software when it behaves as designed and they don't
read manuals and/or know background theory, please keep non mod_python
issues out of this list and respect choices made by other people, even
if you think it is a bad one. The question was "How can I use Mysql with

To explain this further, I see people asking questions about MP running
on windows, personally I have the opinion that Microsoft sells crap but
that is since I know about unixes, OpenVMS, MVS etc. etc. and windows.
But I'll never suggest : trow out windows and install xxxxx since the
other person might be more familiar with windows, that is the tool of
choice for him.

Getting started with MP has a steep learning curve, not because MP is
difficult to use, but since a lot of different technologies and
standards apply (file protection, apache, mime types, character sets,
HTML, javascript, xml, htttp (headers!), browser incompatibility,
python, etc, etc. 
I have over 30 years of professional experience and I had a lot to learn
when I started with MP.

I am thinking of publishing parts of my system on a dedicated MP
examples website, since I solved a lot of technology difficulties
(uploading pictures, resizing pictures, pictures stored in Mysql,
characterset conversion, a simple spiderbot, a simple templating system

MP beats a lot of other similar tools but in my eyes there is a lack of
proper simple to understand examples for so called newbie's that are
well documented

Martijn Moeling

-----Oorspronkelijk bericht-----
Van: mod_python-bounces at modpython.org
[mailto:mod_python-bounces at modpython.org] Namens Mike Looijmans
Verzonden: Wednesday, March 28, 2007 12:02 PM
Aan: mod_python at modpython.org
Onderwerp: Re: [mod_python] how to use Mysql on mod_python+apache?

This thread is going way of topic, but I'd really hate to see people
being turned away from MySQL as 
a DB engine just because of the inaccuracies posted here.

I have several years of experience with many DBMSs, including Informix,
Oracle, MS SQLServer, 
Postgress, MySQL and several others. MySQL is my favorite for (non-.NET)
web applications, because 
of its scalability, performance, reliability, low maintenance and ease
of use.

There are many DBMSs, each with their own pecularities, and none of them
I would consider "bad" (or 
even "evil"). Which DBMS to chose for a particular task seems to become
a matter of personal taste 
more everyday, because the more general offerings, like the ones I
mentioned, all provide the same 
rich feature set (e.g. transactions, subqueries, stored procedures,
outer joins). While I would not 
hesitate to recommend in favor of some of the databases I am familiar
with, there is not one that i 
would ever tell people to "avoid at all cost". Even FoxPro has its uses.

> Sorry, but accepting invalid input without error, truncating overly
> input and clipping overflowed numbers are not "quirks".

I agree so far.

>  They are serious data integrity issues.

I disagree here, because the issues you mentioned are configuration
settings in MySQL. You can 
change the behaviour of the server in the config file. It is unfortunate
that the default settings 
are not the ANSI ones. But you can make the MySQL DB comply to the more
general standard of issuing 
an error on overflow and other forms of invalid input.

I suggest reading some documentation before proceeding on the subject.

> That's fine.  At least you aren't really using MySQL in production :-)

I have. On my previous assignment, the customer would loose about $10k
per hour in lost machine time 
only (not even taking the engineers cost into account) if the MySQL
database went offline. It did go 
offline once in that three year period i worked there - the power went
down, the UPS failed and the 
operating system (Solaris) died a horrible dead along with the hard
drive. When the power came up, I 
had its replacement up and running in about ten minutes, and no data was

Mike Looijmans

Mod_python mailing list
Mod_python at modpython.org

More information about the Mod_python mailing list