Home > Database > Mysql Tutorial > How to Resolve MySQL Error 1045: Access Denied?

How to Resolve MySQL Error 1045: Access Denied?

DDD
Release: 2024-11-28 18:06:12
Original
981 people have browsed it

How to Resolve MySQL Error 1045: Access Denied?

Error: SQLSTATE[HY000] [1045] Access Denied for User "username"@'localhost'

Overview

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.

Inspecting User Configuration

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;
Copy after login

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');
Copy after login

Granting Privileges

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';
Copy after login

2. Refreshing Privileges

After modifying privilege tables, execute a FLUSH PRIVILEGES statement to make the changes effective:

FLUSH PRIVILEGES;
Copy after login

Additional Troubleshooting

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template