[英]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.