MySQL:django.db.utils.OperationalError:(1698,“访问被拒绝用户'root'@'localhost'”)具有正确的用户名和pw

问题描述 投票:3回答:2

使用mysql时我有django.db.utils.OperationalError: (1698, "Access denied for user 'root'@'localhost'")。用户名和密码是正确的:

DB_HOST = '127.0.0.1'
DB_USER = 'root'
DB_PASSWORD = ''

我可以以root身份登录mysql:

$ sudo mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 16

但不像cchilders

$ mysql -u root
ERROR 1698 (28000): Access denied for user 'root'@'localhost'

这可能会导致问题。上次我安装mysql时没有发生这种情况,所以对我来说没有意义。我有客户端罚款:

$ pip3 freeze
Django==1.10.5
mysqlclient==1.3.9

我如何允许普通用户运行mysql,所以我可以在终端运行django?谢谢

脏的解决方案:

没有任何修复,总是运行mysql作为sudo

python mysql django python-3.x django-settings
2个回答
2
投票

您可以在服务器上以root身份登录的原因是您可能在.my.cnf/root文件中指定了密码(即root用户的主目录)。检查那里是否有密码,并将其用于cchilders。然后,您可以创建一个django特定的应用程序用户,以确保django应用程序只读/写/等。到它需要访问的数据库,而不是通过root mysql用户访问。

create user 'django'@'localhost' identified by 'django-user-password';
grant usage on *.* to 'django'@'localhost';
grant all privileges on django-database-1.* to 'django'@'localhost';

1
投票

创建一个非root SQL用户并更改Django的settings.py文件中的DB_USER变量

© www.soinside.com 2019 - 2024. All rights reserved.