Home >Database >Mysql Tutorial >How to Fix 'Access denied for user 'root@localhost'' MySQL Error?

How to Fix 'Access denied for user 'root@localhost'' MySQL Error?

DDD
DDDOriginal
2024-12-13 12:43:10934browse

How to Fix

Error: "Access denied for user 'root@localhost' (using password:NO)" When Accessing MySQL

This error occurs when attempting to access the MySQL server as the 'root' user without a password or using an incorrect one.

To resolve this issue, follow these steps:

  1. Stop the MySQL Service: Navigate to Windows Services and stop the MySQL service.
  2. Reset the Root Password:

    • Open CMD and navigate to the MySQL bin directory (e.g., C:program filesMySQLMySQL Server 5.1bin).
    • Execute the following command:

      mysqld --defaults-file="C:\program files\MySQL\MySQL Server 5.1\my.ini" --init-files=C:\root.txt
  3. Start the MySQL Service: Restart the MySQL service from Windows Services.
  4. Connect to MySQL: Open CMD and type the following:

    mysql -u root -p

    Enter the password you set in step 2.

  5. Grant Privileges: If you encounter the error "Access denied for user 'root@localhost' (using password:YES)", use the following command to grant all privileges to the root user:

    GRANT ALL PRIVILEGES ON *.* TO root@localhost IDENTIFIED BY 'YourNewPassword' WITH GRANT OPTION;

    Flush the privileges with:

    FLUSH PRIVILEGES;
  6. Exit and Restart MySQL: Exit the MySQL shell and restart the MySQL service in normal mode.

You should now be able to successfully log in to MySQL as the 'root' user using the password you set.

The above is the detailed content of How to Fix 'Access denied for user 'root@localhost'' MySQL Error?. 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