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
Release: 2025-01-21 01:06:08
Original
735 people have browsed it

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>
    Copy after login
  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>
      Copy after login
    • For MariaDB:

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

    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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template