Laravel: SQLSTATE[28000] [1045] 用户 'homestead'@'localhost' 访问被拒绝
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29345841/
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
Laravel: SQLSTATE[28000] [1045] Access denied for user 'homestead'@'localhost'
提问by Kevin Steen Hansen
I just installed laravel and made a migration. But when i try to run it i get this error:
我刚刚安装了 laravel 并进行了迁移。但是当我尝试运行它时,我收到此错误:
[PDOException]
SQLSTATE[28000] [1045] Access denied for user 'homestead'@'localhost' (using password: YES)
Can anyone tell me what is wrong? I think the Database.php file looks different than normal. Is this something new in the new Laravel?
谁能告诉我出了什么问题?我认为 Database.php 文件看起来与正常情况不同。这是新 Laravel 中的新东西吗?
My Database config:
我的数据库配置:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'LaraBlog'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'root'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
Hope someone can help me :)
希望可以有人帮帮我 :)
采纳答案by naib khan
Try to check out the ".env" file in your root directory. These values are taken first. Yours are just the defaults if there are none given in the .env file.
尝试检查根目录中的“.env”文件。这些值首先被采用。如果 .env 文件中没有给出,则您的只是默认值。
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=Your_Database_Name
DB_USERNAME=Your_UserName
DB_PASSWORD=Your_Password
回答by Supravat Mondal
I tried to make changes to database.php file present within config folder it looks something like this
我试图对 config 文件夹中存在的 database.php 文件进行更改,它看起来像这样
'default' => 'mysql',
....
...
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'sample'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
I am not using any VM, I am using my local machine, with the database user as root and password a null.
我没有使用任何虚拟机,我使用的是本地机器,数据库用户为 root,密码为空。
I have also changed my .env file and it looks something like this:
我还更改了我的 .env 文件,它看起来像这样:
APP_ENV=local
APP_DEBUG=true
APP_KEY=zLzPMzs5W4FNNuguTmbG8M0iFqhIVnsP
DB_HOST=localhost
DB_DATABASE=sample
DB_USERNAME=root
DB_PASSWORD=null
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
Even after doing all the changes when I try to register using the registration form that is shipped with laravel, I try to add a user to my database I get the following error
即使在我尝试使用 Laravel 附带的注册表单进行注册时进行了所有更改后,我仍尝试将用户添加到我的数据库中,但出现以下错误
After doing all the changes I cleared the cache and loaded it again and it seems to work for me now! if any one is also facing the same issue just run the following commands
完成所有更改后,我清除了缓存并再次加载它,现在它似乎对我有用!如果有人也面临同样的问题,只需运行以下命令
php artisan cache:clear
php artisan config:cache
回答by Kevin Steen Hansen
I figured it out :) Had to chance the .env file :)
我想通了:) 不得不碰碰 .env 文件 :)
Is there any changes in the schemaes?
模式是否有任何变化?
Shouldn't this work:
这不应该工作:
public function up()
{
// Create table with columns
Schema::create('users', function($table) {
$table->increments('id');
$table->string('username');
$table->string(Hash::make('password'));
$table->string('firstname');
$table->string('lastname');
$table->string('email')
$table->string('role');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
// Insert table to database
Schema::drop('users');
}
回答by Ali
i got the same issue after creating laravel project. The authentication command run successfully.
创建 Laravel 项目后,我遇到了同样的问题。认证命令运行成功。
php artisan make:auth
But when i try to register or login i got the same error. I run the following command
但是当我尝试注册或登录时,我遇到了同样的错误。我运行以下命令
php artisan config:clear
then stopped laravel application and also my server. after restarting my laravel app and server everything was ok.
然后停止了laravel应用程序和我的服务器。重新启动我的 Laravel 应用程序和服务器后一切正常。
回答by Udhav Sarvaiya
Tried all the solutions here but no work for me
在这里尝试了所有解决方案,但对我不起作用
This Worked on the first try:
这在第一次尝试时有效:
If you are using the PHP's default web server (eg.
php artisan serve
) you need to restart your server after changing your.env
file values.
如果您使用 PHP 的默认 Web 服务器(例如
php artisan serve
),则需要在更改.env
文件值后重新启动服务器。
I found this solution from here laravel.io(Read the solution of Byjml)
我从这里laravel.io找到了这个解决方案(阅读Byjml的解决方案)
回答by Mayur Vadher
Make changes in the database.php file in **config > local >database.php rather than change config > database.php file.
在 **config > local >database.php 中的 database.php 文件中进行更改,而不是更改 config > database.php 文件。
hope it will work ;)
希望它会起作用;)
回答by Delino
practically when you are using localhost server like xampp, you create the database manually "homestead", such errors are due to missing database arguments. 100% percent working test it and see
实际上,当您使用像 xampp 这样的 localhost 服务器时,您会手动“homestead”创建数据库,此类错误是由于缺少数据库参数造成的。100% 工作测试并查看
hit your cmd and type in:
点击你的cmd并输入:
create database homestead;