how to solve sqlstate[hy000] [1045] access denied in laravel

In this section, we'll discuss how to solve SQLSTATE[HY000] [1045] Access denied for user 'abua'@'localhost' (using password: YES) and SQLSTATE[HY000] [1045] Access denied for user ''@'localhost' (using password: NO) while using laravel 9.


Example 1

Lets see why you got this error. SQLSTATE[HY000] [1045] Access denied for user ''@'localhost' (using password: NO).

Its simple you forgot to give database name, username and password.

.env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=
DB_USERNAME=
DB_PASSWORD=
laravel  SQLSTATE[HY000] [1045] Access denied for user ''@'localhost' (using password: NO)

laravel SQLSTATE[HY000] [1045] Access denied for user ''@'localhost' (using password: NO)

.env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database_name
DB_USERNAME=user_name
DB_PASSWORD=your_password


Example 2

Lets see why you got this error. SQLSTATE[HY000] [1045] Access denied for user 'abua'@'localhost' (using password: YES)

Some time you give DB_USERNAME or DB_PASSWORD wrong name that why you got 1045 password yes error.

laravel SQLSTATE[HY000] [1045] Access denied for user 'abua'@'localhost' (using password: YES)

laravel SQLSTATE[HY000] [1045] Access denied for user 'abua'@'localhost' (using password: YES)

.env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database_name
DB_USERNAME=add correct username
DB_PASSWORD=correct password


Checking if User Exists

Log into MySQL with an administrative account. You can do this via the command line or a tool like MySQL Workbench. Once logged in, run.

SELECT User, Host FROM mysql.user;

This will list all users and their associated hosts. Look for admin_user in the list.


Granting Permissions

If admin_user exists but cannot access from 'localhost', you might need to grant permissions. As a MySQL admin, execute.

GRANT ALL PRIVILEGES ON *.* TO 'admin_user'@'localhost' IDENTIFIED BY 'password_here';
FLUSH PRIVILEGES;

Replace password_here with the actual password. This command grants all privileges to admin_user when connecting from 'localhost'.

 

Checking MySQL Server Status

Make sure the MySQL server is running. On a Linux system, you might use.

sudo systemctl status mysql