Apache Mod-Python - mysqldb error

This is Interesting: Free IT Magazines  
Home > Archive > Apache Mod-Python > April 2006 > mysqldb error





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author mysqldb error
Firat KUCUK

2006-04-18, 12:06 am

Hi,
i wrote a simple connection script.
It works as a command line script or cgi script.
But does not work in mod_python.

import MySQLdb
conn = MySQLdb.connect(
host = '127.0.0.1',
user = 'pismikrop',
passwd = 'pass',
db = 'db')
print conn
conn.close()

command-line and cgi output:
<_mysql.connection open to '127.0.0.1' at 81a304c>

mod_python output:
<_mysql.connection open to '(null)' at 82ac504>

i cannot execute mysql queries. What should i do?


Graham Dumpleton

2006-04-18, 12:06 am


On 13/04/2006, at 5:26 AM, Firat KUCUK wrote:

> Hi,
> i wrote a simple connection script.
> It works as a command line script or cgi script.
> But does not work in mod_python.
>
> import MySQLdb
> conn = MySQLdb.connect(
> host = '127.0.0.1',
> user = 'pismikrop',
> passwd = 'pass',
> db = 'db')
> print conn
> conn.close()
>
> command-line and cgi output:
> <_mysql.connection open to '127.0.0.1' at 81a304c>
>
> mod_python output:
> <_mysql.connection open to '(null)' at 82ac504>
>
> i cannot execute mysql queries. What should i do?


Can you supply the remainder of your mod_python handler code
so we can see the context in which this used? Minimal but complete
as possible is preferable.

If the above is truly exactly how you are using it, you probably aren't
using mod_python but a CGI scripts as "print" statement will not
output anywhere in mod_python so you wouldn't be able to capture
it.

Graham

Deron Meranda

2006-04-18, 12:06 am

On 4/12/06, Firat KUCUK <firat@kucuk.org> wrote:
> conn =3D MySQLdb.connect(
> host =3D '127.0.0.1',
> user =3D 'pismikrop',
> passwd =3D 'pass',
> db =3D 'db')


Unrelated to your question, but I see this a lot. You should
not hardcode the database password in your script. If your
script ever fails you may end up with a Python traceback, and
if PythonDebug is enabled (or you're using a framework on
top of mod_python) parts of your source code could be
dumped across the HTML output, including your password!

In web scripts, you should always read any passwords, etc.,
from some other place such as a file.

pw =3D open('.htdbpass','r').readline().strip()
conn =3D MySQLdb.connect(
host =3D '127.0.0.1',
user =3D 'pismikrop',
passwd =3D pw,
db =3D 'db')
--
Deron Meranda

Barry Pearce

2006-04-18, 12:06 am

One extra comment on this...

Firstly make sure you have upgraded to the latest version of MySQLdb - we found earlier versions less than great.

Warning: If you have php installed it uses mysql client libraries which are OUT OF DATE - and will cause failures. Remove the php module from the server and try again.


We use mysql 4.1 servers quite happily from mod_python under both win32 and linux.



Deron Meranda wrote:
> On 4/12/06, Firat KUCUK <firat@kucuk.org> wrote:
>
>
> Unrelated to your question, but I see this a lot. You should
> not hardcode the database password in your script. If your
> script ever fails you may end up with a Python traceback, and
> if PythonDebug is enabled (or you're using a framework on
> top of mod_python) parts of your source code could be
> dumped across the HTML output, including your password!
>
> In web scripts, you should always read any passwords, etc.,
> from some other place such as a file.
>
> pw = open('.htdbpass','r').readline().strip()
> conn = MySQLdb.connect(
> host = '127.0.0.1',
> user = 'pismikrop',
> passwd = pw,
> db = 'db')
> --
> Deron Meranda
>
>


Firat KUCUK

2006-04-18, 12:06 am

