php 用户 'homestead'@'localhost' 访问被拒绝(使用密码:YES)

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

Access denied for user 'homestead'@'localhost' (using password: YES)

phplaravellaravel-5database-migrationhomestead

提问by cyber8200

I'm on a Mac OS Yosemite using Laravel 5.0.

我在使用 Laravel 5.0 的 Mac OS Yosemite 上。

While in my localenvironment, I run php artisan migrateI keep getting :

在我的本地环境中,我运行php artisan migrate我不断得到:

Access denied for user 'homestead'@'localhost' (using password: YES)

用户 'homestead'@'localhost' 访问被拒绝(使用密码:YES)

Configuration

配置

Here is my .env

这是我的.env

APP_ENV=local
APP_DEBUG=true
APP_KEY=*****

DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

app\config\database.php

应用\配置\数据库.php

   'mysql'       => [
    'driver'      => 'mysql',
    'host'        => env('DB_HOST', 'localhost'),
    'database'    => env('DB_DATABASE', 'homestead'),
    'username'    => env('DB_USERNAME', 'homestead'),
    'password'    => env('DB_PASSWORD', 'secret'),
    'unix_socket' => '/tmp/mysql.sock',
    'charset'     => 'utf8',
    'collation'   => 'utf8_unicode_ci',
    'prefix'      => '',
    'strict'      => false,
    ]

How do I avoid this kind of error ?

我如何避免这种错误?

I've tried :

我试过了 :



1

1

in app/database.php

app/database.php 中

Replace localhostwith 127.0.0.1

替换localhost127.0.0.1

'host'=> env('DB_HOST', 'localhost')-->'host' => env('DB_HOST', '127.0.0.1')

'host'=> env('DB_HOST', 'localhost')-->'host' => env('DB_HOST', '127.0.0.1')

Also, in .env

此外,在.env

DB_HOST=localhost--> DB_HOST=127.0.0.1

DB_HOST=localhost--> DB_HOST=127.0.0.1



2

2

Try specify environment

尝试指定环境

php artisan migrate --env=local

php artisan migrate --env=local



3

3

Check to see if the MySQL is running by run

通过 run 检查 MySQL 是否正在运行

mysqladmin -u homestead -p status Enter password: secret

mysqladmin -u homestead -p status Enter password: secret

I got

我有

Uptime: 21281 Threads: 3 Questions: 274 Slow queries: 0 Opens: 327 Flush tables: 1 Open tables: 80 Queries per second avg: 0.012

Uptime: 21281 Threads: 3 Questions: 274 Slow queries: 0 Opens: 327 Flush tables: 1 Open tables: 80 Queries per second avg: 0.012

Which mean it's running.

这意味着它正在运行。



4

4

Check MySQL UNIX Socket (This step work for me)

