SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: NO) . DB_HOST set to localhost

假装没事ソ 提交于 2020-05-15 05:35:03

问题


I know this question may have answers already in stackOverflow . But I have different issue here . I moved the laravel project from localhost to server . Which I have done every steps in server. I can able to view the login page in my server. The problem is, I can't able to connect with my mysql server . my .env file .

APP_NAME=Transport
APP_ENV=local
APP_KEY=base64:mrakeyidharhaikonsdf
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=transport_db
DB_USERNAME=root
DB_PASSWORD=mypass

I tried to change the host to 127.0.0.1 and also tried to put my server's ip . It didn't helped me. Am I missing something ?

My error :

SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: NO) (SQL: select count(*) as aggregate from users where email = user.email@gmail.com)


回答1:


Make sure your DB credential and DB Host is set correctly :

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE="your_database_name"
DB_USERNAME="put_db_user_name _here"
DB_PASSWORD="put_db_password_here_if_have_set"

If you have not set any db password the put

DB_PASSWORD=""



回答2:


To be honest while working on laravel 6 i also faced this issue many times and probably was unable to figure out the solution i tried commands like php artisan config:cache and php artisan config:clear but these two commands didn't help.

To come over this issue you have to execute the all the below commands:

php artisan route:cache
php attisan route:clear
php artisan config:cache
php artisan config:clear
php artisan optimize

Note: Make sure in .env you have put db_password and it is not null and also check if your db_password uses any special character always enclose them in ""

Example:

DB_PASSWORD="%123456%"

To debug this issue you can also create a test route and then dump .env variable there to check if they have the correct values or not?

Route::get('/test/env', function () {
    dd(env('DB_DATABASE')); // dump db variable value one by one
});

Hope it helps.

Thanks

Note: Make sure to restart your server.




回答3:


So , The problem is mine . I apologize for that. I missed to put my DB_PASSWROD inside "" . So , that caused my two hours . I have the password which contains with some special characters at the beginning. So it should be inside the "" to avoid the problems . For example , If my password is "%helloworld@" , Then in .env file , it should be ,

`DB_PASSWORD="%helloworld@" `

Previously , I had

`DB_PASSWORD=%helloworld@



回答4:


I was working in Laravel framework using homestead and had such problem. In fact this error was because I did not create the database in vagrant. So, I created the database. In more details:

1- run vagrant ssh to get homestead command line.

2- run mysql to get mysql command line.

3- run create database database_name; to create your database. Note that the data base should be inside two ` (Not ') mark.

4- modify .env file of the laravel project as below:

DB_CONNECTION=mysql
DB_HOST=192.168.10.10
DB_PORT=3306
DB_DATABASE=askidb2
DB_USERNAME=homestead
DB_PASSWORD=secret

5- (if you previously made your model and configured files in laravel projects for creation tables) run php artisan migrate in the command line of the projects folder. This cause to create tables in the askidb2 database.

6- (perhaps optional for you) since before I set needed files for seeding the question table I just run php artisan db:seed to seed my desired table(s).




回答5:


i had this error while my connection with database was established, the problem was that i was on localhost not 127.0.0.1



来源:https://stackoverflow.com/questions/58233866/sqlstatehy000-1045-access-denied-for-user-rootlocalhost-using-password

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!