Barry Pearce yazmış:
[vbcol=seagreen]
>One extra comment on this...
>
>Firstly make sure you have upgraded to the latest version of MySQLdb - we found earlier versions less than great.
>
>Warning: If you have php installed it uses mysql client libraries which are OUT OF DATE - and will cause failures. Remove the php module from the server and try again.
>
>
>We use mysql 4.1 servers quite happily from mod_python under both win32 and linux.
>
>
>
>
> Deron Meranda wrote:
>
my distro is ubuntu breezy,
I used cgi handler. And text mime type
so i can view the print statement.

MySQL server 4.0.24
apache 2.0.54
python2.4-mysqldb 1.2.1

php, console python, cgi Python works fine.

my .htaccess file

Allow from All

AddHandler mod_python .py
PythonHandler mod_python.cgihandler
PythonDebug On

DirectoryIndex index.htm index.html index.php index.py index.pl

-----------------------------------------------------------------------------

#! /usr/bin/python
# -*- coding: UTF-8 -*-

print 'Content-Type: text/plain\n'

import MySQLdb
conn = MySQLdb.connect(
host = '127.0.0.1',
user = 'pismikrop',
passwd = 'pass',
db = 'gate')
print conn
conn.close()


-------------------------------------------

output:
<_mysql.connection open to '(null)' at 82a97e4>


if host = '10.0.0.6'

Mod_python error: "PythonHandler mod_python.cgihandler"

Traceback (most recent call last):

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

File "/usr/lib/python2.4/site-packages/mod_python/cgihandler.py", line 96, in handler
imp.load_module(module_name, fd, path, desc)

File "/home/pismikrop/vhosts/mikropyuvasi/content/tests/mpcgi/firat.py", line 11, in ?
db = 'gate')

File "/usr/lib/python2.4/site-packages/MySQLdb/__init__.py", line 66, in Connect
return Connection(*args, **kwargs)

File "/usr/lib/python2.4/site-packages/MySQLdb/connections.py", line 134, in __init__
super(Connection, self).__init__(*args, **kwargs2)

OperationalError: (2003, "Can't connect to mysql server on '10.0.0.6' (111)")

------------------------------------------

mysql> SELECT User, Host FROM user;
+------------------+--------------+
| User | Host |
+------------------+--------------+
| pismikrop | % |
| debian-sys-maint | localhost |
| root | localhost |
| root | mikropyuvasi |
+------------------+--------------+

pismikrop user has all priviliges to all databases.



Graham Dumpleton

2006-04-18, 12:06 am

Apache doesn't probably run as any of the users which your database
allows access to. Add lines in your CGI which says:

import os
print os.getuid()

and then see what user that UID actually is and give it access. User
may be something like "apache", "www", "wwwroot" or possibly even
"nobody" depending on the system configuration.

You can also check your Apache configuration for lines:

User www
Group www

to see what it runs as:

Graham

