Home >Database >Mysql Tutorial >How Can I Fix the 'Access Denied for User 'root'@'localhost'' MySQL Error?

How Can I Fix the 'Access Denied for User 'root'@'localhost'' MySQL Error?

Linda Hamilton
Linda HamiltonOriginal
2025-01-21 01:06:08797browse

How Can I Fix the

Fixing the MySQL "Access Denied" Error for 'root'@'localhost'

The error "Access denied for user 'root'@'localhost' (using password: YES)" can be tricky to solve. Many online solutions are overly complex. This guide offers a straightforward fix.

The Solution:

  1. Open your terminal and connect to MySQL as root:

    <code class="language-bash">sudo mysql</code>
  2. Execute this command to reset the root password and authentication method:

    • For MySQL:

      <code class="language-sql">ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';</code>
    • For MariaDB:

      <code class="language-sql">ALTER USER 'root'@'localhost' IDENTIFIED VIA mysql_native_password USING PASSWORD('root');</code>

    Replace 'root' with your desired password.

  3. Exit the MySQL console (Ctrl D or exit).

Understanding the Fix:

This method changes the authentication plugin to mysql_native_password and sets the root password. You have the flexibility to choose your own password.

Further Reading:

For more detailed information, consult the official MySQL and MariaDB documentation. Remember to exit the MySQL console using Ctrl D or the exit command.

The above is the detailed content of How Can I Fix the 'Access Denied for User 'root'@'localhost'' MySQL Error?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn