So I've uploaded my tutorial website which I made using python flask and mysql onto Heroku. I'm using clear db to connect to the mysql database. But it doesn't always work and gives me this error:
2021-07-09T09:54:07.008053+00:00 app[web.1]: sqlalchemy.exc.OperationalError: (mysql.connector.errors.OperationalError) MySQL Connection not
available.
But when I refresh the page it works. I don't think that it's to do with my internet because it doesn't do this when I'm running it from my local computer. So how can I fix this problem??
This is the code for my database connection:
ENV = "prod"
if ENV == "prod" :
app.config['MYSQL_HOST'] = 'us-cdbr-east-04.cleardb.com'
app.config['MYSQL_USER'] = 'bf4f325cc53121'
app.config['MYSQL_PASSWORD'] = ''
app.config['MYSQL_DB'] = 'heroku_da1c413642e9212'
username = "bf4f325cc53121"
password = ""
server= "us-cdbr-east-04.cleardb.com"
database = "heroku_da1c413642e9212"
app.config['SQLALCHEMY_DATABASE_URI'] = f'mysql+mysqlconnector://{username}:{password}@{server}/{database}'
else :
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = ''
app.config['MYSQL_DB'] = 'flask'
username = "flask"
password = ""
server= "localhost"
database = "flask"
app.config['SQLALCHEMY_DATABASE_URI'] = f'mysql+mysqlconnector://{username}:{password}@{server}/{database}'
So when I change the env to something other than prod it works on my local computer but when I change it back to prod it works on heroku.