Flask-SQLAlchemy PyMySQL - 拒绝访问错误 [英] Flask-SQLAlchemy PyMySQL - Access Denied Error
问题描述
Python版本:3.6
SQLAlchemy版本:1.1.10
PyMySQL版本:0.7.11
MySQL分布:5.7.17-13
$ b $ pre $
import pymysql
conn = pymysql.connect(host ='localhost',port = 3306,user ='testuser ',passwd ='###########',db ='mysql')
cur = conn.cursor()
cur.execute (SELECT Host,User FROM user)
print(cur.description)
print()
for row in cur:
print(row)
cur.close()
conn.close()
当我尝试执行上面的代码时,我收到以下错误消息,但是仍然可以通过命令行使用相同的帐户和密码连接到mysql。
pre $ p $ c $ pymysql.err.OperationalError:(1045,拒绝用户testuser'@127.0.0.1(使用密码:是))
我尝试在网路上搜寻这个错误讯息,但是我找不到实际的问题。我真的很感激,如果有人能帮我解决这个问题。
解决方案
我发现这个问题:MySQL 5.7的默认身份验证插件是PyMySQL不支持的sha256。将其更改为mysql_native_password并解决了问题。
OS: RHEL Python Version: 3.6 SQLAlchemy Version: 1.1.10 PyMySQL Version: 0.7.11 MySQL Distrib: 5.7.17-13
import pymysql
conn = pymysql.connect(host='localhost', port=3306, user='testuser', passwd='###########', db='mysql')
cur = conn.cursor()
cur.execute("SELECT Host,User FROM user")
print(cur.description)
print()
for row in cur:
print(row)
cur.close()
conn.close()
When I try to execute the above code I'm getting the following error message, but I'm still able to connect to mysql using the same account and password via command line.
pymysql.err.OperationalError: (1045, "Access denied for user 'testuser'@'127.0.0.1' (using password: YES)")
I tried searching all over the internet for this error message but I could not figure out the actual issue. I would really appreciate if anyone could help me fix this issue.
I found the issue: The default auth plugin for MySQL 5.7 was sha256 which is not supported by PyMySQL. Changed it to mysql_native_password and that fixed the issue.
这篇关于Flask-SQLAlchemy PyMySQL - 拒绝访问错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!