【问题标题】:How to fix "Illuminate\Database\QueryException: SQLSTATE[HY000] [1044] Access denied for user"如何修复“Illuminate\Database\QueryException: SQLSTATE[HY000] [1044] Access denied for user”
【发布时间】:2019-06-05 01:29:04
【问题描述】:

我尝试运行: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=

【问题讨论】:

  • .env 文件中设置您的数据库用户名。并从项目根目录中的控制台运行php artisan cache:clear
  • 您的config/database.php.env 文件中有什么内容? (不要忘记删除密码!)
  • @Jerodev 我删除了密码,并使用 .env 的其他详细信息进行了编辑,但仍然无法解决我的问题。
  • @HilmiErdemKEREN 我已经添加了 .env 连接详细信息,仍然无法解决问题,请查看问题所在。
  • 你是否真的创建了一个名为homestead的数据库和用户?

标签: php mysql laravel laravel-5


【解决方案1】:

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

DB_USERNAME 如果你在安装 MySQL 时没有默认的用户名,应该设置为 root强>xampp。

如果数据库没有设置密码,请清除它DB_PASSWORD空白也必须删除(过去我也遇到过这个问题,窗口将空格视为密码)

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

php artisan config:cache

【讨论】:

  • 嘿,这有帮助,但现在它显示“Illuminate\Database\QueryException:语法错误或访问冲突:1071 指定的密钥太长;最大密钥长度为 767 字节(SQL:alter table @987654328 @ add unique users_email_unique(email))" 请同时告诉解决方法。
  • @indiadevelops 编辑您的 app/Providers/AppServiceProvider.php 文件并在 boot() 方法中设置默认字符串长度:use Illuminate\Support\Facades\Schema; public function boot() { Schema::defaultStringLength(191); }
  • 现在它显示“Illuminate\Database\QueryException : SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists (SQL: create table users (id int unsigned not null auto_increment 主键,name varchar(191) not null,email varchar(191) not null,email_verified_at timestamp null,passwordvarchar(191) not null,remember_token varchar(100) null, created_at timestamp null, updated_at timestamp null) 默认字符集 utf8mb4 collat​​e 'utf8mb4_unicode_ci')"
  • @indiadevelops 您需要从数据库中删除(如果有)users tablepassword_resets 表,还需要 delete userspassword_resets 条目来自migrations 表。
  • 为什么它需要root,描述所需的previl会更好
【解决方案2】:

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

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

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

【讨论】:

  • 感谢您的宝贵时间,问题已解决,请查看已接受的答案并对此进行评论讨论。 :)
【解决方案3】:

你应该试试这个:

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`)

然后你运行下面的命令

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

【讨论】:

  • 感谢您的宝贵时间,问题已解决,请查看已接受的答案并对此进行评论讨论。 :)
【解决方案4】:

在你的终端试试这个:启动 XMAPP 使用

sudo /opt/lampp/lampp start

控制台输出应该是

Starting XAMPP for Linux 7.4.8-0...
XAMPP: Starting Apache...fail.
XAMPP: Another web server is already running.
XAMPP: Starting MySQL...ok.
XAMPP: Starting ProFTPD...ok.

当你收到错误时

error: /opt/lampp/bin/mysql.server: 264: kill: No such process**

在你的终端再试一次:

sudo service mysql stop
sudo service apache2 stop

迁移文件后打开数据库。

【讨论】:

    【解决方案5】:

    试试这个,转到您的 .env 文件,更改数据库的名称。 转到您的 phpmyadmin,在 .env 文件中使用新名称创建一个新数据库。

    然后运行php artisan migrate

    【讨论】:

      【解决方案6】:

      在我更改 DB_PORT 并从 MAMP 设置值之前,我一直遇到同样的错误

      【讨论】:

      • 将 DB PORT 更改为什么?从 MAMP 设置什么值?以后请在您的答案中更具描述性,为读者提供价值的答案。
      【解决方案7】:
      you fix file .env Pay attention to the password section 'password'
       +need to have '
       
       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 
       
      

      【讨论】:

      • 您能否通过删除enter code here 行来编辑您的答案?
      • 请分享更多细节 - 对我来说,这看起来像是两年前已经给出的答案
      【解决方案8】:

      我正在使用 MacOS 和 MAMP

      我尝试了上述建议,但无法解决。

      为我解决的问题是在 MAMP 中从 PHP 7.x 切换到 8.0.x

      【讨论】:

        【解决方案9】:

        对于使用 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

        【讨论】:

          猜你喜欢
          • 2019-12-17
          • 1970-01-01
          • 1970-01-01
          • 2021-01-05
          • 2019-05-21
          • 2019-11-11
          • 2020-09-30
          • 2017-11-28
          • 2019-03-29
          相关资源
          最近更新 更多