The MySQL error, Access denied for user ‘root’@’localhost’, usually appears for new installations of MySQL when you try to connect to MySQL with the root user.
When you log in using the below command:
mysql -u root -p
You may get the following error message:
ERROR 1045: Access denied for user: ‘root@localhost’ (Using password: YES)
This error is got because the user ‘root’ does not have the requires access to the MySQL database. It may occur due to different reasons like:
- A user who does not exist on the MySQL server tries to access the database.
- Privilege Issue for the user.
- Incorrect username or password.
Solution for, Access denied for user ‘root’@’localhost’
Step 1: To reset the root password follow the below steps:
1. Open and edit the MySQL configuration file (my.cnf). The file will be located at /etc/my.cnf or /etc/mysql/my.cnf
2. On the my.cnf file, under [mysqld], add skip-grant-tables to remove all user access
Add skip-grant-tables under [mysqld] section of my.cnf file.
NOTE: Using skip-grant-table is very dangerous. As it allows any user to log in with full access to the database. This is used only because there is no other option to login to the database.
3. Restart MySQL
4. Now login to MySQL using below command
mysql -u root
5. Run the below command to flush all privileges
mysql> flush privileges;
6. Set new password using the command below:
ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘new_password’;
Replace new_password with the password you want set for root.
7. Now go back to /etc/my.cnf and remove skip-grant-tables entry that we added in point 2 above. This is a very important step and should not be missed.
8. Finally restart MySQL
9. Now you should be able to successfully login with the newly set password.
Step 2: If the user is still getting the error. It might be due to no global privilege set for the user. This can be set using the below command:
GRANT ALL PRIVILEGES ON database.* TO 'user'@'localhost';
Trying all the above steps should fix the MySQL, Access denied for user ‘root’@’localhost’ error. If you still face the issue then have your system administrator check this further.
If you want all your KPI‘s or Business metrics monitored on a single dashboard, join our BETA.