If you’re getting error 1045 that reads something like “Access denied for user ‘root’@’localhost,’ “you’re trying to log in to MySQL without the proper credentials. It usually happens when we provide the wrong password. But there could also be another cause.

For example, we could be trying to do something as the root user that requires a password, but the root user hasn’t yet set it. To fix this issue, provide the correct password when connecting to MySQL.

Also read: workforce software monday

Example of Error

Here’s an example of code that produces the error:

Mysql -uhomer -pWrongPwd

Result:

ERROR 1045 (28000): Access denied for user ‘homer’@’localhost’ (using password: YES)

In that example, I provided the wrong password.

Here’s another example that can cause the same error:

mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql

Result:

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

I was trying to import named time zones into MySQL using the root user, and I provided a password, but the root user hadn’t yet set its password.

Solution:

The most obvious solution is to ensure we pass the correct credentials when connecting to MySQL. In the first case above, I provided the wrong password.

Here’s an example of providing the correct password:

MySQL -uhomer -pWheelyStrongPwd

Result:

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

Your MySQL connection id is 80

Server version: 8.0.29 Homebrew

Copyright (c) 2000, 2022, Oracle and its affiliates.

Oracle is a recorded trademark of Oracle Corporation and its

affiliates. Other names may remain trademarks of their respective

owners.

Type ‘help;’ or ‘\h’ for help. Kind ‘\c’ to clear the current input statement.

This time I was able to log in successfully using the homer account.

Also read: error in match.names(clabs, names(xi)) : names do not match previous names

How to fix Error 1045 (28000)?How to fix 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)

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

3.      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. If 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) The host doesn’t 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.

Conclusion

Apart from all this, ensure the host contains the correct IP address and hostname to avoid the Error 1045 (28000) access denied for user ‘root’@’localhost’ (using password: yes). To fix this issue, provide the correct password when connecting to MySQL.