MySQL shuts down connections after they've been unused for a while; the amount of time it waits depends on your MySQL server's settings. Often when you install it on your own machine, you'll wind up with a very long setting (which is good when you're doing development) but on a live system like PythonAnywhere it's generally shorter, to stop programs that don't close connections properly from locking up the server. Specifically, we have it set to five minutes.
To manage this, code should normally close connections when it's no longer using them. This can be a bit messy to code, but if you're using Flask, SQLAlchemy is a great library that handles all of this stuff for you. This blog post is a tutorial on building a DB-backed Flask app that uses SQLAlchemy, so you can probably take the code you need from there.