Can Connect to Database via Command Line but not in Python Script?

1424 views python

I hope the title is pretty self explanatory. I set up a database and web-server on the same machine using Amazon RDS and EC2 instance. I am running a Python script in the machine's cgi folder, and am having trouble connecting to the database. The errors are on the order of: _mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on '' (111)")

I have tried this with mySQLdb and _mysql without success. What I can't understand is that I am able to successfully connect to the mysql client via the command line with mysql -u username -p -h but not inside the script.

This is what I have tried in the Python script

db = _mysql.connect("","st4rgut25","pwd","st4rgut25")


db = MySQLdb.connect(host="",user="st4rgut25",passwd="pwd",db="st4rgut25")

answered question

1 Answer


The RDS instance is not running on the EC2 instance, they're separate "machines". From the EC2 instance, instead of using the loopback address, which would assume MySQL is running on the local EC2 instance, just use the host name as you're doing from the MySQL client.

posted this

Have an answer?


Please login first before posting an answer.