In this article, we are going to create a new MySQL user and discuss all related aspects of it. And in the process, we will solve MySQL Error 1045 (Access denied for user).
Error Details
Error Number | 1045 |
Error Symbol | ER_ACCESS_DENIED_ERROR |
SQLSTATE | 28000 |
Message | Access denied for user ‘%s’@’%s’ (using password: %s) |
Possible Reason | 1. Access/Permission not granted for the user. 2. Remote access to the server is not allowed for that user. 3. Remote access to the server is not allowed at all (No remote access allowed). |
Error Type | Server-Side Error |
Let’s see how can we address all these possible reasons and solve the issue.
Using all the following queries, we are creating a user named myuser and password will be test1234.
Login to MySQL and execute the following query.
Create user
Create a new user that can be accessed from localhost:
CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'test1234';
Grant permission
Give all permission to the user:
GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'localhost';
To give the user permission to only one database (say, DB name is testdb) use the following command:
GRANT ALL PRIVILEGES ON tested.* TO 'myuser'@'localhost';
Then you need to flush/reload the privilege:
FLUSH PRIVILEGES;
Allow Remote access
If you want to change the host and want it to be accessed from any computer:
UPDATE mysql.user SET Host=‘%’ WHERE Host='localhost' AND User='myuser';
Also, use the following command to reflect the permission changes immediately:
FLUSH PRIVILEGES;
Additionally, if you want it to access the MySQL server from a remote computer, then in the MySQL configuration file, comment the following line (if it is not already commented)
# bind 127.0.0.1
Related Errors
You may also face a few other MySQL errors due to the same reason. The same fix will work for these MySQL errors too.
Here is the list of errors related to MySQL Error: 1045:
Error Number | Error Symbol | SQLSTATE | Message | Error Type |
---|---|---|---|---|
1044 | ER_DBACCESS_DENIED_ERROR | 42000 | Access denied for user ‘%s’@’%s’ to database ‘%s’ | Server-Side Error |
1698 | ER_ACCESS_DENIED_NO_PASSWORD_ERROR | 28000 | Access denied for user ‘%s’@’%s’ | Server-Side Error |
MY-010925 | ER_ACCESS_DENIED_ERROR_WITHOUT_PASSWORD | HY000 | Access denied for user ‘%s’@’%s’ | Server-Side Error |
MY-010926 | ER_ACCESS_DENIED_ERROR_WITH_PASSWORD | HY000 | Access denied for user ‘%s’@’%s’ (using password: %s) | Server-Side Error |
This solved the error. Thanks for sharing this info