1

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()
JamesB
  • 61
  • 2
  • 5

1 Answers1

0

As you can see from the error: pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([Errno 111] Connection refused)") MySQL tried to connect to localhost server, which mean, its doesnt load your config variables.

If you are using flask extension from https://flask-mysql.readthedocs.io/en/latest/# to load MySQL then you are probably forgot to init your application (pip install flask-mysql)

mysql.init_app(app)

The head of you code should now look like this

And watch for the config variables names, the extension using slightly different convention

from flask import Flask
from flaskext.mysql import MySQL

#instaniation
app = Flask(__name__)
app.secret_key= 'secret'

#config
app.config['MYSQL_DATABASE_USER'] = 'user'
app.config['MYSQL_DATABASE_PASSWORD'] = 'password'
app.config['MYSQL_DATABASE_DB'] = 'db'
app.config['MYSQL_DATABASE_HOST'] = 'host'

#init MySQL
mysql = MySQL()
mysql.init_app(app)
iafst
  • 1
  • 3
  • Thanks a lot, that fixed that problem. Now I can work on the others, but you have given me something to go on now. – JamesB Jul 26 '19 at 23:17