[英]MySQLdb._exceptions.OperationalError OperationalError: (1045, "Access denied for user 'root'@'localhost' (using password: YES)")
I am trying to use mysql to run a python-flask web application.我正在尝试使用 mysql 来运行 python-flask Web 应用程序。 I am connecting to the database in the main application file however, everytime I try to enter data into the database (ie register a customer) I keep getting the error:
我正在连接到主应用程序文件中的数据库,但是,每次我尝试将数据输入数据库(即注册客户)时,我都会收到错误消息:
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)")
I have looked at similar articles with the main suggestion being:我看过类似的文章,主要建议是:
(in sql)
USE mysql;
UPDATE mysql.user SET plugin = 'mysql_native_password' WHERE User='root';
flush privileges;
exit;
(in terminal): sudo service mysql restart
How I config the database:我如何配置数据库:
# 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)
However, the error still persists after trying this.但是,尝试此操作后错误仍然存在。 I am unsure how to continue so any help would be appreciated.
我不确定如何继续,所以任何帮助将不胜感激。
If you are able to make the queries when logged into MySQL normally, there should be no reason to not be able to do them in the application (with the same user/pass).如果您能够在正常登录 MySQL 时进行查询,则没有理由不能在应用程序中进行查询(使用相同的用户/密码)。
I would recommend checking if your password has any characters that could parse incorrectly, eg ' \\
.我建议检查您的密码是否包含任何可能解析错误的字符,例如
' \\
。
If you don't see any issues with the password, you can try to just connect to MySQL from the application before doing any queries and see if the connection is successful.如果您没有发现密码有任何问题,您可以尝试从应用程序连接到 MySQL,然后再进行任何查询,看看连接是否成功。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.