尽管已设置密码并在连接 uri 中输入,但用户 root@localhost 的 mysql 访问被拒绝(使用密码:否) [英] mysql Access denied for user root@localhost (using password: NO) despite having set a password and entered it in connection uri

查看:83
本文介绍了尽管已设置密码并在连接 uri 中输入,但用户 root@localhost 的 mysql 访问被拒绝(使用密码:否)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Flask 应用程序中使用 pymysql 和 Flask-sqlalchemy 配置 mysql 数据库.

I'm trying to configure a mysql db with pymysql and Flask-sqlalchemy in a flask app.

db = SQLAlchemy()
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:password@localhost:3306/ftm'
db.init_app(app)

但是,当我运行我的应用程序时,我得到:

However, when I run my app, I get:

OperationalError: (pymysql.err.OperationalError) (1045, u"Access denied for user 'root'@'localhost' (using password: NO)")

我尝试重置密码,并验证它有效:从命令行,我可以通过写入进入mysql:

I tried resetting the password, and verified that it works: from command line, I can enter mysql by writing:

mysql -u root -p

然后在提示时输入我的密码.有没有人知道为什么 mysql 似乎认为我试图在没有密码的情况下进行连接,即使我在 URI 中有一个(有效的)密码?

And then entering my password at the prompt. Does anybody have any insight as to why mysql seems to think I'm trying to connect without a password, even though I have a (valid) password in the URI?

请注意,这是与 mysql_exceptions.OperationalError 不同的问题:(1045,用户'root'@'localhost' 的访问被拒绝(使用密码:YES)") 和其他潜在的重复,因为这些错误消息至少注册了连接尝试附加了密码.

Note that this is a different problem from mysql_exceptions.OperationalError: (1045, "Access denied for user 'root'@'localhost' (using password: YES)") and other potential duplicates because those error messages at least register that the connection attempt has a password attached.

没有特别的顺序,我已经:尝试了不同的用户在 db.init_app 之前检查了连接字符串的状态,创建了一个 不同的 MySQL 实例 并尝试连接到它,从连接字符串中删除 pymysql,并尝试用 pdb 回溯它(没用,因为唯一的方法调用我认识到我自己的是 init_app()).我还搜索了互联网,发现没有提到任何人在尝试使用密码连接到数据库时收到此错误消息.我能想到的唯一剩下的可能性是同时从 app.config 传递到 mysql(当我调用 db.init_app 时)某些东西正在更改我的字符串以删除密码. 有什么想法吗?如果没有人,我只需要使用 Postgre 什么的……

In no particular order, I have: tried a different user, checked the state of the connect string immediately before db.init_app, created a different instance of MySQL and tried connecting to it, removed pymysql from the connection string, and tried back tracing it with pdb (useless because the only method call which I recognized as my own was init_app()). I have also scoured the internet, and found no mention of anybody getting this error message when trying to connect to the database using a password. The only remaining possibility that I can think of is that while being passed from app.config to mysql (when I call db.init_app) something is changing my string to remove the password. Any ideas? If nobody’s got any, I’ll just have to go use Postgre or something…

推荐答案

我遇到了这个问题并通过进入 db shell 并告诉 mysql 使用 mysql_native_password 身份验证插件来验证您的数据库来解决它.这是一个较旧的插件,可能值得寻找一个与 Python 的 mysql 连接驱动程序兼容的新插件,但这是我让它工作的唯一方法.

I ran into this problem and solved it by entering the db shell and telling mysql to use the mysql_native_password authentication plugin to authenticate your database. This is an older plugin and it's probably worth looking for a newer one that's also compatible with Python's mysql connect driver, but this is the only way I could get it to work.

ALTER USER 'user_name'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password123'

在操作提供的示例中,您可以将 root 替换为 user_name.

In the example provided by the op you would substitute root for user_name.

关于插件的更多信息:https://dev.mysql.com/doc/refman/8.0/en/native-pluggable-authentication.html

这篇关于尽管已设置密码并在连接 uri 中输入,但用户 root@localhost 的 mysql 访问被拒绝(使用密码:否)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