简体   繁体   中英

KeyError: 'MYSQL_UNIX_SOCKET' Error while using flask_mysqldb in Flask app

Edit 1 I created a hosted mysql instance on digital ocean and I am getting a new error.

I get the following TypeError: an integer is required (got type str)

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 init .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")

because of the app.config['MYSQL_HOST'] = 'localhost' , you are using unix socket to connect the mysql. you could change MYSQL_HOST to ip address , in order to use TCP/IP to connect the mysql.

我为端口号提供了一个字符串而不是一个整数

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM