简体   繁体   English

KeyError: 'MYSQL_UNIX_SOCKET' 在 Flask 应用程序中使用 flask_mysqldb 时出错

[英]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.编辑 1我在数字海洋上创建了一个托管的 mysql 实例,但出现新错误。

I get the following TypeError: an integer is required (got type str)我得到以下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我有一个正在处理的 Flask 应用程序,但在使用 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在我的init .py 中,我有以下内容

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.在我的 auth.py 中,您可以看到我正在尝试将结果打印到控制台。

If I move the the code for mysql into my main.py it does work and I am able to print out the data.如果我将 mysql 的代码移动到我的 main.py 中,它确实可以工作并且我能够打印出数据。 Its only when I attempt to use the mysql code within my auth.py that I get the error.只有当我尝试在我的 auth.py 中使用 mysql 代码时才会出现错误。

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.由于the app.config['MYSQL_HOST'] = 'localhost' ,您使用 unix 套接字连接 mysql。 you could change MYSQL_HOST to ip address , in order to use TCP/IP to connect the mysql.您可以将 MYSQL_HOST 更改为ip地址,以便使用TCP/IP连接 mysql。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python & Flask 错误使用 Flask-MySQL | 未找到模块错误:没有名为“flask_mysqldb”的模块 - Python & Flask error using Flask-MySQL | Module Not Found Error: No module named 'flask_mysqldb' ImportError: No module named flask_mysqldb ("from flask_mysqldb import MySQL") - ImportError: No module named flask_mysqldb ("from flask_mysqldb import MySQL") Python Flask_MYSQLDB 问题 - Python Flask_MYSQLDB problems 无法从“flask_mysqldb”导入名称“MYSQL” - cannot import name 'MYSQL' from 'flask_mysqldb' 'pip install flask_mysqldb'失败,并显示错误消息'退出状态2' - 'pip install flask_mysqldb' fails with error message 'exit status 2' 如何使用带有蓝图的flask_mysqldb - How to use flask_mysqldb with blueprint 为什么flask找不到模块flask_mysqldb? - Why flask won't found module flask_mysqldb? Pip 安装 flask_mysqldb 出现错误:错误:命令出错,退出状态为 1: - Pip install flask_mysqldb getting error: ERROR: Command errored out with exit status 1: `pip install flask_mysqldb`由于clang失败:错误:链接器命令失败,退出代码为1 - `pip install flask_mysqldb` fails due to clang: error: linker command failed with exit code 1 如何解决 ModuleNotFoundError: No module named 'flask_mysqldb' 在 Vs.Code 中的错误? - How can I solve ModuleNotFoundError: No module named 'flask_mysqldb' Error in Vs.Code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM