Error: #1045 - Access Denied for User 'root'@'localhost' (Using Password: YES)
Issue
Users encounter an access denial error when attempting to connect to MySQL using the root user and a password, typically via the MySQL console or phpMyAdmin. The config file config.inc.php is configured with the correct credentials, but the login still fails.
Solution
To resolve this issue, follow these steps:
-
Change the Root Password:
Use the command prompt to run mysql -u root -p to connect to MySQL using the root user. If prompted for a password, enter your existing password or leave it blank if the AllowNoPassword option in config.inc.php is set to true.
-
Update Password:
Once connected, execute the following command to update the root password:
UPDATE mysql.user SET authentication_string=PASSWORD('MyNewPass') WHERE User='root';
Replace 'MyNewPass' with your new password.
-
Edit config.inc.php:
Update the password field in the Servers section of config.inc.php with your new password:
$cfg['Servers'][$i]['password'] = 'MyNewPass';
-
Restart MySQL Service:
Stop and then restart the MySQL service.
-
Verify Connection:
Attempt to log in to MySQL again using the updated password.
Additional Notes
- If MySQL version 5.7 or later is used, authentication_string should be used instead of Password in the SQL update command.
- If the issue persists, verify that the MySQL service is running correctly and that there are no firewall or antivirus restrictions blocking connections.
- Consider creating a separate user with limited privileges for use with phpMyAdmin, rather than using the root user.
The above is the detailed content of Why Am I Getting an \'Access Denied\' Error When Connecting to MySQL as Root?. For more information, please follow other related articles on the PHP Chinese website!