Home >Database >Mysql Tutorial >How Can I Quickly Fix the MySQL 'Access Denied' Error for the Root User?

How Can I Quickly Fix the MySQL 'Access Denied' Error for the Root User?

Linda Hamilton
Linda HamiltonOriginal
2025-01-21 00:47:09818browse

How Can I Quickly Fix the MySQL

A Simple Fix for the MySQL "Access Denied" Error

Encountering the dreaded "Access denied for user 'root'@'localhost'" error in MySQL? This guide offers a quick and easy solution, avoiding the complex procedures often found online.

Here's how to resolve this issue:

First, access the MySQL console using the following command:

<code class="language-bash">sudo mysql</code>

Next, execute the appropriate query within the MySQL console. Choose the query based on your database system:

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>

These commands perform two crucial actions:

  • They change the authentication plugin to mysql_native_password.
  • They set the root password to 'root' (you can easily change this to your desired password within the query).

After running the command, you should be able to log in using the root user and your new password.

For comprehensive details, consult the official MySQL or MariaDB documentation. To exit the MySQL console, press Ctrl D or type exit.

The above is the detailed content of How Can I Quickly Fix the MySQL 'Access Denied' Error for the Root User?. 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