gpt4 book ai didi

python - MySQLdb._exceptions.OperationalError OperationalError : (1045, "Access denied for user ' root' @'localhost'(使用密码:YES)“)

转载 作者:行者123 更新时间:2023-12-04 18:50:08 31 4
gpt4 key购买 nike

我正在尝试使用 mysql 来运行 python-flask Web 应用程序。但是,我在主应用程序文件中连接到数据库,每次尝试将数据输入数据库(即注册客户)时,我都会收到错误消息:

Traceback (most recent call last):
File "/home/<username>/.local/lib/python2.7/site-packages/flask/app.py", line 2463, in __call__
return self.wsgi_app(environ, start_response)
File "/home/<username>/.local/lib/python2.7/site-packages/flask/app.py", line 2449, in wsgi_app
response = self.handle_exception(e)
File "/home/<username>/.local/lib/python2.7/site-packages/flask/app.py", line 1866, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/<username>/.local/lib/python2.7/site-packages/flask/app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "/home/<username>/.local/lib/python2.7/site-packages/flask/app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/<username>/.local/lib/python2.7/site-packages/flask/app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/<username>/.local/lib/python2.7/site-packages/flask/app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "/home/<username>/.local/lib/python2.7/site-packages/flask/app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/mnt/c/Users/<username>/Documents/<web application>/app.py", line 156, in register
cur = mysql.connection.cursor()
File "/home/<username>/.local/lib/python2.7/site-packages/flask_mysqldb/__init__.py", line 94, in connection
ctx.mysql_db = self.connect
File "/home/<username>/.local/lib/python2.7/site-packages/flask_mysqldb/__init__.py", line 81, in connect
return MySQLdb.connect(**kwargs)
File "/home/<username>/.local/lib/python2.7/site-packages/MySQLdb/__init__.py", line 84, in Connect
return Connection(*args, **kwargs)
File "/home/<username>/.local/lib/python2.7/site-packages/MySQLdb/connections.py", line 179, in __init__
super(Connection, self).__init__(*args, **kwargs2)
OperationalError: (1045, "Access denied for user 'root'@'localhost' (using password: YES)")

我看过类似的文章,主要建议是:
(in sql)
USE mysql;
UPDATE mysql.user SET plugin = 'mysql_native_password' WHERE User='root';
flush privileges;
exit;

(in terminal): sudo service mysql restart

我如何配置数据库:
# Config MySQL
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = '<root password>'
app.config['MYSQL_DB'] = '<db name>'
app.config['MYSQL_CURSORCLASS'] = 'DictCursor'
#init MYSQL
mysql= MySQL(app)

但是,尝试此操作后错误仍然存​​在。我不确定如何继续,所以任何帮助将不胜感激。

最佳答案

如果您在正常登录 MySQL 时能够进行查询,那么应该没有理由不能在应用程序中进行查询(使用相同的用户/密码)。

我建议检查您的密码是否有任何可能无法正确解析的字符,例如 ' \ .

如果您没有看到密码有任何问题,您可以尝试在执行任何查询之前从应用程序连接到 MySQL,并查看连接是否成功。

关于python - MySQLdb._exceptions.OperationalError OperationalError : (1045, "Access denied for user ' root' @'localhost'(使用密码:YES)“),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60489595/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com