This error occurs when attempting to access a database from a specific user and host but being denied due to invalid credentials or missing user privileges.
1. Database User Existence
Verify that the specified user ("test2") exists in the MySQL database. Run the following query:
SELECT user, host FROM mysql.user;
Look for a row where "user" is "test2" and "host" is "localhost" or a wildcard ("%"), as indicated in the error message.
2. Password Verification
If the database user exists, check if the password matches the one used in the connection request. Change the password if necessary:
SET PASSWORD FOR 'test2'@'localhost' = PASSWORD('new_password');
1. Checking Permissions
Confirm that the database user has sufficient privileges on the desired database objects. For example, to grant SELECT privileges on the "jobs" database:
GRANT SELECT ON jobs.* TO 'test2'@'localhost';
2. Refreshing Privileges
After modifying privilege tables, execute a FLUSH PRIVILEGES statement to make the changes effective:
FLUSH PRIVILEGES;
1. Host Mismatch
Ensure that the host specified in the connection request ("localhost") matches the host associated with the user in the database. Change the host to "localhost" or "%".
2. Firewall Blocking
Disable the firewall temporarily to check if it's blocking access to MySQL on the specified port. Adjust the port in the connection request if necessary.
3. Incorrect Username or Password
Double-check the database username and password used in the connection request.
The above is the detailed content of How to Resolve MySQL Error 1045: Access Denied?. For more information, please follow other related articles on the PHP Chinese website!