我正在尝试在flask
中进行基本查询,并将其输出。在prod
这是无法创建一个mysql
游标。Flask Python App无法创建数据库光标
这里是app.py
:
#from flask_mysqldb import MySQL
import pymysql.cursors
connection = pymysql.connect(host='localhost', user='myusr', port=3306, password='mypass', db='mydb', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)
@app.route('/', methods=('GET', 'POST'))
def email():
form = EmailForm()
try:
with connection.cursor() as cursor:
cursor.execute("SELECT post_title, post_name, YEAR(post_date) YEAR, MONTH(post_date) MONTH FROM mydb.wp_posts WHERE post_status='publish' ORDER BY RAND() LIMIT 3")
r = cursor.fetchall()
print(r)
Exception as e:
print(e)
finally:
connection.close()
我db gui
程序有我的数据库用户这些访问权限(见黑线):收到
修订:::错误,当我包括上述try
行:
2017/01/02 09:10:30 [error] 1042#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: xx.xxx.xxx.xxx, server: mysite.com, request: "GET/HTTP/1.1", upstream: "http://127.0.0.1:8001/", host: "mysite.com"
我测试了这个导入(from flask_mysqldb import MySQL
)在解释器中工作正常,本身也安装了mysql
。
我测试了上述工作的凭据通过gui db program
连接什么是我做错了什么?我正在关注烧瓶和mysql的official documentation,但现在我正在关注link2
这是为什么会失败?任何人都看到什么
谢谢
请您提供收到错误消息。 –
当我在上面包含'cursor'时,nginx会失败,Http 502错误 – Jshee
@PavelRyvintsev - 请参阅更新的错误和db权限。 – Jshee