Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

MySQL says Access denied for user 'root'@'localhost' and Django cannot connect

ErrorERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

Short answer

ERROR 1045 means MySQL found no account that matches the user, host and password your client sent. Read the message first: the user@host pair is the account MySQL tried, and using password YES or NO says whether a password was sent. Then reset the password, fix the host part of the account, or fix the credentials Django actually uses.

  • Applies to MySQL 8.4, Django 5.2, mysqlclient 2.2, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Try the same login by hand over TCP, with the same user and host Django uses. If this fails too, the problem is the account, not Django.

    mysql -u root -p -h 127.0.0.1 -P 3306
  2. 2Match what you see, then jump to its fix

    1. 1Access denied for user 'root'@'localhost' (using password: YES) in the mysql client, and the same text from DjangoThe password is wrong, empty, or was never set the way you thinkFix
    2. 2Access denied for user 'app'@'172.18.0.5' (the host part is not the one you created the user for)The account exists, but not for the host you connect fromFix
    3. 3The command-line login works, but Django still reports (using password: YES) or (using password: NO)Django reads different credentials than the ones you testedFix

What does the error look like?

  • ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
  • ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
  • django.db.utils.OperationalError: (1045, "Access denied for user 'root'@'localhost' (using password: YES)")

Cause 1 of 3

The password is wrong, empty, or was never set the way you think

1Check

Try the password you believe is right in the client. If it fails, the stored password differs. A fresh install, a package upgrade or a copied data directory often leaves root with a password you never chose.

mysql -u root -p

2Fix

Reset the root password with an init file, which is the method the MySQL manual prefers because it never turns authentication off. Stop the server, write the statement to a file only you can read, start mysqld with it, delete the file, then restart normally.

echo "ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';" > /home/me/mysql-init
chmod 600 /home/me/mysql-init
mysqld --user=mysql --init-file=/home/me/mysql-init &
# after it starts:
rm /home/me/mysql-init

3Verify

Stop that server process, start the service the normal way, and log in with the new password.

mysql -u root -p -e "SELECT CURRENT_USER();"

Confirmed by dev.mysql.com

Did this fix it?

Cause 2 of 3

The account exists, but not for the host you connect from

1Check

MySQL accounts are a user plus a host, so 'app'@'localhost' and 'app'@'%' are two different accounts. The host in the error message is the one the server saw. List the accounts and compare.

SELECT user, host, plugin FROM mysql.user;
SELECT CURRENT_USER();

2Fix

Create the account for the host the error shows, and grant only the privileges the app needs.

CREATE USER 'app'@'%' IDENTIFIED BY 'StrongPassword!';
GRANT SELECT, INSERT, UPDATE, DELETE ON mydb.* TO 'app'@'%';

3Verify

Show the grants, then connect from the client machine with that user.

SHOW GRANTS FOR 'app'@'%';
mysql -u app -p -h DB_HOST mydb

Confirmed by dev.mysql.com

Did this fix it?

Cause 3 of 3

Django reads different credentials than the ones you tested

1Check

Django merges settings in a fixed order: OPTIONS first, then NAME, USER, PASSWORD, HOST and PORT, then MySQL option files. A read_default_file in OPTIONS or an environment variable that is empty can override what you expect. (using password: NO) usually means PASSWORD arrived empty.

python manage.py shell -c "from django.conf import settings; d=settings.DATABASES['default']; print(d['USER'], d['HOST'], d['PORT'], bool(d['PASSWORD']), d.get('OPTIONS'))"

2Fix

Put the credentials in one place only. Either set USER and PASSWORD in DATABASES, or point read_default_file at one option file, and make sure the environment variable that fills PASSWORD is set where the process runs.

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.mysql",
        "OPTIONS": {"read_default_file": "/etc/myapp/my.cnf"},
    }
}

# /etc/myapp/my.cnf
[client]
database = mydb
user = app
password = StrongPassword!

3Verify

Run a command that opens a database connection.

python manage.py migrate --check

Confirmed by docs.djangoproject.com

Did this fix it?

How do I stop it coming back?

  • Create one MySQL user per application instead of using root from Django.
  • Keep the password in one secret store or one option file, not in two places.
  • Write down which host each account was created for before moving the app to a container or another server.
  • Keep the init file for a root reset out of shared folders, and delete it as soon as the server starts.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.