Home > Database > Mysql Tutorial > Why Am I Getting a 'Connect Failed: Access Denied' Error When Connecting to MySQL with the Root User?

Why Am I Getting a 'Connect Failed: Access Denied' Error When Connecting to MySQL with the Root User?

Susan Sarandon
Release: 2024-12-15 12:12:12
Original
344 people have browsed it

Why Am I Getting a

"Connect Failed: Access Denied" Error When Connecting to MySQL with Root User

When attempting to connect to a MySQL database using a PHP function, users may encounter the error: "Connect failed: Access denied for user 'root'@'localhost' (using password: YES)."

Reason:

This error typically occurs when the root user does not have the necessary permissions to access the database or when the password is incorrect.

Solution:

To resolve this issue, follow these steps:

  1. Create a Non-Root User:

    • Log in to MySQL using the root username:
      mysql -u root -p -h localhost
    • Create a new user with appropriate permissions:
      CREATE USER 'new_user'@'localhost' IDENTIFIED BY 'new_password';
  2. Create the Database:

If the database has not yet been created, use the following command:
CREATE DATABASE shop;

  1. Grant Privileges:

    • Grant the new user privileges on the database:
      GRANT ALL PRIVILEGES ON shop.* TO 'new_user'@'localhost';
  2. Log Out and In:

    • Log out of the root user:
      quit;
    • Log in using the newly created user:
      mysql -u new_user -p -h localhost
  3. Import Data:

    • Rebuild the database using a script, such as shop.sql.
  4. Update PHP Function:

    • Modify the PHP function to use the new username and password:
      $conn = new mysqli("localhost", "new_user", "new_password", "shop");

By following these steps, you can establish a successful connection to the MySQL database and prevent the "Connect failed: Access denied" error.

The above is the detailed content of Why Am I Getting a 'Connect Failed: Access Denied' Error When Connecting to MySQL with the Root User?. 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