6

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)
Community
  • 1
  • 1
code-8
  • 54,650
  • 106
  • 352
  • 604
  • 1
    Did you GRANT permissions? Once granted, did you FLUSH privileges? – Jay Blanchard Feb 27 '17 at 22:36
  • Try to login directly to say `phpmyadmin` directly using your credentials. Also, your host is not in the error. Your host is `45.55.88.57` but your error reads `@localhost`. If you just changed your config, do `php artisan config:clear` – EddyTheDove Feb 27 '17 at 22:38
  • @JayBlanchard : How can log-in to mysql with as root ? I tried `mysql -u root -p` I kept getting `ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)` – code-8 Feb 27 '17 at 22:41
  • How do I know my root mysql password guys ? – code-8 Feb 27 '17 at 22:44
  • Usually you set your root password whilst installing mysql / your server. Default could be just root as username and an empty password. How did you setup this VM? – Björn Feb 27 '17 at 23:03
  • I can't even get in as root. I've tried to re-set my root password. For some reasons new changes doesn't to take effect. I aked separate question here = http://stackoverflow.com/q/42497385/4480164 – code-8 Feb 27 '17 at 23:05
  • In my case, I had to enclose .env values in double quotes like DB_PASSWORD="#$Er34]" because it contained complex characters. – govind Jun 29 '23 at 12:52

7 Answers7

15

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' => '*********',
        ],
code-8
  • 54,650
  • 106
  • 352
  • 604
João Mantovani
  • 2,230
  • 1
  • 21
  • 33
  • I tried both `php artisan config:clear` and `service nginx restart` , refresh my site, and still the same = http://www.bunlongheng.com/ – code-8 Feb 28 '17 at 00:19
  • 1
    Try the edit section of my answer, it's not recommend, but we can test the .env with that – João Mantovani Feb 28 '17 at 00:22
  • I tried that with root user work, but with my other user `bheng` doesn't work. I deleted my forge user. – code-8 Feb 28 '17 at 00:25
  • I update my post with more info. Do I need to grant my bheng user for more permissions like my root to be able to get int ? – code-8 Feb 28 '17 at 00:28
  • You are not receiving more the `forge@localhost` error, now you are receiving the `bheng@locahost` in your error, your .env was refreshed and is getting the username but not the host, try to check your host in your .env or change it manually on the `config/database.php` – João Mantovani Feb 28 '17 at 00:28
  • Yes, you need to set the permissions to your user. – João Mantovani Feb 28 '17 at 00:28
  • Can you please share me the instruction to do that for Laravel ? – code-8 Feb 28 '17 at 00:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/136799/discussion-between-joao-mantovani-and-ihue). – João Mantovani Feb 28 '17 at 00:30
  • Thank you! It was very helpful after I removed the configuration in the .env file the migration completed without errors. I just don't understand why it is working without this configuration? What's the difference with and without? – Dimitrina Stoyanova Mar 19 '18 at 08:05
2

I'm also faced this issue. ENV Variable should be with in the double quotes When we used complex characters of string on .env file. Such as passwords, secrets, key and so on.

DB_PASSWORD="FDXSCHVBGFFUOVIDVKTFUVIF++++++****"

please first check this scenario.

Thank you..

Najathi
  • 2,529
  • 24
  • 23
1

I had the same issue, trying to install Matomo on my localhost. I thought it was my MySQL configuration but it seems it was my adblockers and adtrackers plugins in my browser (Opera). Once deactivated, it worked fine. I hope I could help... Good luck !

Kristof
  • 11
  • 1
0

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

user3161153
  • 41
  • 1
  • 6
0

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.

zx485
  • 28,498
  • 28
  • 50
  • 59
Annymosse
  • 51
  • 2
  • 4
0

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

Toeur Tenh
  • 71
  • 4
  • 14
0
  1. Make sure the "database user" has all the privileges of the database (and don't forget to import the SQL file).

  2. Edit the Database credentials in .env file and make sure to add the quotes

    DB_DATABASE="dbname"

    DB_USERNAME="dbusername"

    DB_PASSWORD="dbpassword"

  3. Run the following commands

    1. composer install

    2. php artisan config:clear

    3. php artisan cache:clear

  4. Browse the URL : "http://localhost/Foldername/" or "http://yourdomain.com"

  5. Done

Omkar Ghurye
  • 195
  • 1
  • 8