Home >Database >Mysql Tutorial >Why Am I Getting 'Access denied for user 'root'@'localhost'' in MySQL?

Why Am I Getting 'Access denied for user 'root'@'localhost'' in MySQL?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-20 05:51:12598browse

Why Am I Getting

Access Denied for User 'root'@'localhost' on MySQL

Deploying a web application on a server may lead to an error like "Access denied for user 'root'@'localhost' (using password: YES)" when accessing a MySQL database. This error message usually implies that the root user on the local machine is not authorized to connect to the database from the application.

To resolve this issue, ensure that the root user has the necessary privileges from the local machine. To do this, connect to the database from the command line using mysql -u root -p. Once connected, grant access to the root user from localhost by running the following query:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY 'your_password';

Replace 'your_password' with the root user's password. After executing this query, flush the privileges table to ensure the changes are applied immediately:

FLUSH PRIVILEGES;

Now, the root user should be able to connect to the database from the application without any errors.

The above is the detailed content of Why Am I Getting 'Access denied for user 'root'@'localhost'' in MySQL?. 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