MySQLdb problem with mod_python, please help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ws Wang

    MySQLdb problem with mod_python, please help

    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="localhos t", 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_typ e = "text/html"
    db = MySQLdb.connect (host="localhos t", 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: "PythonHand ler 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="localhos t", user="root",
    passwd="mypass" , db="my_db")

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

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

    File "/usr/lib/python2.2/site-packages/MySQLdb/connections.py" , line
    41, in _make_connectio n
    apply(super(Con nectionBase, 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
  • Damjan

    #2
    Re: MySQLdb problem with mod_python, please help

    > MySQLdb is working fine at command line, however when I tried to use[color=blue]
    > it with mod_python, it give me a "server not initialized" error.[/color]

    Maybe its this problem?


    --
    damjan

    Comment

    • ws Wang

      #3
      Re: MySQLdb problem with mod_python, please help

      Damjan <gdamjan@gmail. com> wrote in message news:<313b0qF36 dhk7U1@uni-berlin.de>...[color=blue][color=green]
      > > 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.[/color]
      >
      > Maybe its this problem?
      > http://www.modpython.org/FAQ/faqw.py...=faq02.013.htp[/color]

      yep! that's the culprit.

      I havent' recompile php, but by removing loadmodule php, mod_python+mysq l worked.

      Thank you very much Damjan.

      Comment

      Working...