Forums

OperationalError: (MySQLdb.OperationalError) (2005, "Unknown MySQL server host '{xxxxx.mysql.pythonanywhere-services.com}' (-2)")

I am trying to connect python to mysql database use SQLAlchemy engine and after overcoming a password parsing error/issue, I received this issue. I looked at the forums for similar issues and one of the recommendations is to go through BASH and see if I can connect to the server/database that way and I am able to do so.

engine = create_engine ( "mysql+mysqldb://{username}:{password}@{xxxxxx.mysql.pythonanywhere-services.com}:3306/{xxxxxxx$FORECAST_RESULTS}" )

xxxxx is where I changed the hostname for anonymity purposes. Any help would be great. Thank you.

Are you trying to connect to your PythonAnywhere database from code that is not running on PythonAnywhere, but instead on your own computer? If so, you'll need to use an SSH tunnel -- this help page has the details.

No, I am running the script from Pythonanywhere but it's easy enough for me to run it locally on Jupyter Notebook to try SSH route.

I was able to fix the script. The script didn't like the { }

The solution: engine = create_engine ( "mysql+mysqldb://username:password@xxxxxx.mysql.pythonanywhere-services.com:3306/xxxxxxx$FORECAST_RESULTS" )

Ah! Yes, I missed that in your previous message. The curly brackets in the example that you presumably took the original code from are there to say which parts of the string you should replace (or, if you're using an f-string), which parts should be interpolated with the values of variables.