Fix "ERROR 1045 (28000): Access denied for user 'root'@'localhost'" (MySQL, MariaDB)
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
- ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
- SQLSTATE[HY000] [1045] Access denied for user 'laravel'@'172.18.0.3' (using password: YES)
- ERROR 1698 (28000): Access denied for user 'root'@'localhost'
- mysqli_sql_exception: Access denied for user 'app'@'localhost' (using password: YES)
The MySQL or MariaDB server was reached, but it rejects the user + host + password combination. Check the password and that the account exists for the host you connect from ('app'@'localhost' is not 'app'@'%'), then reset it with ALTER USER from an admin account.
What this error means
In MySQL and MariaDB an account is not just a name: it is a 'user'@'host' pair. On connect, the server looks for the mysql.user row matching your name and the address you come from, then checks the password with the account’s authentication plugin. If no row matches, or the password is wrong, it returns error 1045 with SQLSTATE 28000. (using password: YES) means a password was sent; NO means it was empty. Error 1698 shows up when root uses auth_socket/unix_socket and accepts no password at all.
Common causes
- Wrong or stale password:
.envchanged but configuration still cached (php artisan config:cache), or a special character (#, space) not wrapped in quotes. - Wrong host: the account exists for
localhost, but the app connects from a Docker container (172.18.0.3) or via127.0.0.1over TCP. - root using socket authentication (Ubuntu, Debian):
rootonly opens withsudo mysql, no password is accepted (error 1698). - Docker volume already initialized:
MYSQL_ROOT_PASSWORDorMARIADB_ROOT_PASSWORDis only read on first start, on an empty volume. - User never created, or created on another server (a local MySQL answers on port 3306 instead of the container’s).
Step-by-step fix
-
Reproduce on the command line
Test exactly the same parameters as the application. Read the host shown in the error: it is the one the server sees, not the one you think you are using.
mysql -u app -p -h 127.0.0.1 -P 3306 appdb -
Log in as admin and list accounts
On Ubuntu/Debian,
sudo mysqlopensrootover the socket without a password. In Docker:docker compose exec db mysql -u root -p. Then check which user/host pairs exist and with which plugin.SELECT user, host, plugin FROM mysql.user ORDER BY user, host; -
Reset the password
ALTER USERworks on MySQL 5.7+, 8.x and MariaDB 10.2+. No need forFLUSH PRIVILEGESafter anALTER USER.ALTER USER 'app'@'localhost' IDENTIFIED BY 'A-Long-Random-Password-42'; -
Create the account for the right host
If the app comes from a container or another machine, create the account for that host. Avoid
'%'on an exposed server: restrict it to the Docker network ('172.18.%') or the app’s IP, and grant rights on its database only.CREATE USER 'app'@'172.18.%' IDENTIFIED BY 'A-Long-Random-Password-42'; GRANT ALL PRIVILEGES ON appdb.* TO 'app'@'172.18.%'; -
Fix the application configuration
Wrap in quotes a password containing
#, spaces or$, then clear the configuration cache (Laravel).# .env DB_HOST=127.0.0.1 DB_USERNAME=app DB_PASSWORD="p#ss word" php artisan config:clear -
Docker: change the password without wiping everything
Changing
MYSQL_ROOT_PASSWORDincompose.yamlhas no effect on an existing volume. Change the password inside the container with the old one.docker compose down -valso resets it, but deletes all the database data.docker compose exec db mysql -u root -p -e "ALTER USER 'root'@'%' IDENTIFIED BY 'New-Root-Password-42';"
Did this fix work for you?
Confirm it in one click: it will rise for the next developers stuck on the same error.
Keep it from coming back
- Create one dedicated account per application, limited to its database and host, instead of using
root. - Keep credentials in
.env(not committed) and avoidconfig:cachein development. - In Docker, create the app user with
MYSQL_USER/MYSQL_PASSWORD/MYSQL_DATABASEon the first initialization.
Frequently asked questions
Why does sudo mysql work but not mysql -u root -p?
On Ubuntu and Debian, root uses auth_socket (MySQL) or unix_socket (MariaDB): it is authenticated by the system user, not by a password. Keep that mode and create another account for your apps.
What does "using password: NO" mean?
No password was sent: the variable is empty, misnamed (DB_PASSWORD) or the -p option was forgotten.
I lost the MySQL root password, what now?
Stop the service and restart it with --skip-grant-tables --skip-networking, log in, run FLUSH PRIVILEGES; then ALTER USER, and restart normally. That mode disables all authentication: never leave it on.
Useful tools for this error
More Databases (SQL) errors
- Fix "ERROR 1062 (23000): Duplicate entry for key" (MySQL, MariaDB, PostgreSQL)
- Fix "Deadlock found when trying to get lock; try restarting transaction" (MySQL, PostgreSQL)
- Fix FATAL: password authentication failed for user "postgres" (PostgreSQL)
- Fix "ERROR 1040: Too many connections" (MySQL) and "too many clients already" (PostgreSQL)
- Fix "ERROR 2006 (HY000): MySQL server has gone away"
- Fix "ERROR 1054 (42S22): Unknown column in 'field list'" (MySQL)
- Fix "ERROR: relation "users" does not exist" (PostgreSQL)