Laravel 5 error SQLSTATE[HY000] [1045] Access deni

2020-05-24 20:42发布

问题:

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'

回答1:

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



回答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.



回答3:

You need to run these two commands

php artisan cache:clear
php artisan config:cache


回答4:

if you before use localhost and root your file be cached

remove /bootstap/cache/config.php



回答5:

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.



回答6:

This works to me:

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

Thanks.



回答7:

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.



回答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.



回答9:

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"



回答10:

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


回答11:

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.



回答12:

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=''


回答13:

Pls Update .env file

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

After then restart server



回答14:

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;


回答15:

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.



回答16:

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



回答17:

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.



回答18:

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


回答19:

  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.



回答20:

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=