Skip to content
FR
  1. LuzineDebug
  2. Databases (SQL)
  3. ERROR 1045 (28000): Access denied for user 'root…
Databases (SQL) Beginner ≈ 10 min to fix

Fix "ERROR 1045 (28000): Access denied for user 'root'@'localhost'" (MySQL, MariaDB)

Error message
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)
In short

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.

Be the first to confirm this fix

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

  1. Wrong or stale password: .env changed but configuration still cached (php artisan config:cache), or a special character (#, space) not wrapped in quotes.
  2. Wrong host: the account exists for localhost, but the app connects from a Docker container (172.18.0.3) or via 127.0.0.1 over TCP.
  3. root using socket authentication (Ubuntu, Debian): root only opens with sudo mysql, no password is accepted (error 1698).
  4. Docker volume already initialized: MYSQL_ROOT_PASSWORD or MARIADB_ROOT_PASSWORD is only read on first start, on an empty volume.
  5. User never created, or created on another server (a local MySQL answers on port 3306 instead of the container’s).

Step-by-step fix

  1. 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.

    bash
    mysql -u app -p -h 127.0.0.1 -P 3306 appdb
  2. Log in as admin and list accounts

    On Ubuntu/Debian, sudo mysql opens root over 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.

    sql
    SELECT user, host, plugin FROM mysql.user ORDER BY user, host;
  3. Reset the password

    ALTER USER works on MySQL 5.7+, 8.x and MariaDB 10.2+. No need for FLUSH PRIVILEGES after an ALTER USER.

    sql
    ALTER USER 'app'@'localhost' IDENTIFIED BY 'A-Long-Random-Password-42';
  4. 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.

    sql
    CREATE USER 'app'@'172.18.%' IDENTIFIED BY 'A-Long-Random-Password-42';
    GRANT ALL PRIVILEGES ON appdb.* TO 'app'@'172.18.%';
  5. Fix the application configuration

    Wrap in quotes a password containing #, spaces or $, then clear the configuration cache (Laravel).

    bash
    # .env
    DB_HOST=127.0.0.1
    DB_USERNAME=app
    DB_PASSWORD="p#ss word"
    
    php artisan config:clear
  6. Docker: change the password without wiping everything

    Changing MYSQL_ROOT_PASSWORD in compose.yaml has no effect on an existing volume. Change the password inside the container with the old one. docker compose down -v also resets it, but deletes all the database data.

    bash
    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 avoid config:cache in development.
  • In Docker, create the app user with MYSQL_USER / MYSQL_PASSWORD / MYSQL_DATABASE on 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

All Databases (SQL) errors →