Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

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.

~5 min readSQL
#mysql#authentication#grants#docker
Bold white headline reading ACCESS DENIED with a MySQL account crate stopped at an amber authentication gate on the right.

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 mysql container after the root password was set with MYSQL_ROOT_PASSWORD but the client connects over TCP while root is only granted for the socket.
  • Debian and Ubuntu packages where root@localhost uses auth_socket, so a password cannot work at all.
  • An old client (MySQL 5.7 CLI, PHP mysql_*, some ORMs) against MySQL 8.4, where caching_sha2_password is the default and the client does not support it.
  • A connection from 127.0.0.1 while the grant exists only for localhost, 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;
code
+------+-----------+-----------------------+ | 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:

sql
-- 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 invocationTransportHost matched
mysql -u rootsocket'root'@'localhost'
mysql -h 127.0.0.1 -u rootTCP'root'@'127.0.0.1' or 'root'@'%'
mysql -h localhost -u rootsocket (unless --protocol=tcp)'root'@'localhost'
mysql --protocol=tcp -h localhostTCP'root'@'localhost' via name resolution
From a containerTCP'root'@'%' or the container subnet

Key Comparison Table

What the parenthesised hint tells you:

Message endingMeaningFirst action
(using password: YES)password sent, rejectedcheck grant host and plugin
(using password: NO)no password sentset one, or use socket auth
ERROR 1045 (28000)authentication failedgrants and credentials
ERROR 1044 (42000)authenticated, no privilege on the databaseGRANT on that schema
ERROR 2002 (HY000)cannot reach the socketserver not running, wrong socket path
ERROR 2003 (HY000)cannot reach TCP porthost, port, firewall, bind-address

The authentication plugins and their client requirements:

PluginDefault inPassword sent over TLSOld clients
mysql_native_passwordMySQL 5.7 and earlierhasheduniversally supported
caching_sha2_passwordMySQL 8.0+required unless RSA key configuredneeds a modern client
auth_socketDebian/Ubuntu rootnot used at allonly local socket
sha256_passwordoptionalrequiredlimited 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:

sql
-- 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:

bash
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:

bash
sudo mysql
sql
ALTER 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 documentation

The SQL documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit SQL work — they run entirely in this browser tab, so nothing you paste leaves your device.

All SQL guides

More in SQL

All SQL guides

The guides filed next to this one in SQL, in library order.