Forums

Connect to a MySQL database hosted outside of PythonAnywhere

I'm on the free tier of PythonAnywhere as I evaluate it for my small app.

I'm trying to connect a Flask App with SqlAlchemy to a MySQL database I have stored on another hosting service outside of PythonAnywhere. Any connection I attempt gets a long traceback error which appears to be cut off at 8,024 characters.

I've whitelisted PythonAnywhere.com on my hosted MySQL server and can access my database locally via the deployed app when ran as localhost.

Has anyone else attempted this before? I thought I saw in the forums that I might need to upgrade to at least the Hacker tier but it seemed inconclusive . Thank you for any insights!

Yes, AFAIK you need any type of paid account to be able to make connections to anything outside. Except for whitelisted (by PA stuff, for all users) external sites that have public API and only http/s protocol ports.

im using database that provided in pythonanywhere, i want to connect to java application using netbeans, but i have connection error, why does this happen? im using database in pythonanywhere for being manage by django admin .

@Astronoms - Thank you for the guidance. I'll upgrade and give it a shot. I appreciate the reply!

Okay, I have this resolved but I'm not a big fan.

Instead of whitelisting %.pythonanywhere.com, I had to whitelist %.amazonaws.com which is what our apps are being hosted on. I don't exactly like this solution since that opens my database up to anyone on aws to attempt to talk to it but it's working.

If I missed something, I'd appreciate any refinement.

I'm using Flask, SQLAlchemy and the pymysql package since I'm on Python 3.6 and using a virtualenv to install these modules.

Analyticsinc, you could try different steps to secure this, like:

  • with firewall rules forward another port to default 3306 and/or add allow/block rules for IP addresses OR at least change the port number MySQL daemon is listening on (tho this will not stop sniffers);
  • setup SSH tunnel from PythonAnywhere to MySQL host, you may need sshtunnel - read here;
  • enable SSL encryption in MySQL daemon for certain users and connect with cert

I have not done any of this myself, so can't provide any further help! :)

P.S. Some of them require you to have access to the whole system on your MySQL hosting service.

Another option that you can use: If you can update the whitelist through an API, you can have your code on PythonAnywhere update the whitelist when it starts. It can lookup the ip address it has just started on (using ipify or something similar) and add it to the whitelist.

Hello, I am trying to connect to the MySQL DB outside of PythonAnywhere using python via PyCharm (Community edition, not Professional edition). My python script connects well within PythonAnywhere but the same script doesn't work locally in PyCharm (I am using instructions here to connect through the SSH tunnel: https://help.pythonanywhere.com/pages/AccessingMySQLFromOutsidePythonAnywhere/).

Initially I thought it was because I have 2-factor authorization, but when I temporarily turned it off to test, it still doesn't work. Essentially the script/connection just hangs and doesn't return at all - I have to kill the process. I have a paid account, so expecting to achieve this outside this site. Anyone encounter something similar and have ideas to fix?

Thanks in advance, Leo

Are you using the specific instructions on that page from the "JetBrains PyCharm" section, including the workaround for database names with dollar signs in them?

Hi Giles, not exactly because I do not have PyCharm Professional, only the Community edition, which does not have the dedicated settings to connect to a DB. I am doing it via a python script based on the example in your page.

But I see your point - although I don't exactly know how to incorporate the solution in your link into the python example. Tried without success. Any assistance is much appreciated!

I see. So you are not putting any database settings into pycharm at all.

Do you mind showing us a simplified version of what your script is doing? And also what is the exact error that you are seeing?

Good evening, does anyone know where I can see information about how the pythonanywhere database works. I have the hacker plan and I would like to know information such as how many simultaneous connections it can have and its limitations.

I dont we have a page describing all the limitation of the database. Maybe you want to look at the docs for mysql itself

To find the number of max connections, open a mysql console and run SHOW VARIABLES LIKE "max_connections";

i ma trying to connect mysql to django for some its not even connecting "django.db.utils.OperationalError: (2003, "Can't connect to MySQL server on 'adxbay.mysql.pythonanywhere-services.com' (timed out)")" i cant even access it from tableplus. i have done all the needed seetings and configuration its just does not work. i am on payed plan so i believe i should be able to access it from outside

Are you using the instructions on this help page?