How to Solve SQLSTATE[HY000] [1045] Access denied In Laravel

In this section, we’ll discuss how to solve the following errors while using Laravel:

  • SQLSTATE[HY000] [1045] Access denied for user ‘abua’@’localhost’ (using password: YES)
  • SQLSTATE[HY000] [1045] Access denied for user ”@’localhost’ (using password: NO)

Example 1

Let’s see why you got this error:

  • SQLSTATE[HY000] [1045] Access denied for user ”@’localhost’ (using password: NO).

It’s simple—you forgot to provide the database name, username, and password.

.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=
DB_USERNAME=
DB_PASSWORD=
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

Let’s see why you got this error:

  • SQLSTATE[HY000] [1045] Access denied for user ‘abua’@’localhost’ (using password: YES)

Sometimes, this error occurs because you provided an incorrect DB_USERNAME or DB_PASSWORD, which results in the 1045 “password yes” error.

laravel SQLSTATE[HY000] [1045]
.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 the following commands.

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 the admin_user exists but cannot access from ‘localhost’, you might need to grant permissions. As a MySQL admin, execute the following command.

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 the 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
saim ansari
saim ansari

I'm Saim Ansari, a full-stack developer with 4+ years of hands-on experience who thrives on building web applications that leave a lasting impression. When it comes to tech, I'm particularly adept at Laravel, React, Tailwind CSS, and the Tall Stack