Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Help req: Problems with MySQLdb

Reply
Thread Tools

Help req: Problems with MySQLdb

 
 
rodmc
Guest
Posts: n/a
 
      06-22-2006
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

 
Reply With Quote
 
 
 
 
Sybren Stuvel
Guest
Posts: n/a
 
      06-22-2006
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
 
Reply With Quote
 
 
 
 
rodmc
Guest
Posts: n/a
 
      06-22-2006
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


 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      06-22-2006
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('@')])"
 
Reply With Quote
 
Sybren Stuvel
Guest
Posts: n/a
 
      06-22-2006
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
 
Reply With Quote
 
Simon Forman
Guest
Posts: n/a
 
      06-22-2006
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

 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      06-22-2006
On 22 Jun 2006 02:50:04 -0700, "rodmc" <userprogoogle->
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: web-)
HTTP://www.bestiaria.com/
 
Reply With Quote
 
timw.google
Guest
Posts: n/a
 
      06-22-2006
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/...ing-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.
>


 
Reply With Quote
 
ben.hamilton@gmail.com
Guest
Posts: n/a
 
      06-23-2006
for x in range(self.MAX_CRAWL_THREADS+1):
self.con.append(
[MySQLdb.connect(host,username,passwor,database,POR T),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.

 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      06-23-2006
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('@')])"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
MySQLdb problems with named pipe connection on Windows 7? John Nagle Python 4 06-12-2010 04:44 AM
Confusing, desparate MySQLdb problems... stopchuckingstuff Python 13 01-17-2010 07:41 PM
Problems installing MySQLdb on Windows [newbie] Alex Meier Python 5 12-29-2004 01:21 PM
MySQLdb problem with mod_python, please help ws Wang Python 2 11-30-2004 08:40 PM
Importing MySQLdb module causes malformed header error.. pls help!!! Kiran B. Python 0 11-05-2003 01:52 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57