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

rossvh's avatar

Unable to connect to server's database

I've been scratching my head all day at this and feel like I've tried every conceivable Google search possible. Apologies if I'm being a total idiot!

Error message reads:

Illuminate\Database\QueryException  : SQLSTATE[HY000] [1045] Access denied for user 'username'@'localhost' (using password: NO) (SQL: select * from information_schema.tables where table_schema = database_name and table_name = migrations)

I've created a user (I'm using 'username' in this example). This user has been given all the available privileges for the database.

Within Envoyer I've set up the Server Environment to somewhat resemble the .env.example file while changing all the necessary fields.

This is what I've ended up with:

APP_NAME="Test Project"
APP_ENV=production
APP_KEY=[key does show correctly]
APP_DEBUG=true

LOG_CHANNEL=stack

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=database_name
DB_USERNAME=username
DB_PASSWORD=...

BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
SESSION_LIFETIME=120
QUEUE_DRIVER=sync

This successfully creates a .env file which is a sibling to the current & releases folders. I've then ssh'd into the server and can see a link to this .env file while in the current folder.

I've also set up a hook to run after 'Purge Old Releases' which looks like:

cd public_html/current
php artisan key:generate
php artisan config:cache
/scripts/restartsrv apache_php_fpm (Restarts the server. The default Envoyer way doesn't work either..)
php artisan migrate --force

This will return the error message above. I don't know where to go next from here.

I've signed into the server through ssh and I am able to login to MySQL through the command line. I've opened up MySQL Workbench and set up a test connection to the database which works just fine. I've then run the command 'php artisan tinker' and DB::connection()->getPdo(); will return a similar Access denied message. However, DB::connection()->getDatabaseName(); DOES return the Database's Name that is in the .env file. So I concluded that the .env file is being linked up correctly.

Again, sorry if this is a simple fix but I've tried everything I can find and I'm completely stuck right now.

Thanks for any help in advance! Would be much appreciated :)

0 likes
9 replies
lostdreamer_nl's avatar

Just to make sure:

DB_PASSWORD=...

There is a password in your env file right? And it matches the password for 'username'?

Because the error says:

(using password: NO)

Could you (for completeness) also show config/database.php? It should still look like this:

        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],
rossvh's avatar

Hey! Yeah the password is in the env file. I've just put in the three dots instead of the actual password. I've checked several times to make sure that the password is correct as well :P

Here is what you're after:

'mysql' => [
   'driver' => 'mysql',
   'host' => env('DB_HOST', '127.0.0.1'),
   'port' => env('DB_PORT', '3306'),
   'database' => env('DB_DATABASE', 'forge'),
   'username' => env('DB_USERNAME', 'forge'),
   'password' => env('DB_PASSWORD', ''),
   'unix_socket' => env('DB_SOCKET', ''),
   'charset' => 'utf8mb4',
   'collation' => 'utf8mb4_unicode_ci',
   'prefix' => '',
   'strict' => true,
   'engine' => null,
]

It's identical to what you've got in your post. Am I right in thinking that the fields I enter into the env file will take precedence over what's inputted in these settings?

36864's avatar

Is your config cached? Try clearing it with php artisan config:clear.

jlrdw's avatar

Also you don't have two occurrences of MySQL do you I've seen where that's been a problem before.

And try connecting from the command line to ensure everything is correct.

lostdreamer_nl's avatar

"Am I right in thinking that the fields I enter into the env file will take precedence over what's inputted in these settings?"

Yes,

   'database' => env('DB_DATABASE', 'forge'),

Will use the setting DB_DATABASE= from your .env file, or if there isn't one, it will take 'forge'

36864 & jlrdw also have great points: use php artisan config:clear to clear any config that might be cached.

You could also try without the php artisan config:cache in the purge old releases.

Also: I'm not using envoyer myself, but isn't purging done more then once (every time after a deploy when you have more then X old deployments?)

If so: You might not want the php artisan key:generate in there as it could override an existing app key.

Snapey's avatar
Snapey
Best Answer
Level 122

Any doubts about env or cached config, just open tinker and type config('database')

There, you get the definitive list of current settings.

rossvh's avatar

Thanks for the responses! Using the config('database') command I was able to narrow it down to the password not being picked up. Thanks @Snapey for that tip! The password was in the env file but there was a # symbol in it. This was the source of the issue and after I had wrapped it in double quotes the database linked up.

So, thanks for the help guys! Much appreciated!

Snapey's avatar

ouch! a hash in a password causes the rest of the line to be regarded as comment

Thats a good one. I wonder how many have had that problem in the past and not realised it.

Cronix's avatar

Hah, yeah. # denotes a comment so it would ignore anything after that on the line

Personally I think the .env package should only assume # is a comment if it's on it's own line and starts with #.

Please or to participate in this conversation.