检查 MySQL UNIX 套接字(这一步对我有用

采纳答案by cyber8200

Check MySQL UNIX Socket

检查 MySQL UNIX 套接字

Find unix_socketlocation using MySQL

使用 MySQL查找unix_socket位置

mysql -u homestead -p

mysql -u homestead -p

mysql> show variables like '%sock%';
+-----------------------------------------+-----------------------------+
| Variable_name                           | Value                       |
+-----------------------------------------+-----------------------------+
| performance_schema_max_socket_classes   | 10                          |
| performance_schema_max_socket_instances | 322                         |
| socket                                  | /var/run/mysqld/mysqld.sock |
+-----------------------------------------+-----------------------------+
3 rows in set (0.00 sec)

Then I go to config/database.php

然后我去config/database.php

I update this line : 'unix_socket' => '/tmp/mysql.sock',

我更新这一行: 'unix_socket' => '/tmp/mysql.sock',

to : 'unix_socket' => '/var/run/mysqld/mysqld.sock',

到 : 'unix_socket' => '/var/run/mysqld/mysqld.sock',

That's it. It works for my as my 4th try.I hope these steps help someone. :D

就是这样。它适用于我的第四次尝试。我希望这些步骤对某人有所帮助。:D

回答by Abdou Tahiri

The reason of Access denied for user ‘homestead'@'localhost' laravel 5 error is caching-issueof the .env.php file cause Laravel 5 is using environment based configuration in your .env file.

用户 'homestead'@'localhost' laravel 5 错误的访问被拒绝的原因是.env.php 文件的缓存问题,导致 Laravel 5 在您的 .env 文件中使用基于环境的配置。

1. Go to your application root directory and open .env file (In ubuntu may be it's hidden so press ctrl+hto show hidden files & if you are in terminal then type : ls -ato show hidden files) in your editor and change database configuration setting. then save your .env file

1. 转到您的应用程序根目录并在编辑器中打开 .env 文件(在 ubuntu 中它可能是隐藏的,所以按ctrl+h显示隐藏文件,如果您在终端中,则输入 :ls -a显示隐藏文件)并更改数据库配置设置。然后保存您的 .env 文件

DB_HOST=localhost
DB_DATABASE=laravelu
DB_USERNAME=root
DB_PASSWORD=''

2. then restart your apache server/web server. and refresh your page and you have done

2. 然后重新启动您的 apache 服务器/网络服务器。并刷新您的页面,您就完成了

3. If still issue try to run below command to clear the old configuration cache file.

3. 如果仍然出现问题,请尝试运行以下命令以清除旧的配置缓存文件。

php artisan config:clear

Now you are done with the error

现在你已经完成了错误

回答by Mohsin

TLDR: You need to stop the server Ctrl+ cand start again using php artisan serve

TLDR:您需要停止服务器Ctrl+c并重新开始使用php artisan serve



Details:

细节:

If you are using Laravel, and have started local dev server already by php artisan serve

如果您使用的是 Laravel,并且已经通过以下方式启动了本地开发服务器 php artisan serve

And after having above server already running, you change your database server related stuff in .env file. Like moving from MySQL to SQLite or something. You need to make sure that you stop above process i.e. Ctrcl Cor anything which stop the process. And then restart Artisan Serve again i.e. y php artisan serveand refresh your browser and your issue related to database will be fixed. This is what worked for me for Laravel 5.3

在上面的服务器已经运行之后,你可以在 .env 文件中更改与数据库服务器相关的内容。就像从 MySQL 迁移到 SQLite 之类的。您需要确保停止上述过程,即Ctrcl C停止该过程的任何内容。然后再次重新启动 Artisan Serve,即 yphp artisan serve并刷新您的浏览器,您与数据库相关的问题将得到解决。这就是 Laravel 5.3 对我有用的方法

回答by absiddiqueLive

Two way to solve it

两种方法解决

First way (Not recommended)

第一种方式(不推荐)

Open your database config file (laravel_root/config/database.php) & search for the below code block.

打开您的数据库配置文件 (laravel_root/config/database.php) 并搜索以下代码块。

        'host'      => env('DB_HOST', 'localhost'),
        'database'  => env('DB_DATABASE', 'blog'),
        'username'  => env('DB_USERNAME', 'root'),
        'password'  => env('DB_PASSWORD', ''),

Change the code block as below

更改代码块如下

        'host'      => 'yourHostName',
        'database'  => 'YourDatabastName',
        'username'  => 'YoutDatabaseUsername',
        'password'  => 'YourDatabasePassword',

Second way (Recommended by Laravel)

第二种方式(Laravel 推荐)

Check your Laravel root there have a file call .env if not exist, look for .env.example, copy/rename it as .env after that the file looks blow !

检查您的 Laravel 根目录,如果不存在文件调用 .env,请查找 .env.example,将其复制/重命名为 .env 之后该文件看起来很糟糕!

APP_ENV=local
APP_DEBUG=true
APP_KEY=someRandomNumber

DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null

Modify the below block as follow

修改下面的块如下

DB_HOST=yourHostName
DB_DATABASE=yourDatabaseName
DB_USERNAME=yourDatabaseUsername
DB_PASSWORD=youPassword

Now it will work fine.

现在它会正常工作。

回答by Vrushal Raut

If you are using the PHP's default web server (e.g. php artisan serve) you need to restart your server after changing your .env file values.

如果您使用 PHP 的默认 Web 服务器(例如php artisan serve),则需要在更改 .env 文件值后重新启动服务器。

回答by wdog

if you are using php artisan migrateNOT from vagrant box but from host machine then you must define inside the .envthe ip of the box 192.168.10.10

如果您使用的php artisan migrate不是来自 vagrant box 而是来自主机,那么您必须.env在 box 的 ip内定义192.168.10.10

and obviously set permission to homestead user from your ip something like

并且显然从您的 ip 设置对 homestead 用户的权限,例如

grant all privileges on *.* to 'homestead'@% identified by 'secret';

in .envfile

.env文件中

DB_HOST=192.168.10.10
DB_DATABASE=homestead
DB_USERNAME=homestead

回答by code511788465541441

I had the same issue and in the end It turned out that I just had to restart the server and start again

我遇到了同样的问题,最后结果证明我只需要重新启动服务器并重新启动

Ctrl+ cthen

Ctrl+c然后

php artisan serve

回答by Sojan V Jose

When you install Homestead, this creates a default "homestead" database in the VM. You should SSH into the VM homestead sshand run your migrations from there. If you are working locally with no VM, you'll need to create your database manually. By default, the database should be called homestead, the username is homestead and the password is secret.

当你安装 Homestead 时,这会在 VM 中创建一个默认的“homestead”数据库。您应该通过 SSH 连接到 VMhomestead ssh并从那里运行迁移。如果您在没有 VM 的情况下在本地工作,则需要手动创建数据库。默认情况下,数据库应该叫 homestead,用户名是 homestead,密码是 secret。

check this thread on laracastsor this blog postfor more details

查看laracasts 上的线程或此博客文章以了解更多详细信息



if you are getting

如果你得到

[PDOException] SQLSTATE[HY000] [2002] No such file or directory

try changing "host" in the /app/config/database.php file from "localhost" to "127.0.0.1" . you can find more details and other fixes here on this thread.

尝试将 /app/config/database.php 文件中的 "host" 从 "localhost" 更改为 "127.0.0.1" 。您可以在此线程上找到更多详细信息和其他修复程序。

Also check whether you have specified the correct unix_socket. check this thread.

还要检查您是否指定了正确的unix_socket. 检查这个线程

回答by Fred Ondieki

In Windows PC Follow below.

在 Windows PC 中,请按照以下步骤操作。



  1. Access the Root folder of your application.

  2. Edit the .env file

  1. 访问应用程序的根文件夹。

  2. 编辑 .env 文件

Access Root folder of your application

访问应用程序的根文件夹

Edit the .env File

编辑 .env 文件

Edit the Highlighted and change the UserName and the password adn The database Name accordingly.

编辑突出显示并相应地更改用户名和密码以及数据库名称。

回答by sh6210

in my case, after restartingmy server the problem was gone.

就我而言,重新启动我的服务器后,问题就消失了。

exit/close the "php artisan serve" commandand

退出/关闭“php artisan serve”命令

re-run the command "php artisan serve" command.

重新运行命令“php artisan serve”命令