Help req: Problems with MySQLdb

Discussion in 'Python' started by rodmc, Jun 22, 2006.

  1. rodmc

    rodmc Guest

    I have written an application that connects to a database on a remote
    machine which uses MySQLdb 1.2.0. The application works perfectly when
    connecting to the database from a remote machine, however when it
    attempts to connect to a database on the same machine a connection
    error is generated. I have attached what little info I can below.

    DBSERVERIP = "1.2.3.4"
    db = MySQLdb.connect(host=DBSERVERIP, user="user", passwd="password",
    db="nuke")
    --- it refuses to connect on the above line and the exception is caught
    and a message displayed.

    I have tried changing the server IP to "localhost" or the hostname,
    however the same problem arises.

    Information: Python 2.3.5, MySQLdb 1.2.0, MySQL 5.0.21 and Windows 2K
    pro.

    I would be grateful for any help with this problem.

    Kind regards,

    rod
     
    rodmc, Jun 22, 2006
    #1
    1. Advertising

  2. rodmc enlightened us with:
    > --- it refuses to connect on the above line and the exception is
    > caught and a message displayed.


    So.... why do you think this exception and the error message contain
    no useful information at all?

    Sybren
    --
    The problem with the world is stupidity. Not saying there should be a
    capital punishment for stupidity, but why don't we just take the
    safety labels off of everything and let the problem solve itself?
    Frank Zappa
     
    Sybren Stuvel, Jun 22, 2006
    #2
    1. Advertising

  3. rodmc

    rodmc Guest

    Hi,

    Thanks for your email. Well I am kind of new to exceptions in Python,
    but here is the code used below, as you can see it is somewhat basic.
    Is there a way to display more information about the exception?

    Best,

    rod



    try: #Exception handler for database queries
    db = MySQLdb.connect(host=DBSERVERIP, user="user",
    passwd="password", db="nuke")
    except:
    print "A database connection error has occurred"
    return False
    else:
    #The rest of the program

    Sybren Stuvel wrote:
    > rodmc enlightened us with:
    > > --- it refuses to connect on the above line and the exception is
    > > caught and a message displayed.

    >
    > So.... why do you think this exception and the error message contain
    > no useful information at all?
    >
    > Sybren
    > --
    > The problem with the world is stupidity. Not saying there should be a
    > capital punishment for stupidity, but why don't we just take the
    > safety labels off of everything and let the problem solve itself?
    > Frank Zappa
     
    rodmc, Jun 22, 2006
    #3
  4. rodmc wrote:
    (top-post corrected)
    > Sybren Stuvel wrote:
    >
    >>rodmc enlightened us with:
    >>
    >>>--- it refuses to connect on the above line and the exception is
    >>>caught and a message displayed.

    >>
    >>So.... why do you think this exception and the error message contain
    >>no useful information at all?



    > Hi,
    >
    > Thanks for your email. Well I am kind of new to exceptions in Python,
    > but here is the code used below, as you can see it is somewhat basic.
    > Is there a way to display more information about the exception?


    Yes : don't catch it. You'll then have all the needed infos.

    If you want to catch it so you can do some logging, issue a more
    user-friendly error message etc, then do something like this:

    try:
    SomethingThatMayRaise()
    except ClassOfExceptedException, e:
    # e is the exception, let you access error message, traceback etc
    doSomethingWithException(e)


    Some general rules about exception handling:
    - *don't* use bare except clause. Never. Well, almost never (cf below)
    - if you can't fix the problem, just let the exception propagate
    - at the top level of the main program, have a catch-almost-all
    exception handler, that will do logging if possible, proper error
    reporting if possible, sanity clean-up if possible, and then crash as
    noisily as possible.

    >
    > try: #Exception handler for database queries
    > db = MySQLdb.connect(host=DBSERVERIP, user="user",
    > passwd="password", db="nuke")
    > except:
    > print "A database connection error has occurred"
    > return False


    This is the most useless and worst possible way to handle exceptions.
    Just get rid of this exception handler - letting the program crash with
    full traceback would be much much better - at least you'd have a chance
    to get some usefull informations about what went wrong.


    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in ''.split('@')])"
     
    Bruno Desthuilliers, Jun 22, 2006
    #4
  5. rodmc enlightened us with:
    > Thanks for your email. Well I am kind of new to exceptions in
    > Python, but here is the code used below, as you can see it is
    > somewhat basic.


    It is bad style to catch all exceptions and then only display a
    message "A database connection error has occurred". Remove the try:
    line, and the except: block, and just let Python throw the exception.

    > Is there a way to display more information about the exception?


    Yeah, don't catch all exceptions, replacing them with a non-informing
    message.

    I suggest you (re)read the tutorial chapter about exception handling.
    It contains quite useful information.

    Sybren
    --
    The problem with the world is stupidity. Not saying there should be a
    capital punishment for stupidity, but why don't we just take the
    safety labels off of everything and let the problem solve itself?
    Frank Zappa
     
    Sybren Stuvel, Jun 22, 2006
    #5
  6. rodmc

    Simon Forman Guest

    rodmc wrote:
    > Hi,
    >
    > Thanks for your email. Well I am kind of new to exceptions in Python,
    > but here is the code used below, as you can see it is somewhat basic.
    > Is there a way to display more information about the exception?
    >
    > Best,
    >
    > rod
    >


    Use the traceback module (See
    http://docs.python.org/lib/module-traceback.html for info on it.)

    import traceback

    try:
    db = MySQLdb.connect(host=DBSERVERIP, user="user",
    passwd="password", db="nuke")
    except:
    print "A database connection error has occurred"
    traceback.print_exc()
    return False
    else:
    pass

    #The rest of the program


    It's generally very difficult to figure out what's going wrong without
    the traceback in front of you.

    Also, try an empty string (i.e. "") as your hostname, it's shorthand
    for 'localhost'.


    Hope this helps,
    ~Simon
     
    Simon Forman, Jun 22, 2006
    #6
  7. On 22 Jun 2006 02:50:04 -0700, "rodmc" <>
    declaimed the following in comp.lang.python:


    >
    > try: #Exception handler for database queries
    > db = MySQLdb.connect(host=DBSERVERIP, user="user",
    > passwd="password", db="nuke")
    > except:
    > print "A database connection error has occurred"
    > return False


    Oh, now that is really informative... You don't display any of the
    exception parameters -- not even the name. Removing the try/except would
    be more useful...

    However...

    Is the MySQL security system configured to permit specified user to
    connect from the server machine itself? Is there a firewall that may
    have the MySQL port open to the outside world but not on the loopback
    (localhost) connection?
    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
     
    Dennis Lee Bieber, Jun 22, 2006
    #7
  8. rodmc

    timw.google Guest

    Do you have a MySQL acccount set up on the localhost? I usually create
    two users with the same privs. One for the localhost where the server
    is, another to connect from somewhere else.

    Something like

    mysql> grant usage on *.* to 'user'@'localhost' identitfied by
    'some_pass';
    mysql> grant usage on *.* to 'user'@'%' to identified by 'some_pass';


    Check out

    http://dev.mysql.com/doc/refman/4.1/en/adding-users.html

    Hope this helps.

    rodmc wrote:
    > I have written an application that connects to a database on a remote
    > machine which uses MySQLdb 1.2.0. The application works perfectly when
    > connecting to the database from a remote machine, however when it
    > attempts to connect to a database on the same machine a connection
    > error is generated. I have attached what little info I can below.
    >
    > DBSERVERIP = "1.2.3.4"
    > db = MySQLdb.connect(host=DBSERVERIP, user="user", passwd="password",
    > db="nuke")
    > --- it refuses to connect on the above line and the exception is caught
    > and a message displayed.
    >
     
    timw.google, Jun 22, 2006
    #8
  9. rodmc

    Guest

    for x in range(self.MAX_CRAWL_THREADS+1):
    self.con.append(
    [MySQLdb.connect(host,username,passwor,database,PORT),0])

    PORT is an extra argument you might not have perhaps
    rodmc wrote:
    > I have written an application that connects to a database on a remote
    > machine which uses MySQLdb 1.2.0. The application works perfectly when
    > connecting to the database from a remote machine, however when it
    > attempts to connect to a database on the same machine a connection
    > error is generated. I have attached what little info I can below.
    >
    > DBSERVERIP = "1.2.3.4"
    > db = MySQLdb.connect(host=DBSERVERIP, user="user", passwd="password",
    > db="nuke")
    > --- it refuses to connect on the above line and the exception is caught
    > and a message displayed.
    >
    > I have tried changing the server IP to "localhost" or the hostname,
    > however the same problem arises.
    >
    > Information: Python 2.3.5, MySQLdb 1.2.0, MySQL 5.0.21 and Windows 2K
    > pro.
    >
    > I would be grateful for any help with this problem.
    >
    > Kind regards,
    >
    > rod


    Also not catching exceptions is a bad idea (crash thud) for long term
    programs short term testing its possible it could be good.
     
    , Jun 23, 2006
    #9
  10. Simon Forman wrote:
    > rodmc wrote:
    >
    >>Hi,
    >>
    >>Thanks for your email. Well I am kind of new to exceptions in Python,
    >>but here is the code used below, as you can see it is somewhat basic.
    >>Is there a way to display more information about the exception?
    >>
    >>Best,
    >>
    >>rod
    >>

    >
    >
    > Use the traceback module (See
    > http://docs.python.org/lib/module-traceback.html for info on it.)
    >
    > import traceback
    >
    > try:
    > db = MySQLdb.connect(host=DBSERVERIP, user="user",
    > passwd="password", db="nuke")
    > except:
    > print "A database connection error has occurred"


    How can you assert it is a database connection error ?

    > traceback.print_exc()
    > return False
    > else:
    > pass
    >
    > #The rest of the program


    You get the same result - with a more accurate error message - by not
    handling the exception at all.

    >
    > It's generally very difficult to figure out what's going wrong without
    > the traceback in front of you.


    indeed.


    > Also, try an empty string (i.e. "") as your hostname, it's shorthand
    > for 'localhost'.
    >
    >
    > Hope this helps,
    > ~Simon
    >



    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in ''.split('@')])"
     
    Bruno Desthuilliers, Jun 23, 2006
    #10
  11. rodmc

    Simon Forman Guest

    Bruno Desthuilliers wrote:
    > Simon Forman wrote:
    > > rodmc wrote:

    ....
    > > except:
    > > print "A database connection error has occurred"

    >
    > How can you assert it is a database connection error ?


    assert "database connection" in error

    (just kidding)

    I was really just leaving as much of the OP's code unchanged as seemed
    useful. Maybe they *want* to print that (possibly inaccurate) message
    and return False (from what appears to be module-level code at that!)

    FWIW, if I wanted the code to keep going but still report the exception
    I would use the logging module's exception() method. But that seemed a
    lot to explain.

    >
    > > traceback.print_exc()
    > > return False
    > > else:
    > > pass
    > >
    > > #The rest of the program

    >
    > You get the same result - with a more accurate error message - by not
    > handling the exception at all.
    >


    You'd get the same traceback printed out either way, but the generic
    except statement with the possibly inaccurate message does seem naive
    to me. However, it's entirely possible that the OP *would* like the
    code to keep running despite any exceptions while trying to connect to
    the db. In that case, using traceback (or logging) is (IMHO) a
    reasonable way to do that but still find out what went wrong.

    Ciao,
    ~Simon
     
    Simon Forman, Jun 23, 2006
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Elliott
    Replies:
    0
    Views:
    370
    David Elliott
    Oct 15, 2003
  2. Joshua
    Replies:
    8
    Views:
    6,057
    Joshua
    Sep 29, 2004
  3. Alex Meier
    Replies:
    5
    Views:
    1,497
    Steve Holden
    Dec 29, 2004
  4. stopchuckingstuff

    Confusing, desparate MySQLdb problems...

    stopchuckingstuff, Jan 16, 2010, in forum: Python
    Replies:
    13
    Views:
    805
    Ned Deily
    Jan 17, 2010
  5. John Nagle
    Replies:
    4
    Views:
    1,712
    Dennis Lee Bieber
    Jun 12, 2010
Loading...

Share This Page