why .env file configuration in laravel is not working
Asked Answered
B

9

8
DB_CONNECTION=mysql      
DB_HOST=127.0.0.1     
DB_PORT=3306     
DB_DATABASE=
DB_USERNAME=root     
DB_PASSWORD=   

this is my configuration for laravel 5.4 but php artisan migrate is not working and have error and the migrate error

Users-MacBook-Pro: ATP Developers php artisan migrate

In Connection.php line 664: 
SQLSTATE[HY000] [1045] Access denied for user ''@'localhost' (using password: NO) (SQL: select * from information_schema.tables where table schema = atp_db and table_name = migrations)

In Connector.php line 87:
SQLSTATE[HY000] [1045] Access denied for user ''@'localhost' (using password: NO)
Brachiopod answered 21/1, 2018 at 11:57 Comment(5)
Please don't post the errors as image, it's unreadable.Removable
add db name DB_DATABASE=database_name and provide password for phpmyadmin DB_PASSWORD=your_passwordSkinhead
Have you checked whether there is any password set for your root db? The error is saying that your credential is not correct. That's all.Dill
thank you but, the credential is ok , it doesn't have any password and the user is root but again has this problemBrachiopod
Database connection should give and clear config using artiasnNovocaine
S
6

I faced a similar problem. So, I run the following commands as mentioned at https://laracasts.com/discuss/channels/general-discussion/env-file-and-config-keys-not-updating-after-change, php artisan cache:clear php artisan config:clear php artisan route:clear

Also, make sure to restart the server as well

php artisan serve
Sightread answered 10/5, 2020 at 21:55 Comment(0)
G
2

you should write these:

DB_DATABASE = your database name
DB_USERNAME = root     
DB_PASSWORD = your password

and again run php artisan serve to make sure about saving .env and again run php artisan migrate

Galore answered 21/1, 2018 at 12:36 Comment(0)
S
1

Indeed I had the same problem, I can not explain why Laravel indicates the old Host but the solution is to change the password.

Use below => php artisan config:cache

Soerabaja answered 5/6, 2019 at 13:9 Comment(1)
I had this issue after changing my connection password php artisan config:cache solved it for me.Williamsen
V
0

Change env host entry to DB_HOST=localhost. Or add the @'127.0.0.1' credentials to mysql.

Valerie answered 21/1, 2018 at 12:21 Comment(1)
i did (db_host=localhost) but again could not migrate and show this error on the topBrachiopod
T
0

The error indicates that you do not have the correct user and host combination in your database. The env file shows host is 127.0.0.1 but localhost is specified in the error. Add user@localhost entry to the database or user@% for wildcard.

Treasurer answered 21/1, 2018 at 12:57 Comment(0)
D
0

This error would indicate that you don't have a database user configured in your .env file: ''@'localhost' . Your .env should have all those fields populated with the database name and credentials you configured before running any function that connects to the database.

Here are the preliminary steps to setup your database and .env file prior to running a php artisan migrate:

Hope it helps.

Step 1: Login to your mysql and create the database.

mysql -u root -p
create database just_brew_it;

Step 2: Although you can use root to authenticate via laravel, I'd create a new user to mitigate risk of any security issues:

GRANT ALL PRIVILEGES ON just_brew_it.* TO 'brew_user'@'%' identified by 'pint0fStell@' WITH GRANT OPTION;
FLUSH privileges;

Step 3: Modify your .env with the appropriate database, username and password

DB_CONNECTION=mysql      
DB_HOST=127.0.0.1     
DB_PORT=3306     
DB_DATABASE= just_brew_it
DB_USERNAME= brew_user
DB_PASSWORD= pint0fStell@
Diploma answered 21/1, 2018 at 13:12 Comment(11)
thank you very much , though i changed the password to null , again did not worked , and changed db_host to localhost again did not work , i tried all the way but again have the same errorBrachiopod
@Najibjamshidi have you configured your .env file with a database name, username, and password that you have logged into the database with?Diploma
yep i did , the code you have seen is the connection file inside .env but still has problem and can't connectBrachiopod
Just to be clear, the way your .env file won’t work. I’ve highlighted the steps above to create a database, grant access to a user, then to add that database and new credentials to the database. It doesn’t sound like you have performed those 3 steps I highlighted. You don’t have a database mapped to the database name, and I assume you are leaving the password empty because you haven’t secured your root account. If you don’t have a database mapped in the field you have empty you will never be able to connect to a database.Diploma
Please send me your .env variables as they are now.Diploma
i did every thing i assigned database name, password to null, user name to root and the all correct , the problem is that this work in windows but as i migrate from win to mac os it does not work , i don't know why?Brachiopod
Let us continue this discussion in chat.Brachiopod
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=atp_db DB_USERNAME=root DB_PASSWORD=Brachiopod
Ok you missed step two in my answer. Please login to mysql and run that command changing the scheme name, username and password. Once you assign a password the update the password in your.env file.Diploma
you mean to assign a password for my database ?Brachiopod
Correct. Assign a password for the user connected to your database. Typically I run mysql_secure_installation after I install a database so I don’t have the issue you just ran into. I also create a service account for each Laravel instance to ensure a more secure and scalable environment.Diploma
H
0

This combo worked for me:

php artisan clear-compiled 
composer dump-autoload
php artisan optimize
Hollingsworth answered 9/2, 2021 at 22:31 Comment(0)
R
0

I spent almost 24 hrs tried evry solution i found of laravel artisan clear optimise bla bla etc nothing worked

i realised it later after starting the server in powershell outside vscode thats when that new instance started picking from my env

so guys try running php artisan serve in powershell not in vscode and see

Rotund answered 18/9 at 20:55 Comment(0)
B
-1

I had the same issue and i couldnt run "config:cache" artisan command even with Artisan::call('config:cache');

so i done this and solved my issue:

artisan config:cache caches all files from /config into a single array that it stores. It then reads all config variables from that array and ignores anything in .env or the /config files until you recache them. That's why it still works after deleting .env.

https://laravel.com/docs/5.6/configuration#configuration-caching

If you don't have ssh access to your live server, you'd just need to delete the bootstrap/cache/config.php file, which is the cache file config:cache generates.

Berrios answered 7/4, 2021 at 16:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.