MySQLdb._exceptions.OperationalError OperationalError:(1045,“用户'root'@'localhost'的访问被拒绝(使用密码:是)”)

时间:2020-03-02 13:12:18

标签: python mysql ubuntu flask

我正在尝试使用mysql运行python烧瓶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)

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

1 个答案:

答案 0 :(得分:0)

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

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

如果看不到密码有任何问题,可以尝试在进行任何查询之前先从应用程序连接到MySQL,然后查看连接是否成功。