Forums

MySQL connect

I want to remotely connect to a MySQL db on pythonanywhere. The code that I have works for remote connection to my db on AWS:

server = "somelongstring.rds.amazonaws.com"
pwd = "mypassword"
dbname = "mydatabase"
user = "myusername"

cnxn = MySQLdb.connect(server, user, pwd, dbname, charset='utf8', use_unicode=True)

Is there something similar I can use to connect remotely to a MySQL db on PythonAnywhere.

Thanks.

(I have an email in about my headscratching on this, but thought I should posts here too)

That should work fine from a paid PythonAnywhere account like yours. I see that you're using Amazon RDS, though, so one thing that might cause confusion is the security settings on the RDS instance -- you'll need to make sure that the IP addresses of the PythonAnywhere cluster have access. We don't have a fixed set of IPs that we use, so unfortunately you'd probably have to open up access to all AWS internal IPs, ie. 10.0.0.0/8.

Giles,

Thanks, but I don't think that answers my question.

That code does work fine on PythonAnywhere, connecting to AWS. No problem.

What I want to know is

Is there something similar I can use to connect remotely to a MySQL db resident on PythonAnywhere. ?

If I have python code running on a server somewhere (not PA) that connects to a PythonAnywhere MySQL db, what would the MySQLdb.connect parameters be?

This may well all be really obvious, but this is the first web app I have written.

Ah, I'm sorry, I misunderstood.

Doing it that way is a little more tricky, but also should be doable from outside PythonAnywhere for paid accounts. What you need to do is set up an "ssh tunnel" on the remote machine that, on the remote machine, looks like a MySQL server, but is actually just a proxy connecting back through PythonAnywhere's ssh server to the PythonAnywhere MySQL DB. There are instructions on how to do that here, in the second "Manual SSH tunnelling" section.

Thanks. Have now managed to connect to my PythonAnywhere db from a remote python script.

Excellent, thanks for confirming!