KeyError: ‘MYSQL_UNIX_SOCKET’ Error while using flask_mysqldb in Flask app

  flask, flask-mysql, python

I have a flask application I am working on and I am getting an error while using flask_mysqldb

Here is the error I am receiving

KeyError: 'MYSQL_UNIX_SOCKET'


My directory structure is

  • main.py
  • auth.py
  • website
    • templates
    • static
    • init.py

In my main.py I have the following

from flask import Flask
from flask_mysqldb import MySQL

def create_app():
    app = Flask(__name__)
    app.config['MYSQL_USER'] = 'flask'
    app.config['MYSQL_PASSWORD'] = 'mypw'
    app.config['MYSQL_HOST'] = 'localhost'
    app.config['MYSQL_DB'] = 'flask_test'
    app.config['MYSQL_CURSORCLASS'] = 'DictCursor'
    app.config['MYSQL_PORT'] = '3306'

    from views import views
    from auth import auth

    app.register_blueprint(views, url_prefix='/')
    app.register_blueprint(auth, url_prefix='/')

    return app

and in my auth.py I have the following as you can see I am attempting to print out the result to the console.

If I move the the code for mysql into my main.py it does work and I am able to print out the data. Its only when I attempt to use the mysql code within my auth.py that I get the error.

from flask import Blueprint, render_template, request, flash
from flask_mysqldb import MySQL
auth = Blueprint('auth', __name__)

@auth.route('/sign-up', methods=['GET', 'POST'])
def sign_up():
    mysql = MySQL()
    cur = mysql.connection.cursor()
    cur.execute('''SELECT * FROM peeps''')
    results = cur.fetchall()
    print(results)

    return render_template("sign_up.html")

Source: Python Questions

LEAVE A COMMENT