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

1424 views python
4

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 '127.0.0.1' (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 edutechfinal.cqk0lckbky4e.us-east-2.rds.amazonaws.com but not inside the script.

This is what I have tried in the Python script

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

and

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

answered question

1 Answer

12

The RDS instance is not running on the EC2 instance, they're separate "machines". From the EC2 instance, instead of using the loopback address 127.0.0.1, which would assume MySQL is running on the local EC2 instance, just use the host name edutechfinal.cqk0lckbky4e.us-east-2.rds.amazonaws.com as you're doing from the MySQL client.

posted this

Have an answer?

JD

Please login first before posting an answer.