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:
Open your terminal and connect to MySQL as root:
<code class="language-bash">sudo mysql</code>
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.
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!