laravel SQLSTATE[HY000] [1045] 用户“root”@“localhost”的访问被拒绝(使用密码:NO)。DB_HOST 设置为本地主机

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/58233866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:22:42  来源:igfitidea点击:

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

phpmysqllaravellaravel-6

提问by farooq

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 .envfile .

我知道这个问题可能已经在 stackOverflow 中找到了答案。但我在这里有不同的问题。我将 laravel 项目从 localhost 移到了 server 。我在服务器中完成了每一步。我可以在我的服务器中查看登录页面。问题是,我无法连接到我的 mysql 服务器。我的.env文件。

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.1and also tried to put my server's ip . It didn't helped me. Am I missing something ?

我尝试将主机更改为127.0.0.1并尝试放置我的服务器的 ip 。它没有帮助我。我错过了什么吗?

My error :

我的错误:

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

SQLSTATE[HY000] [1045] 用户 'root'@'localhost' 的访问被拒绝(使用密码:NO)(SQL:从userswhere email= [email protected]选择计数(*)作为聚合)

采纳答案by Rahul Gupta

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=""

回答by Salman Zafar

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:cacheand php artisan config:clearbut these two commands didn't help.

老实说,在使用 laravel 6 时,我也多次遇到过这个问题,可能无法找出我尝试过类似命令的解决方案 php artisan config:cachephp artisan config:clear但这两个命令没有帮助。

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

要解决此问题,您必须执行以下所有命令:

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

Note: Make sure in .envyou have put db_passwordand it is not null and also check if your db_passworduses any special character always enclose them in ""

注意:确保.env您已放入db_password并且它不为空,并检查您是否db_password使用任何特殊字符始终将它们括起来""

Example:

例子:

DB_PASSWORD="%123456%"

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

要调试此问题,您还可以创建一个测试路由,然后.env在那里转储变量以检查它们是否具有正确的值?

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

Hope it helps.

希望能帮助到你。

Thanks

谢谢

Note: Please Make sure to restart your server.

注意:请确保重新启动您的服务器。

回答by farooq

So , The problem is mine . I apologize for that. I missed to put my DB_PASSWRODinside "". 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 .envfile , it should be ,

所以,问题是我的。我为此道歉。我错过了把我的DB_PASSWROD里面""。所以,这造成了我的两个小时。我的密码开头包含一些特殊字符。所以应该在里面""避免这个问题。例如,如果我的密码是"%helloworld@",那么在.env文件中,它应该是,

`DB_PASSWORD="%helloworld@" `

Previously , I had

以前,我有

`DB_PASSWORD=%helloworld@

回答by M Shafaei N

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:

我正在使用 homestead 在 Laravel 框架中工作并且遇到了这样的问题。实际上这个错误是因为我没有在 vagrant 中创建数据库。所以,我创建了数据库。更多详情:

1- run vagrant sshto get homestead command line.

1- 运行vagrant ssh以获取 homestead 命令行。

2- run mysqlto get mysql command line.

2-运行mysql以获取mysql命令行。

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

3-运行create database database_name;以创建您的数据库。请注意,数据库应该在两个`(Not ')标记内。

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

4-修改laravel项目的.env文件如下:

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 migratein the command line of the projects folder. This cause to create tables in the askidb2 database.

5-(如果您之前在 laravel 项目中制作了模型和配置文件以用于创建表)php artisan migrate在项目文件夹的命令行中运行。这导致在 askidb2 数据库中创建表。

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

6-(可能对你来说是可选的)因为在我设置需要的文件来播种问题表之前,我只是运行php artisan db:seed来播种我想要的表。

回答by omarhabeh

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

我在建立与数据库的连接时遇到了这个错误,问题是我在 localhost 而不是 127.0.0.1