Home >Database >Mysql Tutorial >How to Resolve MySQL Error: 'Access Denied for User 'root'@'localhost''?

How to Resolve MySQL Error: 'Access Denied for User 'root'@'localhost''?

DDD
DDDOriginal
2025-01-21 01:01:09359browse

How to Resolve MySQL Error:

Troubleshooting MySQL Error: "Access Denied for user 'root'@'localhost'"

Encountering the "Access denied for user 'root'@'localhost'" error in MySQL? This means your MySQL server is blocking login attempts from the root user. This is often due to incorrect credentials or misconfigured settings.

Resolution:

  1. Updating Authentication and Password:

Use the MySQL console (access via sudo mysql) to run this SQL command:

<code class="language-sql">-- For MySQL
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_new_password';

-- For MariaDB
ALTER USER 'root'@'localhost' IDENTIFIED VIA mysql_native_password USING PASSWORD('your_new_password');</code>

Replace 'your_new_password' with your chosen password. This command switches the authentication plugin to mysql_native_password and sets a new password.

  1. Testing the Connection:

After executing the command, close the MySQL console and try logging in again with the root user and your new password. The connection should now be successful.

Important Considerations:

  • This method avoids restarting the MySQL server or altering mysqld privileges directly.
  • For more detailed information, consult the official MySQL or MariaDB documentation.

The above is the detailed content of How to Resolve MySQL Error: 'Access Denied for User 'root'@'localhost''?. 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