Sudo mysql ERROR 1045 (28000) : Access denied for user 'root'@'localhost'

author

Dev

. 0 min read

Follow

When this Error comes : 

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

Here is a simple way to fix mysql Access which is denied for user 'root'@'localhost' ( use below command ) 

mysql -u root -p

Enter password: my_password

Lost Mysql password or Got Locked 

If you lost your mysql password or get locked by extra command used in past. Then this is the last option you can try 

  •  Open file through vs code /etc/mysql/debian.cnf (file location) or through command line : sudo cat /etc/mysql/debian.cnf 

Then you will find default details like below - 

# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host     = localhost
user     = debian-sys-maint
password = KbgKUX4usUDEA2
socket   = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host     = localhost
user     = debian-sys-maint
password = KbgKUX4usUDEA2
socket   = /var/run/mysqld/mysqld.sock

  •  Above we can see password just we are going to use (KbgKUX4usUDEA2) that in the prompt-
mysql -u debian-sys-maint -p

Enter password: 

now provide password (KbgKUX4usUDEA2).

  • Change the mysql password then (by using below SQL query )
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'NewPassword';

Replace 'NewPassword' with your password above in SQL query. 

FLUSH PRIVILEGES;
  • Now exit from MySQL and login again as-
mysql -u root -p

Enter password: 

Now provide new password which were created using SQL Query. That's all, we have new password for further uses.

It worked Well, hope it will help!