Skip to content Skip to sidebar Skip to footer

Django.db.utils.operationalerror: (1045, Access Denied For User ''@'localhost'

I can't get my Django project to load my database correctly. It throws this error. I'm running MariaDB with Django, and I uninstalled all MySQL I added the user by running: creat

Solution 1:

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';
EOFcat$tmp
    mysql -f -u ${MYSQL_ROOTUSR} -p${MYSQL_ROOTPWD} < $tmprm$tmp
}

Warning: this drops and re-creates!

Where:

  • DB: the database name
  • DB_USER: the django database user
  • DB_PASS: the password for the mysql connection for the Django database user
  • MYSQL_ROOTUSR: the mysql root user (must have permissions to create databases)
  • MYSQL_ROOTPWD: the mysql root password

Export those in your environment

Solution 2:

Your Error gives you a clue where you're going wrong...

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

That means django trying to access the database with user user, not what you have defined in settings.py.

I guess you have forgotten to do a python manage.py migrate before doing runserver, as your settings are not yet reflected inside django engine.

Solution 3:

It may due to Django tried to use the wrong user instead of what you set up in MySQL DB

change setting from USERNAME -> USER could help:)

I got the error with

'NAME': 'database name', 'USERNAME':'root', 'PASSWORD':'******', 'PORT':'3306', 'HOST': 'localhost',

But it worked with ---

'NAME': 'database name', 'USER':'root', 'PASSWORD':'********', 'PORT':'3306', 'HOST': 'localhost',

Post a Comment for "Django.db.utils.operationalerror: (1045, Access Denied For User ''@'localhost'"