微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

编辑 1 我在数字海洋上创建了一个托管的 MysqL 实例,但出现新错误

我得到以下信息 TypeError: an integer is required (got type str)

我正在开发一个flask应用程序,但在使用flask_MysqLdb时出现错误

这是我收到的错误

KeyError: 'MysqL_UNIX_SOCKET'

我的目录结构是

  • main.py
  • auth.py
  • website

在我的 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

在我的 auth.py 中,您可以看到我正在尝试将结果打印到控制台。

如果我将 MysqL代码移动到我的 main.py 中,它确实可以工作并且我能够打印出数据。只有当我尝试在 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")

解决方法

因为the app.config['MYSQL_HOST'] = 'localhost',你使用unix socket连接mysql。您可以将 MYSQL_HOST 更改为 ip 地址,以便使用 TCP/IP 连接 mysql。

,

我提供的是端口号的字符串而不是整数

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。