Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I have an apache server (version Apache/2.2.15) configured to use python script. The script is supposed to connect to database and inserts data to table.

I have written a simple code as below:

Python
import MySQLdb as mdb
db = mdb.connect(host="mysql", user="one", passwd="two", db="test")
cur = db.cursor()
querystring = "INSERT INTO mytable (username, password) value (\"%s\", \"%s\")"%("aaa","bbb")


This code works fine when used from python command terminal, but fails when used from cgi script.

SQL
File "/var/www/cgi-bin/add_user.py", line 39, in <module>
addEntry(prop.text.strip())
File "/var/www/cgi-bin/add_user.py", line 19, in addEntry
  db = mdb.connect(host="203.171.xx.xx", user="usrone", passwd="psdone", db="test", port=3306)
File "/usr/lib64/python2.6/site-packages/MySQLdb/__init__.py", line 81, in Connect
  return Connection(*args, **kwargs)
File "/usr/lib64/python2.6/site-packages/MySQLdb/connections.py", line 187, in __init__
  super(Connection, self).__init__(*args, **kwargs2)
OperationalError: (2003, "Can't connect to MySQL server on '203.171.xx.xx' (13)")



The script file has all permissions (chmod 777) and firewall is disabled in both the pc's
any suggestions as to why this might happen?

Thanks
Posted
Updated 25-May-14 22:27pm
v2

1 solution

Port number? I was able to reproduce this error message on my setup by setting an incorrect port number for the database connection. An incorrect host address also produces it.
 
Share this answer
 
v2
Comments
Member 4686590 6-Jun-14 7:09am    
In my case it is clearly not port number because the scrip works when run from shell.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900