SQLSTATE[HY000] [1045] Access denied for user 'username'@'localhost' (using password: YES)

拥有回忆 提交于 2020-04-06 21:49:09

问题


In my Ubuntu VM, I kept getting

SQLSTATE[HY000] [1045] Access denied for user 'username'@'localhost' (using password: YES)


update

I have configured my database like this in my .env file

DB_HOST=45.55.88.57
DB_DATABASE=b-prod
DB_USERNAME=root
DB_PASSWORD=*********
UNIX_SOCKET=/var/run/mysqld/mysqld.sock

It works perfectly.


But when I use

DB_HOST=45.55.88.57
DB_DATABASE=b-prod
DB_USERNAME=b <----------------------------------------------
DB_PASSWORD=*********
UNIX_SOCKET=/var/run/mysqld/mysqld.sock

I didn't work.

What else I should look into to prevent this ?


I have a feeling that I have the wrong password.

How do I test my database password ? DB_PASSWORD=********* ?


This is all user I have

mysql> SELECT User,Host FROM mysql.user;
+------------------+-------------+
| User             | Host        |
+------------------+-------------+
| root             | %           |
| root             | 127.0.0.1   |
| root             | 45.55.88.57 |
| root             | ::1         |
| root             | b       |
| b            | localhost   |
| debian-sys-maint | localhost   |
| root             | localhost   |
+------------------+-------------+
8 rows in set (0.00 sec)

回答1:


The solution:

Sometimes when you change your .env, the configs don't refresh without clear cache or restarting the server.

The error message are:

SQLSTATE[HY000] [1045] Access denied for user 'forge'@'localhost' (using password: YES)

The database are trying to get the information from the "cached" .env configuration, you should try:

php artisan config:clear

if not solve, restart your server.

[Edit] Another NON RECOMMENDED option:

  • Go to your config\database.php
  • remove the .env configuration

And set your own credentials

'mysql' => [
            'host' => '45.55.88.77',
            'database' => 'prod',
            'username' => 'forge',
            'password' => '*********',
        ],



回答2:


This file is caching some data like database configurations \bootstrap\cache\config.php. Either delete it or update it with new data.




回答3:


I think your problem is with your database and not with laravel.

mysql> SELECT User,Host FROM mysql.user;
+------------------+-------------+
| User             | Host        |
+------------------+-------------+
| root             | %           |
| root             | 127.0.0.1   |
| root             | 45.55.88.57 |
| root             | ::1         |
| root             | b           |
| b                | localhost   | <-- must be % not localhost coz you want to access it remotely not locally
| debian-sys-maint | localhost   |
| root             | localhost   |
+------------------+-------------+
8 rows in set (0.00 sec)

Hopefully this can help.




回答4:


check database port if you use wamp, login phpadmin and see Server: MySQL on top of the page.



来源:https://stackoverflow.com/questions/42496917/sqlstatehy000-1045-access-denied-for-user-usernamelocalhost-using-pass

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!