MySQLdb problem with mod_python, please help

ws Wang wenshengwang at gmail.com
Sat Nov 27 02:45:12 EST 2004


MySQLdb is working fine at command line, however when I tried to use
it with mod_python, it give me a "server not initialized" error.

This is working fine:
----------------------- testmy.py -------------------------------
#!/usr/bin/python
import MySQLdb
db = MySQLdb.connect(host="localhost", user="root", passwd="mypass",
db="my_db")
cursor = db.cursor()
cursor.execute("SELECT * FROM parks")
result = cursor.fetchall()
for record in result:
        print record[0] , "-->", record[1]
------------------------------------------------------------------
when I type "testmy.py", it give me correct results. (I run as root)

BUT,
This:
------------------ index.py --------------------
import sys, os, MySQLdb
from mod_python import apache
def handler(req):
        sys.stdout = req
        req.content_type = "text/html"
        db = MySQLdb.connect(host="localhost", user="root",
passwd="mypass", db="my_db")
        cursor = db.cursor()
        cursor.execute("SELECT * FROM parks")
        result = cursor.fetchall()
        for record in result:
                print record[0] , "-->", record[1]
        return apache.OK
---------------------------------------------------

When I tried to open it from the browser, it give me following error
message:
--------------------------------------------------
Mod_python error: "PythonHandler index"

Traceback (most recent call last):

  File "/usr/lib/python2.2/site-packages/mod_python/apache.py", line
299, in HandlerDispatch
    result = object(req)

  File "/var/www/html/index.py", line 6, in handler
    db = MySQLdb.connect(host="localhost", user="root",
passwd="mypass", db="my_db")

  File "/usr/lib/python2.2/site-packages/MySQLdb/__init__.py", line
64, in Connect
    return apply(Connection, args, kwargs)

  File "/usr/lib/python2.2/site-packages/MySQLdb/connections.py", line
116, in __init__
    self._make_connection(args, kwargs2)

  File "/usr/lib/python2.2/site-packages/MySQLdb/connections.py", line
41, in _make_connection
    apply(super(ConnectionBase, self).__init__, args, kwargs)

InternalError: (-1, 'server not initialized')
-----------------------------------------------------------

My apache setting for mod_python is:
   AddHandler mod_python .py
   PythonHandler index
   PythonDebug On

mod_python works on my site, if I replace the whole database stuff
with just (print "hello, world"), I get "hello, world" on the browser.
apache is run as "apache/apache"

What did I do wrong? (Is this a priviledge problem or something else?)

Any help is greatly appreciated.

Wensheng



More information about the Python-list mailing list