I'm learning flask etc. and created a simple app locally. Everything worked fine with a local mysql database in xammp. I exported this and imported into clearDB on heroku and deployed my app. The app runs, but whenever I try to access a page relying on the database in the logs I see this error.
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([Errno 111] Connection refused)")
I ran the app locally, but using the clearDB database, and it was able to retrieve the data no problem. Everything is configured in the file to access based on the URL details from heroku.
Code is below. I'm sure there are a ton of other issues, but as I say I'm learning and got all this from a tutorial.
#instaniation
app = Flask(__name__)
app.secret_key= 'secret123'
#config
app.config['MYSQL_HOST'] = 'us-cdbr-iron-east-02.cleardb.net'
app.config['MYSQL_USER'] = 'redacted'
app.config['MYSQL_PASSWORD'] = 'redacted'
app.config['MYSQL_DB'] = 'heroku_0c6326c59d8b6e9'
app.config['MYSQL_CURSORCLASS'] = 'DictCursor' #a cursor is a connection to let us run methods for queries. We set is as a dictionary
#init MySQL
mysql = MySQL(app,cursorclass=DictCursor)
#url endpoint
@app.route('/')
#function for starting app
def index():
#can directly return HTML
return render_template('index.html')
@app.route('/about')
def about():
return render_template('about.html')
#many articles
@app.route('/articles')
def articles():
#create Cursor
cur = mysql.get_db().cursor() #to execute commands
cur.execute("USE myflaskapp")
#get articles
result = cur.execute("SELECT * FROM articles")
#should retrieve all as a dictionary
articles = cur.fetchall()
if result > 0:
return render_template('articles.HTML', articles=articles)
else:
msg= 'No Articles Found'
return render_template('articles.HTML', msg=msg)
cur.close()