1
votes

When tried to connect to mysql database through python MySQLdb getting following err:

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)")

python code used to connect is as below:

import MySQLdb as mdb
db = mdb.connect(host="203.171.xx.xx", user="usrone", passwd="psdone", db="test", port=3306)

this code is working when used from python command prompt however it fails with above error when used from cgi script.

I have checked the wireshark captures and noticed that command failed without even connecting to remote mysql server.

Apache server version : Apache/2.2.15 I have given all permissions (chmod 777) to python script file.

Any suggestion as to why this might happen?

2
to test the environment written a simple tcp client from cgi script and tried to connect to local db and even that fails with error "Permission denied"!rik

2 Answers

0
votes

I did some troubleshooting around MySQL (with redmine) on Friday :).

Make sure that the MySQL instance has a user that is "allowed" to listen to that port. Mostly you will see the users refer to ::1 or 127.0.0.1 or localhost - or in fact "your client's" computer.

Also under MySQL.ini make sure the remove the line binding it to 127.0.0.1 (just search for 127.0.0.1) and comment out with # (you will know it when you see it). That worked for me.

Hope it helps.

0
votes

It seems that apache did not have permission to connect to mysql

try this :

setsebool -P httpd_can_network_connect_db on