简体   繁体   English

SQL: select * from users: SQLSTATE[HY000] [1045] 用户 'root'@'localhost' 访问被拒绝(使用密码:YES)

[英]SQL: select * from users: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES)

In Laravel, while using the native query, I am using the following code:在 Laravel 中,在使用本机查询时,我使用了以下代码:

$users = DB::select('select * from users');
return $users;

But, I am getting the error like the following:但是,我收到如下错误:

SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) (SQL: select * from users)

My .env file is this:我的 .env 文件是这样的:

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

Can you help me here?你能帮我吗?

As mentioned in my comment: put your password after DB_PASSWORD= or setting it to DB_PASSWORD=null正如我在评论中提到的:将您的密码放在DB_PASSWORD=或将其设置为DB_PASSWORD=null

Explanation:解释:

In your posted error the message contains "using password: YES" what means that a password was used.在您发布的错误中,消息包含“使用密码:是”,这意味着使用了密码。 According to your .env there is no password.根据您的 .env 没有密码。 But laravels env() function returns an empty string in that case.但是在这种情况下,laravel env()函数返回一个空字符串。

That empty string is used as the password what is not what you want.该空字符串用作密码,这不是您想要的。 You have to get env('DB_PASSWORD') to return null for auth without password.您必须让env('DB_PASSWORD')为没有密码的身份验证返回null

This can be archived by writing DB_PASSWORD=null or removing that line.这可以通过写入DB_PASSWORD=null或删除该行来存档。 But writing it out prevents future you and others from thinking that it was forgotten.但是把它写出来可以防止未来的你和其他人认为它被遗忘了。

How is your database server hosted?您的数据库服务器是如何托管的? Often this error is linked to a non-authorization of the remote root.通常此错误与远程根的非授权有关。

For example, this command :例如,这个命令:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password';

And after, restart your Mysql service之后,重新启动你的 Mysql 服务

service mysql restart

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQLSTATE[HY000] [1045] 用户 'root'@'localhost' 的访问被拒绝(使用密码:YES)(SQL:从 `cats` 中选择 *) - SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) (SQL: select * from `cats`) SQLSTATE [HY000] [1045] 用户'root'@'localhost'的访问被拒绝(使用密码:NO)(SQL:select *来自`permissions`) - SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: NO) (SQL: select * from `permissions`) SQLSTATE[HY000] [1045] 用户 'homestead'@'localhost' 的访问被拒绝(使用密码:YES)(SQL:从 `table` 中选择 *) - SQLSTATE[HY000] [1045] Access denied for user 'homestead'@'localhost' (using password: YES) (SQL: select * from `table`) SQLSTATE [HY000] [1045] 用户'root'@'localhost'的访问被拒绝(使用密码:YES)symfony2 - SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) symfony2 无法迁移 SQLSTATE[HY000] [1045] 用户 'root'@'localhost' 的访问被拒绝(使用密码:YES) - Cannot migrate SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) SQLSTATE [HY000] [1045]拒绝用户'root'@'localhost'的访问(使用密码:是)错误 - SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) error occurs SQLSTATE[HY000] [1045] 用户'root'@'localhost'的访问被拒绝(使用密码:YES)? - SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES)? Symfony-理论:用户'root'@'localhost'的SQLSTATE [HY000] [1045]访问被拒绝(使用密码:是) - Symfony - doctrine: SQLSTATE[HY000] [1045] access denied for user 'root'@'localhost' (using password: YES) Illuminate\Database\QueryException: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (使用密码: YES) - Illuminate\Database\QueryException : SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) SQLSTATE[HY000] [1045] 用户 'root'@'localhost' 的访问被拒绝(使用密码:NO)(SQL:select * from `rooms` order by `order_by` asc) - SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: NO) (SQL: select * from `rooms` order by `order_by` asc)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM