Help req: Problems with MySQLdb

R

rodmc

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
 
S

Sybren Stuvel

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
 
R

rodmc

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
 
B

Bruno Desthuilliers

rodmc wrote:
(top-post corrected)
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.
 
S

Sybren Stuvel

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
 
S

Simon Forman

rodmc said:
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
 
D

Dennis Lee Bieber

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
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
T

timw.google

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.
 
B

ben.hamilton

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 said:
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.
 
B

Bruno Desthuilliers

Simon said:
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.
 
S

Simon Forman

Bruno said:
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.
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,001
Messages
2,570,250
Members
46,848
Latest member
Graciela Mitchell

Latest Threads

Top