1

I am working with MySQL-python package. I am able to execute MySQL dependent scripts from command line, however doing the same through browser (Apache CGI) yields the following error:

A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred.  
/var/www/html/temp.py in ()  
9 
10 # Establich a connection
11 db = MySQLdb.connection(host="127.0.0.1", user="root", passwd="", db="inserv")
12 
13 # Run a MySQL query from Python and get the result set  
   db undefined, MySQLdb = <module 'MySQLdb' from 
  '/usr/lib64/python2.6/site-packages
/MySQLdb/__init__.pyc'>, MySQLdb.connection = <type '_mysql.connection'>,  
host undefined,   user undefined, passwd undefined

<class '_mysql_exceptions.OperationalError'>: 
(2003, "Can't connect to MySQL server on '127.0.0.1' (13)")
  args = (2003, "Can't connect to MySQL server on '127.0.0.1' (13)")
  message = '' 

I have been stuck in this situation for past few days. MySQL commands issued through php based sites execute appropriately and I can, also, login to MySQL from command line. The problem seem to be with Python CGI only.

I have also tried the same with oursql package and there seems to be a similar problem. How can I address this situation?

Edit
As per @Real's answer I have edited my code to use MySQLdb.connect() but the problem still persist and traceback ends with:

2003, "Can't connect to MySQL server on '127.0.0.1' (13)"

1 Answer 1

1

You should be using connect, not connection. Mysqldb.connect() returns a connection object but you appear to be calling mysqldb.connection(). See The docs for an example of how to do it.

Sign up to request clarification or add additional context in comments.

7 Comments

I have changed my code as per your answer, but the issue persists. (I have updated the question)
Perhaps look at the MySQL logs. I'd also verify that MySQL is indeed listening to TCP Port on local host. It is possible your other scripts are using the UNIX socket instead of a TCP connection.
Fair enough connecting through socket (using localhost instead of 127.0.0.1) works for the CGI script but fails with SqlALchemy create_engine
The way I read docs.sqlalchemy.org/en/rel_0_7/dialects/mysql.html it doesn't support UNIX sockets, and expects a host/port combo. I suspect that it uses the default port and tries TCP. This answer may help: stackoverflow.com/questions/6245813/…
I have nmap(ed) port 3306 and MySQL is listening to it. AFAIK SqlAlchemy connects to sockets with the following string in DSN: mysql://....?unix_socket=<path to mysql.sock>.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.