python - MySQL: django.db.utils.OperationalError: (1698, "Access denied for user ' root' @'localhost' ") with correct username and pw

标签 python mysql django python-3.x django-settings

我在使用 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?谢谢

肮脏的解决方案:

没有任何修复,始终以 sudo 运行 mysql

最佳答案

您可以在服务器上以 root 身份登录的原因是您可能在 /root< 的 .my.cnf 文件中指定了密码(即 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';

关于python - MySQL: django.db.utils.OperationalError: (1698, "Access denied for user ' root' @'localhost' ") with correct username and pw,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41542045/

相关文章:

python - 这个 return 语句究竟指示程序做什么?

mysql - 如何从内容不包含辅助表中存在的停用词的表中选择所有帖子

html - Django 中的 Bootstrap4 未按预期工作

python - 我无法在 PyCharm 下运行 Django Python pytest

python - 用python更新elasticsearch查询

python - aiohttp asyncio.TimeoutError 来自 None 使用 ClientSession

php - MYSQL中如何按字母顺序过滤记录

html - 960gs 和循环

python - 从 Nose2 插件跳过单元测试

php - Laravel 中批量分配的风险