Search code examples
laravelxampp

MySQL password in .env file not working, but works fine in database.php (env() caching an old password, config:clear does not remove it)


I am getting an SQLSTATE[HY000] [1045] Access denied for user error when using the .env file.

I have confirmed the credentials are working -- I can connect using MySQL Workbench.

I'm using XAMPP on Windows and Laravel 6.2. I have the same issue when running the site using php artisan serve.

I've tested the credentials in MySQL Workbench. I've even tested the credentials by commenting out the password line in config/database.php and hard coding the password.

The problem seems to be how I declare DB_PASSWORD in my .env file (DB_DATABASE and DB_USERNAME work fine.) I've tried single and double quotes (like the other examples in the .env file) Neither option works. I tried using the root user with no password, but that doesn't work either.

This is the error that I get when I try to use the .env file for credentials:

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

Or (for others searching via a common tutorial code example):

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

If I type the password into the config/database.php -- it works great. What am I doing wrong? I have tried stopping the Apache service in XAMPP, then running these commands for Laravel:

php artisan cache:clear
php artisan config:clear
php artisan config:cache

But no dice.

Here is my .env:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_test
DB_USERNAME=dba
DB_PASSWORD="simplepass1000"

And here is config/database.php (works with the password hardcoded, does not work with the password loaded from env):

        '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', ''),
            'password' => 'simplepass1000',
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],

UPDATE: To debug this issue better I printed out the env('DB_PASSWORD', '') variable into a view (it's also placed in the log file, so I should have checked there first.) It was an old password, so not the password I was trying to use. I ran these commands to get the env to reset:

php artisan cache:clear
php artisan config:clear
php artisan config:cache

However, this does not clear the env('DB_PASSWORD') value. I changed DB_PASSWORD to DB_PASSWORD2 in the .env file, and this solved my problem:

DB_PASSWORD2=simplepass1000

config/database.php:

'password' => env('DB_PASSWORD2', ''),

I'm still unsure of where this old env() value is coming from. I have grep'ed my site and do not see it in any code files, so this must be pulling from somewhere on my machine.

The env value seems to be loaded from my environment, because I get the same value when outside of the Laravel website:

$ php -r "echo getenv('DB_PASSWORD');"

I have tried putenv() to reset this variable, but this is not working:

$ php -r "echo putenv('DB_PASSWORD=test');"

Solution

  • I found solution for this!!!!

    The error is the environment of terminal, do you run echo $DB_PASSWORD and this echo the password in the environment.

    To solve delete this variable from .zprofile or .bash_profile or set the password with export DB_PASSWORD=PASS