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

2024-05-20 18:22:20 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

我可以以根用户身份登录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?谢谢你

脏溶液:

在没有任何修复的情况下,始终以sudo的身份运行mysql


Tags: djangolocalhostfordbaccessmysqlsudo情况
3条回答
    $ sudo mysql -u root # I had to use "sudo" since is new installation

    mysql> USE mysql;
    mysql> SELECT User, Host, plugin FROM mysql.user;

    As we can see in the query, the root user is using the auth_socket plugin

    There are 2 ways to solve this:

    1) You can set the root user to use the mysql_native_password plugin
    2) shown in above post

    option 1:
    mysql> USE mysql;
    mysql> UPDATE user SET plugin='mysql_native_password' WHERE User='root';
    mysql> FLUSH PRIVILEGES;
    mysql> exit;

    option 2:
seen in the above post

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

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

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';

相关问题 更多 >