Question

[Solved] Laravel 5 error SQLSTATE[HY000] [1045] Access denied for user ‘homestead’@’localhost’ (using password: YES)

I have installed Laravel 5 successfully and changed MySQL credentials in database.php file in config directory to

mysql' => [
            'driver'    => 'mysql',
            'host'      => env('DB_HOST', 'localhost'),
            'database'  => env('DB_DATABASE', 'wdcollect'),
            'username'  => env('DB_USERNAME', 'root'),
            'password'  => env('DB_PASSWORD', ''),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
        ],

I don’t want to use homestead and I have changed .env file to

APP_ENV=local
APP_DEBUG=true
APP_KEY=apLIzEMOgtc5BUxdT9WRLSvAoIIWO87N

DB_HOST=localhost
DB_DATABASE=wdcollect
DB_USERNAME=root
DB_PASSWORD=

CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null

I don’t understand that why it’s saying that access denied for ‘homestead’@’localhost’

Solution #1:

It’s working now. I had to restart server.
Thanks

Respondent: Ali Rehman

Solution #2:

I was facing the same issue. Everything was fine but in

bootstrap/cache/config.php

always had the incomplete password. Upon digging further, realized that the password had ‘#’ character in it and that was getting dropped. As ‘#’ is used to mark a line as a comment.

Respondent: umair.ashfr

Solution #3:

You need to run these two commands

php artisan cache:clear
php artisan config:cache

Solution #4:

None of these worked for me when I deployed my website online on shared hosting, below is what I did that worked.

In the .env file, I changed

DB_HOST=127.0.0.1

to

DB_HOST=localhost

and viola, it worked well as expected.

Respondent: BlackPearl

Solution #5:

if you before use localhost and root your file be cached

remove /bootstap/cache/config.php

Respondent: Omid Ahmadyani

Solution #6:

I spent hours on this ūüôĀ and finally, it was really easy to hack

BEFORE

DB_PASSWORD=#Root2020$

UPDATED

DB_PASSWORD="#Root2020$"

I think the problem was # in the password, that’s why I had to write my password into quotation marks.

Respondent: zarpio

Solution #7:

This works to me:

php artisan config:clear
php artisan cache:clear
php artisan config:cache

Thanks.

Respondent: Y. Joy Ch. Singha

Solution #8:

I had the same problem, so I realized that the .env file does not accept special characters, as my password has these characters, I added the password in the config/database.php file.

Respondent: Mauro Lacerda

Solution #9:

I Faced Same Problem what i did

  1. php artisan cache:clear
  2. php artisan config:cache

but Same problem found than i run this artisan command

php artisan view:clear

Hope it will helpful.

Respondent: Arman H

Solution #10:

Jeez!
Thanks to umair.ashfr answer here below, I finally hit the nail with an issue that was eating my neurons.

I had a password like
DB_PASSWORD=ffjdh5#fgr
on my .env, and MySQL connection was gracefully saying that “SQLSTATE[HY000] [1045] Access denied for user…”, no matter what flavor of php artisan I was trying to run.

The problem was that “#” sign inside the password.
BEWARE! That means the start of a comment, thus my password was silently truncated!
The solution is to declare password like
DB_PASSWORD=”ffjdh5#fgr”

Regards

Respondent: Internetbug256

Solution #11:

You do not need to set credentials in database.php file. It is enough if you have credentials in .env

If you are able to login to database directly then this password must work. It can be possible that you are having different environment than “local” which is defined in this file. Test is with “php artisan env”

Respondent: Margus Pala

Solution #12:

Try to checkout the “.env” file in your root directory. It will be a hidden file. Correct these values.

 DB_HOST=localhost
 DB_DATABASE=homestead
 DB_USERNAME=homestead
 DB_PASSWORD=secret
Respondent: Harish Lalwani

Solution #13:

Instead of using this

DB_HOST=localhost
DB_DATABASE=wdcollect
DB_USERNAME=root
DB_PASSWORD=

USE this

DB_HOST=localhost
DB_DATABASE=wdcollect
DB_USERNAME=root
DB_PASSWORD=''
Respondent: Ali Khurram

