Register Login

Error 1045 (28000) access denied for user root localhost

Updated Dec 09, 2020

MySQL users often face an issue called Error 1045 (28000) access denied for user 'root'@'localhost' (using password: yes). This usually occurs when you enter an incorrect password or password for your database. Fixing these credentials can resolve this error in no time.

In this article, we will look at the ways to resolve this error.

How to fix “Error 1045 (28000) access denied for user 'root'@'localhost' (using password: yes)”?

The error looks something like this - 

mysql -uroot -proot

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

It arises when you perform a fresh installation of MySQL and try to login with a password. The default password of MySQL is blank () (i.e. empty string).

So, you can login to the MySQL server using the same password.

Example  

>mysql -uroot -p

Enter password:

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 9

Server version: 10.4.11-MariaDB mariadb.org binary distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

mysql login

The best practice is to change the password after the new installation.

Set root user password

You must set the root user password after performing the new installation. Here is the code to set it –

Login as user root with blank password

>mysql -u root

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'abc';

Now the new password for root user is abc.

How to fix the Error 1045 (28000)?

Let us look at the ways to fix this problem – 

  1. Enter the correct credentials

The primary method to fix this error is to enter the correct username and password using the following command –

mysql –u username –p       
  1. Ensure the user is correct

Sometimes, the user you might be trying to access does not exist on the MySQL server. You can check if the user exists using the following code-

MariaDB [(none)]> select user from mysql.user where user like '%root%';

+------+

| User |

+------+

| root |

| root |

| root |

+------+

3 rows in set (0.001 sec)  

select user root

If the user does not exist, create it with the desired username.

  1. Enter the correct host name

You might be trying to access the server from a host that is different from the defined host name. You will encounter Error 1045 in this case. You can use this code to view details of the user –

To fix this, you can update the host name for the user using the code below –

mysql> mysql -u root -pabc -h <IP> -P 3306

You might encounter the error in due to the following scenarios –   

Entered wrong password

>mysql -uroot -pssssss

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

Host doesn’t not have permission to connect database

This is a very common error that occurs while connecting to a remote database. While connecting to such a database we need to give access to the HOST IP ADDRESS to connect to it.

This is the IP Address of the source system which connects to the database server.

If access is not given, then run the given command –

CREATE USER 'dbuser1'@'< Host IP >' IDENTIFIED VIA mysql_native_password USING '***';GRANT ALL PRIVILEGES ON *.* TO 'dbuser1'@'< Host IP >' REQUIRE NONE WITH GRANT OPTION MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0;

Conclusion

Apart from all this, make sure the host contains the correct IP address and host name, to avoid the Error 1045 (28000) access denied for user 'root'@'localhost' (using password: yes).

 

 


×