SQLSTATE[HY000] [1045] Error in Laravel

Database connection failing? Check your .env credentials, MySQL host permissions, and how to clear the Laravel config cache to apply fixes.

SQLSTATE[HY000] [1045] Access Denied in Laravel

If you are working with a Laravel project and suddenly see an error like this:


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

then it means Laravel is not able to connect to your MySQL database.

This is one of the most common problems developers face while installing a Laravel project, migrating a database, or moving a project to another server.

The good news is — this problem is usually very easy to fix.

In this guide, I will show you the exact reasons why this error happens and the simple steps to fix it. These solutions are based on real project experience while setting up Laravel apps on local machines, staging servers, and production servers.

What Does SQLSTATE[HY000] [1045] Access Denied Mean?

This error simply means that MySQL rejected the database login request.

Laravel tried to connect to the database using the credentials provided in the .env file, but MySQL refused the connection.

This usually happens because:

  • Wrong database username
  • Wrong database password
  • Database user does not have permission
  • Database host is incorrect
  • .env configuration is not loaded properly

Let’s go step-by-step and fix each possible reason.

1. Check Your Laravel .env Database Credentials

The first thing you should check is the database configuration inside the .env file.

Open your Laravel project root folder and locate the file:


.env

Then find the database configuration section:


DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_database_user
DB_PASSWORD=your_database_password

Now carefully verify:

  • Database name is correct
  • Database username is correct
  • Password is correct
  • Host is correct (localhost or 127.0.0.1)

Even a small typo can cause the 1045 access denied error.

2. Verify Database Login Using MySQL Command Line

Before blaming Laravel, it is always good to test whether the database credentials actually work.

Try logging in manually using MySQL command line:


mysql -u your_database_user -p

Then enter the password.

If MySQL login fails here also, then the problem is clearly with the database user or password.

In that case, you need to reset the password or create a new database user.

3. Check If Database User Has Permission

Sometimes the database user exists, but it does not have permission to access the database.

Login to MySQL as root and run:


SHOW GRANTS FOR 'your_database_user'@'localhost';

If you don't see permissions for your database, then grant them:


GRANT ALL PRIVILEGES ON your_database_name.* 
TO 'your_database_user'@'localhost';

Then reload privileges:


FLUSH PRIVILEGES;

Now try running your Laravel project again.

4. Clear Laravel Config Cache

This is a very common issue many developers miss.

Laravel caches configuration values. If you changed the .env file recently, Laravel may still be using old values.

Run the following command inside your Laravel project:


php artisan config:clear

You can also run:


php artisan cache:clear

Then restart your Laravel server.

5. Check Database Host Configuration

Sometimes the database host configuration causes the problem.

Try changing the host value in the .env file.

Example:


DB_HOST=127.0.0.1

or


DB_HOST=localhost

Some hosting environments work better with 127.0.0.1 instead of localhost.

6. Restart Your Local Server

If you are using local development environments like:

  • XAMPP
  • Laragon
  • MAMP
  • Docker

Try restarting both:

  • Apache / Nginx
  • MySQL Server

Sometimes MySQL service crashes or loses connection, which results in the access denied error.

7. Check Database User Host Permission

MySQL users are created with host restrictions.

Example:


'user'@'localhost'

If your user is allowed only from localhost but the application is trying from another host, MySQL will reject it.

You can allow access from any host:


CREATE USER 'your_database_user'@'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON your_database_name.* TO 'your_database_user'@'%';
FLUSH PRIVILEGES;

Use this carefully on production servers.

8. Check If Database Exists

Another simple but common mistake is that the database itself does not exist.

Run this command:


SHOW DATABASES;

If your database name is not listed, create it:


CREATE DATABASE your_database_name;

Then update the .env file accordingly.

Real Example From a Laravel Project

Recently while deploying a Laravel application on a staging server, we faced the same error:


SQLSTATE[HY000] [1045] Access denied for user 'laravel_user'@'localhost'

After checking everything, the issue was very simple.

The database password was updated on the server, but the .env file still had the old password.

After updating the password and running:


php artisan config:clear

the problem was fixed instantly.

This is why checking the simple things first can save a lot of debugging time.

Quick Checklist to Fix the Error Fast

Whenever you see the Laravel database access denied error, quickly check these things:

  • Verify database username
  • Verify database password
  • Verify database name
  • Check database user permissions
  • Clear Laravel config cache
  • Restart MySQL server
  • Confirm database exists

In most cases, one of these steps will solve the problem.

Related Laravel Database Issues

If you are working with Laravel and MySQL, you may also face other database issues like:

Understanding these common errors will help you debug PHP and Laravel applications much faster.

Conclusion

The SQLSTATE[HY000] [1045] Access denied error in Laravel simply means MySQL rejected the login request.

Most of the time, the problem is caused by:

  • Incorrect database credentials
  • Missing database permissions
  • Cached configuration in Laravel

By following the steps in this guide, you can quickly identify the root cause and fix the problem.

Whenever you face database connection issues in Laravel, always start with the .env configuration and verify the credentials manually.

It will save you hours of unnecessary debugging.

Ketan Patel - PHP & MySQL Performance Optimization Specialist
Ketan Patel

PHP & MySQL Performance Optimization Specialist

I specialize in diagnosing and fixing slow PHP applications, optimizing MySQL queries, and resolving backend bottlenecks in live production systems. My approach is metric-driven — identifying root causes through profiling, execution analysis, and structured optimization instead of guesswork.