Laravel 5 - SQLSTATE[HY000][1045] 用户 'root'@'localhost' 访问被拒绝(使用密码:NO)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44355677/
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 5 - SQLSTATE[HY000][1045] Access denied for user 'root'@'localhost' (using password: NO)
提问by
I am trying to make CRUD operations in laravel and I alway have this error message. I already configured the .env and the config/database.php files to my local setting but it doesn't work.
我正在尝试在 laravel 中进行 CRUD 操作,但我总是收到此错误消息。我已经将 .env 和 config/database.php 文件配置为我的本地设置,但它不起作用。
I tried php artisan cache:clear,php artisan config:clear too...
我也试过 php artisan cache:clear,php artisan config:clear ......
My config/database.php:
我的配置/database.php:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', ''),
'database' => env('DB_DATABASE', 'feki_test'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
My .env:
我的.env:
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=
DB_DATABASE=feki_test
DB_USERNAME=root
DB_PASSWORD=
回答by Pawan Verma
I was facing the same issue. But I found the solution by deleting the config.php file saved in the bootstrap/cache/config.php
. After that simply run two commands
我面临同样的问题。但是我通过删除保存在bootstrap/cache/config.php
. 之后只需运行两个命令
php artisan config:clear
php artisan cache:clear
php artisan config:cache
回答by Sandeesh
Based on the comments, you're trying to access the local xampp mysql server from within Homestead. This won't work as the application is in an isolated virtual machine. You will need to use Homestead's mysql server for your application. Use the default connection information for this.
根据评论,您正在尝试从 Homestead 访问本地 xampp mysql 服务器。这将不起作用,因为应用程序位于隔离的虚拟机中。您将需要为您的应用程序使用 Homestead 的 mysql 服务器。为此使用默认连接信息。
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
To access Homestead's server on your local machine change the port to 33060
. This can be helpful if you want to use a SQL tool to connect to the database.
要在本地机器上访问 Homestead 的服务器,请将端口更改为33060
. 如果您想使用 SQL 工具连接到数据库,这会很有帮助。
回答by Farhad Hossen
First of all, you need not write credentials in config/database.php. then delete you .env file and then rename your .env.example file to .env afterward run the PHP artisan key:generate command on your terminal .
首先,您不需要在 config/database.php 中写入凭据。然后删除您的 .env 文件,然后将您的 .env.example 文件重命名为 .env 之后在您的终端上运行 PHP artisan key:generate 命令。
give your database credential.
提供您的数据库凭据。
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=feki_test
DB_USERNAME=root
DB_PASSWORD=
and the last restart your development server. .
最后重启你的开发服务器。.
回答by Ali Khurram
In .env and config->database.php file
在 .env 和 config->database.php 文件中
Instead of this
而不是这个
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=feki_test
DB_USERNAME=root
DB_PASSWORD=
Use this
用这个
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=db_name
DB_USERNAME=root
DB_PASSWORD=''
and then restart your server. It will work for sure
然后重新启动您的服务器。它肯定会起作用
回答by Seyed Alireza Ghoreishi
i changed 127.0.0.1 to localhost And its worked
我将 127.0.0.1 更改为 localhost 并且它起作用了
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=efaf
DB_USERNAME=root
DB_PASSWORD=
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=efaf
DB_USERNAME=root
DB_PASSWORD=