django.db.utils.OperationalError: (1045, Access denied for user '<user>'@'localhost'

2022-01-15 00:00:00 python django access-denied mariadb

问题描述

我无法让我的 Django 项目正确加载我的数据库.它抛出这个错误.

I can't get my Django project to load my database correctly. It throws this error.

我正在使用 Django 运行 MariaDB,并且我卸载了所有 MySQL

I'm running MariaDB with Django, and I uninstalled all MySQL

我通过运行添加了用户:

I added the user by running:

create database foo_db;
create user foo_user identified by 'foo_password';
grant all on foo_db.* to 'foo_user'@'%';
flush privileges;

按照这篇文章的建议.

当我尝试运行 ./manage.py runserver

django.db.utils.OperationalError: (1045, "Access denied for user '<user>'@'localhost' (using password: YES)")

我已运行创建用户"命令并创建了用户以匹配我的设置脚本,该脚本的默认设置为:

I have run the 'create user' command and created the user to match my setup script, which has the default set as:

        'ENGINE': 'django.contrib.gis.db.backends.mysql',
        'NAME': 'company_production',
        'USER': 'companydev',
        'PASSWORD': 'companydevpass',
        'HOST': 'localhost',
        'PORT': '',
        'ATOMIC_REQUESTS': True

我已经尝试了在这里找到的与它引发的错误相关的所有内容,但没有任何效果.

I have tried everything I've been able to find on here related to the error its thrown, but nothing has been working.

我正在运行 Mac OSX 10.11.1 El Capitan 和 MariaDB 版本 10.1.8,如果这完全相关的话.

I'm running Mac OSX 10.11.1 El Capitan and MariaDB version 10.1.8 if that's relevant at all.


解决方案

这是我用来为 Django 项目重新创建 MySQL 数据库的方法:

This is what I use to re-create MySQL databases for Django projects:

tmp="/tmp/mysql-tools.sh.tmp"

setup-db ( ) {
    cat <<EOF > $tmp
DROP DATABASE $DB;
CREATE DATABASE $DB;
GRANT USAGE on *.* to '${DB_USER}'@'localhost' IDENTIFIED BY '${DB_PASS}';
GRANT ALL PRIVILEGES ON ${DB}.* to '${DB_USER}'@'localhost';
EOF
    cat $tmp
    mysql -f -u ${MYSQL_ROOTUSR} -p${MYSQL_ROOTPWD} < $tmp
    rm $tmp
}

警告:这会掉落并重新创建!

Warning: this drops and re-creates!

地点:

  • DB:数据库名称
  • DB_USER:django 数据库用户
  • DB_PASS:Django 数据库用户的 mysql 连接密码
  • MYSQL_ROOTUSR:mysql root 用户(必须有创建数据库的权限)
  • MYSQL_ROOTPWD:mysql root 密码

导出环境中的那些

相关文章