laravel 如何修复“Illuminate\Database\QueryException: SQLSTATE[HY000] [1044] Access denied for user”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/54124262/
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:14:45  来源:igfitidea点击:

How to fix "Illuminate\Database\QueryException: SQLSTATE[HY000] [1044] Access denied for user"

phpmysqllaravellaravel-5

提问by harshit

I tried to run: php artisan migrate

我试着跑: php artisan migrate

Also to connect to MySQL using Xampp on Windows.

还可以在 Windows 上使用 Xampp 连接到 MySQL。

I Got this error:

我收到这个错误:

Illuminate\Database\QueryException : SQLSTATE[HY000] [1044] Access denied for user ''@'localhost' to database 'homestead' (SQL: select * from information_schema.tables where table_schema = homestead and table_name = migrations)

at C:\Users\harsh\Laravel1\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664 660| // If an exception occurs when attempting to run a query, we'll format the error 661| // message to include the bindings with SQL, which will make this exception a 662| // lot more helpful to the developer instead of just the database's errors. 663| catch (Exception $e) {

664| throw new QueryException( 665| $query, $this->prepareBindings($bindings), $e 666| ); 667| } 668| Exception trace:

1 PDOException::("SQLSTATE[HY000] [1044] Access denied for user ''@'localhost' to database 'homestead'") C:\Users\harsh\Laravel1\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70

2
PDO::__construct("mysql:host=127.0.0.1;port=3306;dbname=homestead", "homestead", "", []) C:\Users\harsh\Laravel1\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70

Please use the argument -v to see more details.

Illuminate\Database\QueryException : SQLSTATE[HY000] [1044] 用户 ''@'localhost' 对数据库 'homestead' 的访问被拒绝(SQL:select * from information_schema.tables where table_schema = homestead and table_name = migrations)

在 C:\Users\harsh\Laravel1\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664 660| // 如果尝试运行查询时发生异常,我们将格式化错误 661| // 包含与 SQL 的绑定的消息,这将使此异常成为 662| // 对开发人员更有帮助,而不仅仅是数据库的错误。663| 捕获(异常 $e){

664| throw new QueryException(665| $query, $this->prepareBindings($bindings), $e 666|); 667| 第668话 异常跟踪:

1 PDOException::("SQLSTATE[HY000] [1044] 用户 ''@'localhost' 对数据库 'homestead' 的访问被拒绝") C:\Users\harsh\Laravel1\vendor\laravel\framework\src\Illuminate\Database \Connectors\Connector.php:70

2
PDO::__construct("mysql:host=127.0.0.1;port=3306;dbname=homestead", "homestead", "", []) C:\Users\harsh\Laravel1\vendor\laravel\framework\src \Illuminate\Database\Connectors\Connector.php:70

请使用参数 -v 查看更多详细信息。

.env file:

.env 文件:

DB_CONNECTION=mysql 
DB_HOST=127.0.0.1 
DB_PORT=3306 
DB_DATABASE=homestead 
DB_USERNAME=homestead 
DB_PASSWORD=

采纳答案by Udhav Sarvaiya

Open the .envfile and edit it. Just set up correct DB credentials:

打开.env文件并进行编辑。只需设置正确的数据库凭据:

DB_CONNECTION=mysql 
DB_HOST=127.0.0.1 
DB_PORT=3306 
DB_DATABASE=            // Your Database Name
DB_USERNAME=           // Yout Database Username
DB_PASSWORD=          // Your Database Password 

The DB_USERNAMEshould be set to rootif you do not have a default usernamein the installation of MySQL in xampp.

DB_USERNAME应设置为root,如果你没有一个默认的用户名在MySQL安装的XAMPP

If no password is set on the database, clear it DB_PASSWORD, empty space must also be removed(In the past I've also faceout this problem, window consider blank space as a password)

如果数据库没有设置密码,清除它DB_PASSWORD空格也必须删除(以前我也遇到过这个问题,window把空格当作密码)

After completion of .envedit please enter this command in your terminal for clear cache:

.env编辑完成后,请在终端中输入此命令以清除缓存:

php artisan config:cache

回答by haffis asma

Hi go to xampp phpmyadmin create a database for exemple stack and change your .env to this:

嗨,去 xampp phpmyadmin 为示例堆栈创建一个数据库,并将您的 .env 更改为:

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

by default phpmyadmin username is root and without password save and try php artisan migrate

默认 phpmyadmin 用户名是 root 并且没有密码保存并尝试 php artisan migrate

回答by haffis asma

You should try this:

你应该试试这个:

DB_CONNECTION=mysql 
DB_HOST=127.0.0.1 
DB_PORT=3306 
DB_DATABASE=your database name 
DB_USERNAME= your localhost-database username (i.e `root`)
DB_PASSWORD= your localhost-database password (i.e `root`)

then you run below command

然后你运行下面的命令

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