Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

LYiub's avatar
Level 1

SQLSTATE[HY000] [1045] Access denied for user 'DBname'@'localhost' (using password: YES) (SQL: select * from `users` where `id` = 9 limit 1)

I used laravel 5.4 with MAMP. My laravel project 5.4 working well on localhost and production. since 1 week I face this issue, I change some of the CSS and JS files in public, and I change the DB connection (in '.env' file) on my localhost to remote DB on live server, as following:

(.env)

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:D5EnjUeYst35NRdiDf3vfvSR4LQdf3ikfO2pKpp9tWHs=
APP_DEBUG=false
APP_URL=http://localhost

LOG_CHANNEL=stack

DB_CONNECTION=mysql
DB_HOST=....................................rds.amazonaws.com
DB_PORT=.......
DB_DATABASE=DBname
DB_USERNAME=DBusername
DB_PASSWORD=DBpassword


It is stopped logged in and keep showing me this error:

(log file)

Next Illuminate\Database\QueryException: SQLSTATE[HY000] [1045] Access denied for user 'DBusername'@'localhost' (using password: YES) (SQL: select * from `users` where `id` = 9 limit 1) in /Applications/MAMP/htdocs/vendor/laravel/framework/src/Illuminate/Database/Connection.php:647
Stack trace:
#0 /Applications/MAMP/htdocs/vendor/laravel/framework/src/Illuminate/Database/Connection.php(607): Illuminate\Database\Connection->runQueryCallback('select * from `...', Array, Object(Closure))
#1 /Applications/MAMP/htdocs/vendor/laravel/framework/src/Illuminate/Database/Connection.php(326): Illuminate\Database\Connection->run('select * from `...', Array, Object(Closure))
#2 /Applications/MAMP/htdocs/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(1705): Illuminate\Database\Connection->select('select * from `...', Array, true)
#3 /Applications/MAMP/htdocs/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(1690): Illuminate\Database\Query\Builder->runSelect()
#4 /Applications/MAMP/htdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(462): Illuminate\Database\Query\Builder->get(Array)
#5 /Applications/MAMP/htdocs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(446): Illuminate\Database\Eloquent\Builder->getModels(Array)
...
...
#43 {main}  

Just wondering, why it keep showing 'localhost in the error, while I change the DB connection in (.env) file to new remote DB (....................................rds.amazonaws.com) !!! if I change the username , it shows in the error that ( Access denied for user 'NewDBname'@'localhost' (using password: YES) .... etc ), and localhost keep not reflecting to the changes on new DBHOST.!

I check the remote DB, no any errors in tables or anything.

Note: It is stopped working suddenly on localhost and live server after changing to remote DB.

To figure out the problem, I tried to create new laravel project 5.4 and connect it with remote DB ,, it is working well and I can fetch the data from the remote DB. So only the current system keep showing me this error.

Appreciated for any suggestion of find what the error cause this issue

0 likes
15 replies
Nakov's avatar

@lyiub but your .env file is not read at all as it seems. Have you done some changes directly in the config/database.php file?

Because the error says, user DBname while your user is set to DBusername.

Make sure that in the config/database.php you use env('DB_DATABASE', '') in order to read the environment file.

LYiub's avatar
Level 1

yes, I tried to hard coded it in config/database.php as following :

'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '....................................rds.amazonaws.com'),
            'port' => env('DB_PORT', '.......'),
            'database' => env('DB_DATABASE', 'DBname'),
            'username' => env('DB_USERNAME', 'DBusername'),
            'password' => env('DB_PASSWORD', 'DBpassword'),
            'unix_socket'   => '/Applications/MAMP/tmp/mysql/mysql.sock',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => false,
            'engine' => null,
        ],

but still showing me the same error ,, it can reflect to the changes on DBusername, but not reflecting to the changes on DB_HOST, as it keep shows in the error '@localhost'

LYiub's avatar
Level 1

I also tried : php artisan cache:clear && php artisan config:clear && php artisan config:cache

but not make any sense.

tobyreed's avatar

Are you able to connect to the database manually with those credentials?

Snapey's avatar

this

            'host' => env('DB_HOST', '....................................rds.amazonaws.com'),

will still use whatever is in .env as DB_HOST

Your rds parameter is only a fallback incase DB_HOST is not specified.

LYiub's avatar
Level 1

I solved the issue,

  1. comment the socket on database.php.
  2. put port in database.php if not exist.
  3. Put FORCE_HTTP = true in (.env) file

after change these things, I got it work .. Thanks

1 like
Snapey's avatar

You still seem unclear the purpose of .env and database.php

LYiub's avatar
Level 1

It is not that difficult to understand the purpose of env and database.

The thing is I change the above things ,, but not sure why it is working after changing them ,, or maybe the server deploying issue while I re-install everything again from beginning ,, and re-setup everything again .. or maybe the server corrupted or lack to deploy .. So it is not related to understanding for env. if you have any explain for the reason of this problem .. just welcome to share.

wailcoder's avatar

I had the same problem with Hostinger hosting, but I found that the problem was the password of the database. I deleted all the codes like #*+{

jaans's avatar

i have some problem on vps. I find that solution add " " in to .env where is DB_USERNAME="xyz" DB_PASSWORD="123"

1 like
sampathMP's avatar

@jaans Thanks for the tip. This worked after long hours of fighting with the issue. God Bless.

Please or to participate in this conversation.