MySQL 1045: Access denied for user root localhost
Error 1045 is an account match or plugin problem, not a network one. Check user and host rows, localhost versus 127.0.0.1, and client plugin support.

Introduction & Quick Summary
The client refuses to connect:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
Error 1045 means authentication failed — the server does not recognise the pair of user and host, or the password did not verify. The parenthesised part is decisive: using password: YES means a password was sent and rejected, using password: NO means none was sent, which is a completely different problem.
Where it appears:
- A
docker run mysqlcontainer after the root password was set withMYSQL_ROOT_PASSWORDbut the client connects over TCP while root is only granted for the socket. - Debian and Ubuntu packages where
root@localhostusesauth_socket, so a password cannot work at all. - An old client (MySQL 5.7 CLI, PHP
mysql_*, some ORMs) against MySQL 8.4, wherecaching_sha2_passwordis the default and the client does not support it. - A connection from
127.0.0.1while the grant exists only forlocalhost, or the reverse. - An application connecting from a container whose source IP matches no
'user'@'%'grant.
Quick answer: see what the server thinks the account is — SELECT user, host, plugin FROM mysql.user — then fix the grant for the host the client actually connects from, or set the password with the plugin the client supports.
Core Mechanics, Signatures & Reproducible Setup
MySQL identifies an account as 'user'@'host', and the host is matched against the client address using the most specific pattern. localhost means the Unix socket, not 127.0.0.1, which is a TCP connection. That single distinction explains a large share of 1045 reports.
Inspect the accounts from a shell that already works (typically sudo mysql on Debian, which authenticates via the socket):
SELECT user, host, plugin FROM mysql.user ORDER BY user, host;+------+-----------+-----------------------+
| user | host | plugin |
+------+-----------+-----------------------+
| root | localhost | auth_socket |
| app | % | caching_sha2_password |
| app | localhost | caching_sha2_password |
+------+-----------+-----------------------+The plugin column is the second decisive fact: auth_socket ignores passwords entirely, mysql_native_password uses the classic hash, caching_sha2_password is the MySQL 8 default and needs a client that supports it.
Reproduce a 1045 on purpose, then fix it:
-- as an admin
CREATE USER 'demo'@'localhost' IDENTIFIED BY 'correct-horse';
-- client connects with the wrong password
-- ERROR 1045 (28000): Access denied for user 'demo'@'localhost' (using password: YES)
ALTER USER 'demo'@'localhost' IDENTIFIED BY 'battery-staple';Whether a connection is socket or TCP decides which grant applies:
| Client invocation | Transport | Host matched |
|---|---|---|
mysql -u root | socket | 'root'@'localhost' |
mysql -h 127.0.0.1 -u root | TCP | 'root'@'127.0.0.1' or 'root'@'%' |
mysql -h localhost -u root | socket (unless --protocol=tcp) | 'root'@'localhost' |
mysql --protocol=tcp -h localhost | TCP | 'root'@'localhost' via name resolution |
| From a container | TCP | 'root'@'%' or the container subnet |
Key Comparison Table
What the parenthesised hint tells you:
| Message ending | Meaning | First action |
|---|---|---|
(using password: YES) | password sent, rejected | check grant host and plugin |
(using password: NO) | no password sent | set one, or use socket auth |
ERROR 1045 (28000) | authentication failed | grants and credentials |
ERROR 1044 (42000) | authenticated, no privilege on the database | GRANT on that schema |
ERROR 2002 (HY000) | cannot reach the socket | server not running, wrong socket path |
ERROR 2003 (HY000) | cannot reach TCP port | host, port, firewall, bind-address |
The authentication plugins and their client requirements:
| Plugin | Default in | Password sent over TLS | Old clients |
|---|---|---|---|
mysql_native_password | MySQL 5.7 and earlier | hashed | universally supported |
caching_sha2_password | MySQL 8.0+ | required unless RSA key configured | needs a modern client |
auth_socket | Debian/Ubuntu root | not used at all | only local socket |
sha256_password | optional | required | limited support |
Practical Edge Cases, Error Fixes & Deep Dive Code Examples
1. localhost is not 127.0.0.1
mysql -h 127.0.0.1 fails while mysql succeeds, because the first matches 'root'@'127.0.0.1' and the second matches 'root'@'localhost'. Create the grant that matches the transport the application uses:
CREATE USER 'app'@'127.0.0.1' IDENTIFIED BY 'strong-password';
GRANT SELECT, INSERT, UPDATE, DELETE ON appdb.* TO 'app'@'127.0.0.1';
FLUSH PRIVILEGES;FLUSH PRIVILEGES is only required after direct edits to the grant tables; CREATE USER and GRANT apply immediately. Keep it for scripted recovery from --skip-grant-tables.
2. MySQL 8.4 and the removal of mysql_native_password
Connecting from an old PHP or Java driver produces 1045 with a valid password because the client cannot do caching_sha2_password. Two legitimate fixes:
-- option A: give this one account the legacy plugin (MySQL 8.0/8.1)
ALTER USER 'legacy'@'%' IDENTIFIED WITH mysql_native_password BY 'strong-password';
-- option B: set a password that caching_sha2 can send over TLS, and upgrade the client
ALTER USER 'legacy'@'%' IDENTIFIED WITH caching_sha2_password BY 'strong-password';mysql_native_password is disabled by default in MySQL 8.4 and removed in MySQL 9, so option A is a migration step, not a destination. Upgrading the driver — mysqlclient 2.2+, Connector/J 8.0+, mysql2 3.x — is the durable fix.
3. Docker: root exists only for the socket
The official image, with MYSQL_ROOT_PASSWORD set, creates 'root'@'%'. An image that ran mysqld --initialize differently may only have 'root'@'localhost'. Enter the container and check before changing passwords:
docker compose exec db mysql -uroot -p -e "SELECT user, host, plugin FROM mysql.user;"
docker compose exec db mysql -uroot -p -e "CREATE USER IF NOT EXISTS 'root'@'%' IDENTIFIED BY 'pw'; GRANT ALL ON *.* TO 'root'@'%' WITH GRANT OPTION;"Creating a second root grant for % keeps the socket account intact, which matters when the container health check connects locally.
4. The Debian auth_socket surprise
On Ubuntu, root@localhost uses auth_socket, so mysql -u root -p fails with 1045 no matter what password you type, while sudo mysql works because the OS user is verified:
sudo mysqlALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'new-strong-password';After that change sudo mysql stops working and mysql -u root -p starts. Choose one and write it down; mixing the two is how a server ends up with no working admin path.
5. Anonymous users that shadow a real account
An ''@'localhost' row matches connections before 'app'@'localhost' in some sort orders, producing 1045 for a correct password:
SELECT user, host FROM mysql.user WHERE user = '';
DROP USER ''@'localhost';Distribution packages sometimes ship anonymous accounts. Removing them is safe and removes a class of authentication confusion.
Common Compilation/Runtime Mistakes & Version Gotchas
Retrying with --skip-grant-tables as the fix. It disables authentication for everyone, including remote clients, and leaves the server wide open until restarted. Use it only on an isolated host, set the password, and restart immediately.
Changing the password without the host. ALTER USER 'root'@'localhost' does not touch 'root'@'%'. Always name both when the application connects over TCP.
Blaming the network for 1045. A firewall or a wrong port produces 2003 or a timeout, not 1045. Receiving 1045 proves the connection reached MySQL and failed at authentication, so keep looking at grants.
Version differences worth knowing. MySQL 8.0 made caching_sha2_password the default, MySQL 8.4 disabled mysql_native_password, and MariaDB never adopted caching_sha2_password in the same form, so the same ORM configuration behaves differently against the two servers. MySQL 8.0.30 and later also changed --require_secure_transport defaults in some managed offerings, so a password that works locally fails against a managed instance that requires TLS — the client must add --ssl-mode=REQUIRED.
Formatting the diagnostic query. Run the grant inspection through the SQL formatter so the output stays readable in a ticket, and print the connection environment with the env viewer to confirm which host, port, and socket the client is actually using.
Reference material: MariaDB access denied error 1045 (the same account model and error text as MySQL) and the official MySQL Docker image documentation for MYSQL_ROOT_PASSWORD and MYSQL_ROOT_HOST behaviour.
Official references
primary documentationThe SQL documentation that defines the behaviour this guide explains.
Try matching browser tools
All toolsSmall utilities that fit SQL work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related SQL guides
All SQL guidesSQLite: database is locked (SQLITE_BUSY) under concurrent writes
SQLite allows one writer. Set a busy timeout, switch to WAL, start write transactions as IMMEDIATE, and reduce to a single writer connection.
SQL Window Functions: ROW_NUMBER, RANK, DENSE_RANK, and LEAD/LAG Explained
How the four ranking and offset functions differ on ties, why the default RANGE frame makes running totals repeat, and how LAG builds gap-and-island.
Postgres: column does not exist (SQLSTATE 42703)
42703 means the identifier you wrote is not the one that exists. Check case folding, alias scope, search_path and migration drift.
PostgreSQL: canceling statement due to statement timeout
The statement was canceled by statement_timeout. Find whether the role, the pooler or the client set it, then scope a longer budget to that job.
More in SQL
All SQL guidesThe guides filed next to this one in SQL, in library order.