Hi all,
I'm having a problem with my local version of my PythonAnywhere website. I'm using Flask, Flask-SQLAlchemy, and Python 3.5.2, and calls to my MySQL database that had worked in the past (although on a different laptop) are now not working, instead returning the error "MySQL Connection not available".
Somewhat bizarrely, the exact behavior I'm seeing is that when I try to log into my website the first time, the database call returns "None" for the user, even though the user is present in the database. When I then reload the login page and try again, I get sent to an error page that says "MySQL Connection not available".
I have tried running a debugger and running a query that returns all of the database records, and it returns an empty list, even though there should be a single record (my test account).
I have read the Stack Overflow and PythonAnywhere forum posts of others who have had this issue, but those fixes have not worked.
- I already had
SQLALCHEMY_POOL_RECYCLE
set to a lower value than the corresponding MySQL variables (as recommended here). - I had the
SQLALCHEMY_POOL_RECYCLE
value set to 299, as recommended by the PythonAnywhere blog here. - Changing my MySQL
wait_timeout
andinteractive_timeout
values to be much lower (300 from something like 28800), as described here, didn't seem to have any effect. - Putting my queries within a
try
/except
with adb.session.rollback()
in theexcept
clause (as described here and rec'd by harry here) does stop the app from crashing, but it still says it isn't finding the record that is clearly there.
It seems like there may be two problems here:
- The query isn't finding the record I know is there.
- When I try to run the query a second time, it returns the "MySQL Connection not available" error.
Has anyone else run into these issues?
Other things tried:
- If I run the debugger and try inserting records into the database, they show up in my PyCharm database viewer.
- Adding the line
db.init_app(app)
to the bottom of my app.py initialization function stops the error messages from crashing the app, but still doesn't result in the ORM finding the user record.