W3docs

SQLSTATE[HY000] [1698] Access denied for user 'root'@'localhost'

This error message typically indicates that the MySQL server is unable to authenticate the user 'root'@'localhost' with the provided password.

This error message typically indicates that the MySQL server is unable to authenticate the user root@localhost with the provided password.

In modern MySQL (5.7+) and MariaDB, this is almost always caused by the auth_socket (or unix_socket) authentication plugin. This plugin requires OS-level authentication (running commands with sudo) instead of a password, which breaks standard application connections.

To resolve this, you need to switch the root user to a password-based authentication plugin. Follow these steps:

  1. Access the MySQL command line using sudo to bypass the password requirement:
    sudo mysql
  2. Change the authentication plugin for the root user to mysql_native_password (or caching_sha2_password for MySQL 8.0+):
    ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_new_password';
    FLUSH PRIVILEGES;
    EXIT;
  3. Restart the MySQL service to apply changes:
    sudo systemctl restart mysql
  4. Test the connection using the new password:
    mysql -u root -p

If you are still unable to connect, verify that the MySQL service is running and check the server logs or configuration files (typically /etc/mysql/my.cnf or /etc/mysql/mysql.conf.d/mysqld.cnf) for additional details.