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

13

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

2
将您的数据库用户名设置在 .env 文件中,并在项目根目录的控制台中运行 php artisan cache:clear - Hilmi Erdem KEREN
1
你的 config/database.php.env 文件里有什么内容?(别忘了删除密码!) - Jerodev
2
你是否真的创建了一个名为 homestead 的数据库和用户? - Jerodev
你真的使用Homestead吗?如果是这样,您是否删除了密码?默认密码是secret - Hilmi Erdem KEREN
谢谢大家抽出时间,问题已经解决,请查看下面的答案。 :) - harshit
显示剩余2条评论
10个回答

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

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

如果数据库未设置密码,请清除DB_PASSWORD也必须删除空格(我过去也遇到过这个问题,Windows认为空格为密码)。

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

php artisan config:cache

1
嘿,那很有帮助,但现在它显示“Illuminate\Database\QueryException: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table users add unique users_email_unique(email))”。请告诉我如何修复这个问题。 - harshit
@indiadevelops 编辑您的 app/Providers/AppServiceProvider.php 文件,在 boot() 方法内设置默认字符串长度:`use Illuminate\Support\Facades\Schema;public function boot() { Schema::defaultStringLength(191); }` - Udhav Sarvaiya
现在它显示“Illuminate\Database\QueryException: SQLSTATE[42S01]: 基本表或视图已经存在:1050 表 'users' 已经存在 (SQL: create table users (id int unsigned not null auto_increment primary key, name varchar(191) not null, email varchar(191) not null, email_verified_at timestamp null, password varchar(191) not null, remember_token varchar(100) null, created_at timestamp null, updated_at timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')”。 - harshit
@indiadevelops 你需要从数据库中删除(如果存在的话)users表password_resets表,并且还需要从migrations表中删除userspassword_resets的条目。 - Udhav Sarvaiya
为什么需要root权限?描述所需的特权会更好。 - Adi Prasetyo

2

对于使用MAMP的Mac用户,如果接受的答案无法解决此问题(如我所遇到的),请在.env文件中添加socket

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

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


1

你好,请前往xampp phpmyadmin创建一个数据库,例如stack,并将.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

感谢您的时间,问题已解决,请查看被接受的答案和相关评论讨论。 :) - harshit

0
你应该尝试这个:
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

感谢您的时间,问题已经解决,请查看被接受的答案和相关评论讨论。 :) - harshit

0

我一直被同样的错误困扰,直到我改变了DB_PORT并将值设置为MAMP。


1
将数据库端口改成什么?从MAMP设置了什么值?请在以后的回答中更加详细地描述,提供对读者有价值的答案。 - Muhammad Hamza

0
试试这个,打开你的.env文件,修改数据库的名称。 进入phpmyadmin,在.env文件中创建一个新的数据库,并使用新的名称。
然后运行php artisan migrate

0

确保您的 .env 文件正确无误

前往 mysql/data/database_name/tablename.ibd 文件并手动删除它。之后尝试运行命令 php artisan migrate

对于 Windows 系统,例如: C:\xampp\mysql\data\vogDB\tablename.ibd <======

  1. php artisan config:cache
  2. php artisan migrate
  3. php artisan serve

他为什么需要执行这些低级步骤,而不是使用像phpMyAdmin这样的软件简单地删除数据库? - Eyad Mohammed Osama
如果由于缓存或管理问题而无法使用phpMyAdmin,则此解决方案应该是您的最后一招。 - Opemipo Alomaja

-1
在你的终端中尝试这个:要启动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

在迁移文件后打开数据库。


-1

我正在使用MacOS和MAMP。

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

对我有用的是在MAMP中从PHP 7.x切换到8.0.x


-1
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 行? - Ahmet
请分享更多细节 - 对我来说,这看起来像是两年前已经给出的答案。 - Nico Haase

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接