diannone's avatar

Access denied for user 'forge'@'localhost' (using password: NO)

Despite the env file correctly set, with all the DB settings (included DB_USERNAME and DB_HOST) and the database config file left untouched, when I make an API call (through a route defined in the api route file), I get the error in the title.

However, the awkward thing is this occurs only randomly, i.e., most of the time the API call goes fine, but sometimes it returns this error.

I've already launched the next commands:

php artisan config:cache
php artisan config:clear

But it turns out it's not working.

Laravel version is 8, and I'm running the website on an apache server (version 2.4.35) running on Windows 10.

0 likes
4 replies
fylzero's avatar

@diannone It seems like your local mysql is not using forge (without a password) as the user account.

Try this in your terminal, of you get the same error, then that is not your mysql user

mysql -uforge

Try the typical default user

mysql -uroot

If that works, CTRL+C to exit MySQL prompt and change your .env file to use root as the DB_USERNAME instead of forge

Sounds like you just need to figure out what the MySQL username/password is on your server.

diannone's avatar

@fylzero First of all, thank you for your reply.

I'm not using forge. I've set the .env file to work with an instance of Amazon RDS. Most of the time it's used the right DB configuration. However, if I make multiple GET requests, sometimes I get this error. It's like for some reason Laravel can't find the correct cached settings (generated from the .env file) and uses the default value in the database.php.

This is the default mysql variable in the database.php file:

        'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            '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', ''),
	    ...
        ],

Whereas, these are my DB settings in the .env file:

DB_CONNECTION=mysql
DB_DATABASE=localguides
DB_PORT=3306
DB_HOST="xxx.xxx.eu-south-x.rds.amazonaws.com"
DB_USERNAME="vapor"
DB_PASSWORD="xxxxxxxxxx"
fylzero's avatar
fylzero
Best Answer
Level 67

@diannone Silly question, have you tried this by running php artisan config:cache WITHOUT running the clear command? So you know, config:cache will execute clear first. Maybe it is relying on the vars being cached. Just something I'd try if you haven't already.

1 like
diannone's avatar

@fylzero It seems that you're right. I was doing it mechanically and I've not thought that in this way I've could get a mess with the configuration cache.

For now, it seems to work correctly. So, thank you! :D

1 like

Please or to participate in this conversation.