Firat KUCUK wrote ..
> my distro is ubuntu breezy,
> I used cgi handler. And text mime type
> so i can view the print statement.
>
> mysql server 4.0.24
> apache 2.0.54
> python2.4-mysqldb 1.2.1
>
> php, console python, cgi Python works fine.
>
> my .htaccess file
>
> Allow from All
>
> AddHandler mod_python .py
> PythonHandler mod_python.cgihandler
> PythonDebug On
>
> DirectoryIndex index.htm index.html index.php index.py index.pl
>
> -----------------------------------------------------------------------------
>
> #! /usr/bin/python
> # -*- coding: UTF-8 -*-
>
> print 'Content-Type: text/plain\n'
>
> import MySQLdb
> conn = MySQLdb.connect(
> host = '127.0.0.1',
> user = 'pismikrop',
> passwd = 'pass',
> db = 'gate')
> print conn
> conn.close()
>
>
> -------------------------------------------
>
> output:
> <_mysql.connection open to '(null)' at 82a97e4>
>
>
> if host = '10.0.0.6'
>
> Mod_python error: "PythonHandler mod_python.cgihandler"
>
> Traceback (most recent call last):
>
> File "/usr/lib/python2.4/site-packages/mod_python/apache.py", line 299,
> in HandlerDispatch
> result = object(req)
>
> File "/usr/lib/python2.4/site-packages/mod_python/cgihandler.py", line
> 96, in handler
> imp.load_module(module_name, fd, path, desc)
>
> File "/home/pismikrop/vhosts/mikropyuvasi/content/tests/mpcgi/firat.py",
> line 11, in ?
> db = 'gate')
>
> File "/usr/lib/python2.4/site-packages/MySQLdb/__init__.py", line 66,
> in Connect
> return Connection(*args, **kwargs)
>
> File "/usr/lib/python2.4/site-packages/MySQLdb/connections.py", line
> 134, in __init__
> super(Connection, self).__init__(*args, **kwargs2)
>
> OperationalError: (2003, "Can't connect to mysql server on '10.0.0.6' (111)")
>
> ------------------------------------------
>
> mysql> SELECT User, Host FROM user;
> +------------------+--------------+
> | User | Host |
> +------------------+--------------+
> | pismikrop | % |
> | debian-sys-maint | localhost |
> | root | localhost |
> | root | mikropyuvasi |
> +------------------+--------------+
>
> pismikrop user has all priviliges to all databases.


Graham Dumpleton

2006-04-18, 12:06 am

Whoops. I could be talking nonsense here. But then I missed that
your code says 127.0.0.1 yet the error says 10.0.0.6.

FWIW, the reason that I thought to suggest to look at this was that
I was using a database once where using 127.0.0.1 made it use
a local database connection rather than full IP connection and in
that situation, for whatever reason it actually ignored the user name
in the login and was using the user ID of the process connecting
to the database to determine privileges.

I rarely use databases, so could though be completely wrong and
misunderstood what I saw at the time. :-(

Someone who knows what they are talking about should step in
and save me now. :-)

Graham

Graham Dumpleton wrote ..[vbcol=seagreen]
> Apache doesn't probably run as any of the users which your database
> allows access to. Add lines in your CGI which says:
>
> import os
> print os.getuid()
>
> and then see what user that UID actually is and give it access. User
> may be something like "apache", "www", "wwwroot" or possibly even
> "nobody" depending on the system configuration.
>
> You can also check your Apache configuration for lines:
>
> User www
> Group www
>
> to see what it runs as:
>
> Graham
>
> Firat KUCUK wrote ..
> 299,
> (111)")

Deron Meranda

2006-04-18, 12:06 am

> OperationalError: (2003, "Can't connect to mysql server on '10.0.0.6' (11=
1)")

Error 111 is a socket connection refused (ECONNREFUSED). So most
likely this has nothing to do with your db username or password. But
the ip address does look unusual.

First, does the ip 10.0.0.6 mean anything? Is it an address of one of
your interfaces? Use "/sbin/ip addr list" to find out.

Do you have SELinux enabled? Run /usr/sbin/sestatus

Do you have any iptables firewall rules that may be blocking port 3306?
--
Deron Meranda

Firat KUCUK

2006-04-18, 12:06 am

Hi,
SeLinux is not installed.
iptables output is :

mikropyuvasi:~# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination

Chain FORWARD (policy ACCEPT)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source

-----------------------------------------------------

i'll commit suicide

i think it is related with:
http://www.modpython.org/FAQ/faqw.p...e=faq02.013.htp

but i didn't understand.


Graham Dumpleton

2006-04-18, 12:06 am


On 13/04/2006, at 8:33 PM, Firat KUCUK wrote:

> i think it is related with:
> http://www.modpython.org/FAQ/faqw.p...e=faq02.013.htp
>
> but i didn't understand.


If that is the case, it is easy to check. This is done by disabling
the loading
into Apache of php support. If after commenting out the php lines and
doing a "stop/start" of Apache (best not to use "restart" just in
case it doesn't
unloaded shared libraries properly) your problem doesn't go away, then
that FAQ entry is not relevant.

Graham

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com