Solution #14:

Please update below file.

vendor – .env – on line#7

 DB_HOST=localhost
 DB_DATABASE=homestead
 DB_USERNAME=homestead
 DB_PASSWORD=secret

Instead of homestead user your database, username and password. This should work for you.

Respondent: golucoder

Solution #15:

In my case, it was actually root user permission problem. My Laravel configurations were all perfect, but later I found that it was root user permission problem since my MySQL configs were somehow changed.

  • so i navigated to xampp folder > C:xamppmysqlbin
  • searched for my.ini file and opened it in editor then added added skip-grant-tables in following place:

[mysqld] skip-grant-tables port=3306

and it worked.

Respondent: iamnabink

Solution #16:

Declare password like DB_PASSWORD=”[email¬†protected]#5TGR1NG” in .env file

If # sign include in your password then password will declare in between ” “.

Respondent: Chintan Hingrajia

Solution #17:

Pls Update .env file

 DB_HOST=localhost
 DB_DATABASE=homestead
 DB_USERNAME=homestead
 DB_PASSWORD=secret

After then restart server

Respondent: Faridul Khan

Solution #18:

I came up with this too…
then I solve it by putting the database information directly in ” database.php ”
In your case, I would change the information like this

mysql' => [
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'database'  => 'wdcollect',
        'username'  => 'root',
        'password'  => '',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
    ],

and don’t forget to change your setting in XAMPP’s config.inc

['auth_type'] = 'config';
['Servers'][$i]['user'] = 'root';
['Servers'][$i]['password'] = '';
['AllowNoPassword'] = true;
Respondent: Let’s Yo

Solution #19:

Match the .env file and the config.php file with your username and password and your hostname in the database settings.if they are not equal,relation will not connect.

Respondent: asghar

Solution #20:

If you are on MAMP

Check your port number as well generally it is

Host localhost

Port 8889

User root

Password root

Socket /Applications/MAMP/tmp/mysql/mysql.sock

Respondent: shivanisdev

Solution #21:

The .env file should have same database name , username and password as in the mysql database and check whether all permissions are granted to the user for accessing the database or not.
I solved my problem by adding the cpanel username in front of database name and username
like jumbo_admingo and jumbo_user1 respectively where jumbo is my cpanel username and admingo is the database name i created in mysql and user1 is the user which has been provided the access to the database admingo.
THIS SOLVED MY PROBLEM.

Respondent: Jatin Yadav

Solution #22:

Reason is the old database credentials are cached /bootstap/cache/config.php

In the .env file, I modified it as follow

 DB_HOST=localhost
 DB_DATABASE=homestead
 DB_USERNAME=homestead
 DB_PASSWORD=secret

Then removed that file

/bootstap/cache/config.php 

If the issue still there you might try the following.

php artisan config:clear
php artisan cache:clear
php artisan config:cache

Exit vagrant by writing the exit command
Then restart vargarnt/homestead config

vagrant reload --provision

Then opened vagrant again

Vagrant Up
Vagrant ssh
Respondent: AhmedR

Solution #23:

  1. Edit the file .env in your laravel root directory. make looks as in below :

     DB_HOST=localhost
     DB_DATABASE=laravel
     DB_USERNAME=root
     DB_PASSWORD=your-root-pas
    
  2. Also create one database in phpmyadmin named, “laravel”.

  3. Run below commands :

     php artisan cache:clear
     php artisan config:cache
     php artisan config:clear   
     php artisan migrate
    

It worked for me, XAMPP with Apache and MySQL.

Respondent: Neri Kol

Solution #24:

Open terminal on XAMPP > go to /opt/lampp/htdocs/project_name > run php artisan migrate

.env file

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=8080
DB_DATABASE=database_name
DB_USERNAME=root
DB_PASSWORD=

laravel: php artisan migrate on xampp terminal

Respondent: Naiguel Developer

Solution #25:

in my case I wanted to connect to the database installed on windows that i used to access through workbench, so i replaced localhost with the ip address which i obtained by typing ipconfig on windows command prompt
enter image description here

Respondent: Shah Aadil

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy