Forums

Flask mySql Database Setup using python 3.4

I'm transferring from an sqlite database to MySql and am having some trouble creating the database.

Since python 3 does not support MySql I am using PyMySql

app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://<username>:<password>@<Database host address>/<database name>

ERROR MESSAGE: sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on 'ilankessler.mysql.pythonanywhere-services.com' ([Errno 60] Operation timed out)")

note: the Database host address is 'ilankessler.mysql.pythonanywhere-services.com'

Hm, I don't know pymysql. We've seen people successfully use "mysqlclient" tho? info here: https://help.pythonanywhere.com/pages/UsingMySQL

Thanks Harry, is there any info on how to set it up with flask using sqlalchemy?

The only specific recommendation we make is setting the pool_recycle value to 280: https://help.pythonanywhere.com/pages/UsingSQLAlchemywithMySQL - everything else should be pretty standard...

Giles did an intro to flask on pythonanywhere blog post that you might find useful as well?

I cant get this to work for python 3.4.3. The error I keep receiving is:

/venv/lib/python3.4/site-packages/sqlalchemy/dialects/mysql/mysqlconnector.py", line 107, in dbapi from mysql import connector ImportError: No module named 'mysql'

I see your only webapp is using a virtualenv. Did you install MySQL connector into the virtualenv?

Thanks giles, fixed that issue, but now I'm back to the original problem I had using PyMySql...

ERROR: sqlalchemy.exc.InterfaceError: (mysql.connector.errors.InterfaceError) 2003: Can't connect to MySQL server on 'ilankessler.mysql.pythonanywhere-services.com:3306' (60 Operation timed out)

Where are you getting that error? Is it in your web app? I just checked the logs and couldn't see anything like that.

Thanks again, it actually works fine after uploading the code and running it through a python anywhere console.

I'm new to web development, and appreciate all the help.

How can I access the same database during testing mode where I am running flask on localhost?

You can use the technique described here with some modification to connect to your PythonAnywhere database, but it's going to be pretty slow getting your data across the internet.

It would generally be better if you had a little script to upload a dump of the database and populate your PythonAnywhere database from that. Then you could decide whether you need to update the database and run the script only when you need to.