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

2020-05-24 19:55发布

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'

20条回答
Lonely孤独者°
2楼-- · 2020-05-24 20:25

You need to run these two commands

php artisan cache:clear
php artisan config:cache
查看更多
对你真心纯属浪费
3楼-- · 2020-05-24 20:30

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;
查看更多
可以哭但决不认输i
4楼-- · 2020-05-24 20:31

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

查看更多
我命由我不由天
5楼-- · 2020-05-24 20:32

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.

查看更多
ゆ 、 Hurt°
6楼-- · 2020-05-24 20:34

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"

查看更多
劫难
7楼-- · 2020-05-24 20:34

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.

查看更多
登录 后发表回答