[英]How to fix “Illuminate\Database\QueryException: SQLSTATE[HY000] [1044] Access denied for user”
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.
.env file: .env 文件:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=
Open the .env
file and edit it.打开
.env
文件并进行编辑。 Just set up correct DB credentials:只需设置正确的数据库凭据:
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_USERNAME
should be set to root
if you do not have a default username in the installation of MySQL in xampp .如果您在xampp 中安装 MySQL 时没有默认用户名,则应将
DB_USERNAME
设置为root
。
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
,空格也必须删除(过去我也遇到过这个问题,窗口将空格视为密码)
After completion of .env
edit please enter this command in your terminal for clear cache:完成
.env
编辑后,请在终端中输入此命令以清除缓存:
php artisan config:cache
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
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
Try this, go to your .env
file, change the name of the database.试试这个,转到您的
.env
文件,更改数据库的名称。 Go to your phpmyadmin, create a new database with the new name in your .env
file.转到您的 phpmyadmin,在
.env
文件中使用新名称创建一个新数据库。
Then run php artisan migrate
然后运行
php artisan migrate
在更改DB_PORT
并从 MAMP 设置值之前,我一直DB_PORT
相同的错误
For Mac users using MAMP, if the accepted answer was not enough to solve this issue, like me, add socket
to the .env
file.对于使用 MAMP 的 Mac 用户,如果接受的答案不足以解决这个问题,就像我一样,将
socket
添加到.env
文件中。
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
//or
UNIX_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
The variable should match with the setting on config > database.php
.该变量应与
config > database.php
上的设置匹配。 Look for connections > mysql > unix_socket
查找
connections > mysql > unix_socket
Try this in your terminal: To start XMAPP use在你的终端中试试这个:启动 XMAPP 使用
sudo /opt/lampp/lampp start
Console output should be控制台输出应该是
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.
When you get the error当你得到错误
error: /opt/lampp/bin/mysql.server: 264: kill: No such process**
try this again in your terminal:在您的终端中再试一次:
sudo service mysql stop
sudo service apache2 stop
Then open datatbase after migrating the file.然后在迁移文件后打开数据库。
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
I'm using MacOS and MAMP我正在使用 MacOS 和 MAMP
I tried the above mentioned suggestions but I couldn't solve it.我尝试了上述建议,但无法解决。
What solved it for me was switching from PHP 7.x to 8.0.x in MAMP .为我解决的是在 MAMP 中从 PHP 7.x 切换到 8.0.x 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.