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

新手上路,请多包涵

我试图运行: php artisan migrate

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

我收到了这个错误:

 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.

.env 文件:

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

原文由 harshit 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.8k
2 个回答

打开 .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

如果在 xampp 中安装 MySQL 时没有默认 用户名,则 DB_USERNAME 应设置为 root

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

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

 php artisan config:cache

原文由 Udhav Sarvaiya 发布,翻译遵循 CC BY-SA 4.0 许可协议

对于使用 MAMP 的 Mac 用户,如果接受的答案不足以解决这个问题,比如我,请将 socket 添加到 .env 文件中。

 DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
//or
UNIX_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock

该变量应与 config > database.php 上的设置匹配。寻找 connections > mysql > unix_socket

原文由 BibekStha 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题