# SQLSTATE[HY000] [1045] Access denied for user

MySQL refused the user, password or host Laravel sent. Test the login with the mysql client, quote the password in .env and rebuild the config cache.

Message:

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

Exception:

```text
Illuminate\Database\QueryException
```

## What it means

Laravel reached the MySQL server, but the server refused to log in the account it sent. The message names the user, the host MySQL saw the connection come from, and whether a password was sent: `using password: NO` means it was empty. A user that does not exist gives the same message as a wrong password.

## Why it happens

- The user name or password in `.env` is wrong.
- The configuration is cached. After `php artisan config:cache`, Laravel no longer reads `.env`, so a corrected password changes nothing until you cache again.
- The password holds a `#` and is not quoted. The `.env` reader takes `#` as the start of a comment: `DB_PASSWORD=ab#cd` is read as `ab`, and `DB_PASSWORD=#abcd` as empty, which gives `using password: NO`. An unquoted space fails earlier, with "The environment file is invalid!".
- The account exists for another host. A MySQL account is a user plus a host, and the server uses the most specific host that matches. With both `'app'@'%'` and `'app'@'localhost'`, a Unix socket connection is checked against the `localhost` password, and so was a TCP connection to `127.0.0.1` in a test where the server resolves that address to `localhost`. With `skip_name_resolve` on, the server matches by IP address only. `'app'@'127.0.0.1'` accepted TCP but refused the socket. An app on another server shows up with that server's name or address, and a `localhost`-only account refuses it.

## How to fix it

Test the values from `.env` outside Laravel. If the mysql client is refused too, the account is the problem:

```bash
mysql -h 127.0.0.1 -P 3306 -u forge -p laravel
```

Quote the password. Single quotes keep every character as written but cannot hold a `'`. Put such a password in double quotes, writing a backslash as `\\`, a double quote as `\"` and `${` as `\${`:

```ini
DB_PASSWORD='s3cret#with space'
```

`php artisan config:show database.connections.mysql.username` shows the user Laravel uses. If you cache configuration, cache again after every `.env` change:

```bash
php artisan config:cache
```

If the account is missing or exists for another host, create it as an administrator for the host the message names, with rights on the app's database only. Do not use `root` for the application:

```sql
CREATE USER 'forge'@'localhost' IDENTIFIED BY 'a-long-random-password';
GRANT ALL ON laravel.* TO 'forge'@'localhost';
```

To change an existing account's password, use `ALTER USER 'forge'@'localhost' IDENTIFIED BY '...'`. To see which accounts exist for a user:

```sql
SELECT user, host FROM mysql.user WHERE user = 'forge';
```

## Find it in production

With `APP_DEBUG=false` a page that touches the database answers 500 with the "Server Error" page. With the default `SESSION_DRIVER=database`, every page in the `web` group fails, because the session is read first; `/up` kept answering 200. Laravel reports the exception and writes it at error level to `storage/logs/laravel.log`, with the user and host but not the password. If the page Ranetrace's [uptime monitoring](/features/uptime-monitoring) requests touches the database (with database sessions any `web` page does, `/up` does not), it alerts you once the site has been down for two minutes. Its error monitoring is no fallback: capture runs through your queue and cache store, so with the default `QUEUE_CONNECTION=database` and `CACHE_STORE=database` this report is lost with the database.

Reproduced on Laravel 13.45, PHP 8.5, MySQL 8.0.27, 5 December 2026.

Read more: https://laravel.com/docs/configuration

## Related

### Terms

- [Configuration caching in Laravel](https://ranetrace.com/glossary/configuration-caching-in-laravel.md)

### Other errors

- [SQLSTATE\[HY000\] \[2002\] Connection refused](https://ranetrace.com/error-guides/laravel-sqlstate-2002-connection-refused.md)
- [SQLSTATE\[42S02\]: Base table or view not found: 1146 Table doesn't exist](https://ranetrace.com/error-guides/laravel-sqlstate-1146-table-not-found.md)

Read this guide in a browser: https://ranetrace.com/error-guides/laravel-sqlstate-1045-access-denied
