php artisan migrate - SQLSTATE[HY000] [1045] 用户“laravel”@“localhost”的访问被拒绝

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

php artisan migrate - SQLSTATE[HY000] [1045] Access denied for user 'laravel'@'localhost'

phplaravellaravel-5

提问by MattSom

I want to set up and learn Laravel following this course

我想按照本课程设置和学习 Laravel

When I try to use the command php artisan migrateI get this error:

当我尝试使用该命令时,出现php artisan migrate此错误:

[Illuminate\Database\QueryException]                                                                                                        
  SQLSTATE[HY000] [1045] Access denied for user 'laravel'@'localhost' (using password: NO) (SQL: select * from information_schema.tables whe  
  re table_schema = laravel and table_name = migrations)

[PDOException]                                                                            
  SQLSTATE[HY000] [1045] Access denied for user 'laravel'@'localhost' (using password: NO)

I have looked for answers. I figured I may have to make some changes in the .envfile, but I don't know what, and nothing I have tried so far has worked.

我一直在寻找答案。我想我可能需要对.env文件进行一些更改,但我不知道是什么,到目前为止我尝试过的任何内容都没有奏效。

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

I'm using Ubuntu 16.04, not a Mac OS X as in the video, so I wonder what should I do differently? Is there some MySQL setting I did not set correctly?

我使用的是 Ubuntu 16.04,而不是视频中的 Mac OS X,所以我想知道我应该怎么做?是否有一些我没有正确设置的 MySQL 设置?

回答by Mycodingproject

I had the same problem with my plesk ubuntu 18.04 remote server. In your .env file put double quotes around your database password and username then run php artisan config:clearand php artisan cache:clear

我的 plesk ubuntu 18.04 远程服务器也遇到了同样的问题。在您的 .env 文件中,在您的数据库密码和用户名周围加上双引号,然后运行php artisan config:clearphp artisan cache:clear

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=dbname
DB_USERNAME="xyz"    
DB_PASSWORD="123"

I had never come across this problem before.

我以前从未遇到过这个问题。

回答by Yassine Younes

You don't have a user named 'laravel'. You should change the DB_USERNAME to the one you're actually using to access your DB. Mostly it's root by default so the changes in .env should be

您没有名为“laravel”的用户。您应该将 DB_USERNAME 更改为您实际用于访问数据库的那个。大多数情况下它默认是 root,所以 .env 中的更改应该是

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

回答by yusuf

If you are trying to run this command with homestead, you need to make sure you change the values of the following environmental variables within the .envfile to:

如果您尝试使用 homestead 运行此命令,则需要确保将.env文件中的以下环境变量的值更改为:

DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

After you have changed the values, make sure to run the following two commands before you do anything else:

更改值后,请确保在执行任何其他操作之前运行以下两个命令:

php artisan config:clear
php artisan cache:clear

回答by Pooria Honarmand

In my case it solved with using localhostinstead of 127.0.0.1

在我的情况下,它解决了 usinglocalhost而不是127.0.0.1

回答by cschaefer

If you are using MAMP and the default root username/pw, your .env file database configuration should use 'localhost' instead of 127.0.0.1 and 'root' for BOTH the username and password:

如果您使用 MAMP 和默认 root 用户名/密码,您的 .env 文件数据库配置应该使用 'localhost' 而不是 127.0.0.1 和 'root' 作为用户名和密码:

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=root

回答by dos4dev

I had situation when server didn't saw update .env file and php artisan cache:clearnot help, but just was needed refresh server php artisan serve

我遇到服务器没有看到更新 .env 文件并且php artisan cache:clear没有帮助的情况,但只需要刷新服务器php artisan serve

回答by Cameron Hudson

I had the same issue. What finally worked for me was performing Method 2 on this page.

我遇到过同样的问题。最终对我有用的是在此页面上执行方法 2

Through my struggle, I learned that there are 4 different places where you can set a password for the rootof the MySQL installation that comes with XAMPP. Setting the password in only 1 or 2 of these places alone is not enough. This is what I did to be able to perform my initial migration:

通过我的努力,我了解到有 4 个不同的地方可以为rootXAMPP 附带的 MySQL 安装设置密码。仅在其中的 1 或 2 个位置设置密码是不够的。这是我为执行初始迁移所做的工作:

  1. In the Laravel .envfile, in the DB_...section.
  2. In localhost:[your Apache port]/phpmyadmin, via User accounts -> root -> Edit privileges -> Change password.
  3. In localhost:[your Apache port]/phpmyadmin, via the method described in Method 2 on this page.
  4. In your config.inc.phpfile, which for me (on MacOS) was in /Applications/XAMPP/xamppfiles/phpmyadmin/config.inc.php. This method is also described in Method 2 on this page.
  1. 在 Laravel.env文件的DB_...部分中。
  2. 在 中localhost:[your Apache port]/phpmyadmin,通过用户帐户 -> root -> 编辑权限 -> 更改密码。
  3. localhost:[your Apache port]/phpmyadmin,通过本页方法 2 中描述的方法
  4. 在您的config.inc.php文件中,对我来说(在 MacOS 上)在/Applications/XAMPP/xamppfiles/phpmyadmin/config.inc.php. 该方法也被描述在方法2此页面上

And then restart MySQL and your web (Apache) server.

然后重新启动 MySQL 和您的 Web (Apache) 服务器

Be aware that after you successfully harmonize your passwords and try once again to migrate, you may get error output that looks almost the same as the Access Denied password error. However, this is a different error related to string lengths. See here for how to fix that issue.

请注意,在您成功协调密码并再次尝试迁移后,您可能会得到与拒绝访问密码错误几乎相同的错误输出。但是,这是与字符串长度相关的不同错误。请参阅此处了解如何解决该问题。

回答by shivanikoko

If you are on MAMP, check your port number as well. Generally, it should look like this:

如果您使用 MAMP,请检查您的端口号。一般来说,它应该是这样的:

Host    localhost
Port    8889
User    root
Password    root
Socket  /Applications/MAMP/tmp/mysql/mysql.sock

回答by Ankit kumar

step 1: Add a new user in phpmyadmin. eg: username-xyz, password-123

第一步:在phpmyadmin中添加一个新用户。例如:用户名-xyz,密码-123

step 2: create a new database eg: dbname

第 2 步:创建一个新数据库,例如:dbname

step3: update your .env file like this:

第三步:像这样更新你的 .env 文件:

    DB_CONNECTION=mysql
    DB_HOST=127.0.0.1
    DB_PORT=3306
    DB_DATABASE=dbname
    DB_USERNAME=xyz    
    DB_PASSWORD=123

step 4: now clear your cache "php artisan clear:cache"

第 4 步:现在清除缓存“php artisan clear:cache”

step 5: finally try migrating your tables with "php artisan migrate"

第 5 步:最后尝试使用“php artisan migrate”迁移您的表

回答by Daniel Foust

When I had this issue, it was because I forgot my local environment was running mysql on a port other than 3306. For me, it was 3307. If you are using XAMPP, I would check what port you are using and make sure it matches your .env file.

当我遇到这个问题时,是因为我忘记了我的本地环境在 3306 以外的端口上运行 mysql。对我来说,它是 3307。如果您使用的是 XAMPP,我会检查您使用的端口并确保它匹配你的 .env 文件。