MySQL says Access denied for user 'root'@'localhost' and Django cannot connect
ERROR 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.
Fix it fast
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 33062Match what you see, then jump to its fix
- 1
Access 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
Access 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
The command-line login works, but Django still reports (using password: YES) or (using password: NO)Django reads different credentials than the ones you testedFix
- 1
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 -p2Fix
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-init3Verify
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
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 mydbConfirmed by dev.mysql.com
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 --checkConfirmed by docs.djangoproject.com
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- ShopGoAn e-commerce aggregator with a Python scraping engine behind its catalog, past 10K Google Play downloads.
Related fixes
Databases
Installing the Airflow MySQL provider fails on pkg-config or mysqlclient
Building the mysqlclient wheel fails during the install
The Airflow MySQL provider depends on mysqlclient, which pip compiles against the MySQL C client library.
2 causes, each with a fixDatabases
Compiling a C or C++ program that includes mysql.h fails
undefined reference to `mysql_init'
The compiler only finds mysql.h when the MySQL client development files are installed and their folder is on the include path.
3 causes, each with a fixDatabases
An EC2 instance cannot reach its RDS database
Could not open connection to the host, on port <port>: Connect failed
RDS denies all access until a security group allows it.
3 causes, each with a fixDatabases
Locked out of a fresh MariaDB install as root on Mac or Linux
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
A fresh MariaDB root account is not a normal password account.
3 causes, each with a fixDatabases
An Aria table fails with 'Read page with wrong checksum' (error 1030, code 176)
ERROR 1030 (HY000): Got error 176 "Read page with wrong checksum" from storage engine Aria
Code 176 means MariaDB's Aria engine read a data or index page whose checksum does not match, so that table file is damaged.
3 causes, each with a fixDatabases
The client cannot open a TCP connection to MariaDB or MySQL (error 2003)
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1:3306' (111)
Error 2003 means the client tried a TCP connection to the host and port and nobody answered.
4 causes, each with a fix