简体   繁体   中英

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:

$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:

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

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. But laravels env() function returns an empty string in that case.

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.

This can be archived by writing DB_PASSWORD=null or removing that line. 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

service mysql restart

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM