problems with database connection in laravel 5

25,042

Solution 1

You need to change the values in your .env file, located in the root folder of your project.

If there is no .env file, copy the .env.example file to .env.

Laravel uses this file to protect your passwords. The values as you are setting those are only used if there is no configuration available in the .env file, homestead is the standard user there.

Solution 2

You can go to .env file. File available on Project Root folder.

Then some here related to your database.

enter image description here

Then run below command to clear the old configuration cache.

php artisan config:clear

Solution 3

Have this problem as well. After update .env and run php artisan config:clear problem still persisted! Then after stoping the server and restart the server again with php artisan serve, problem fixed!

Solution 4

Change the following attribute from database.php

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

to

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

This must work well then run the following command:

php artisan migrate:install

Solution 5

I solve this issue by update .env file with config/database.php file.

here is my config/database.php structure (I am using mysql)

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

And .env file structure (same like config/database.php)

 DB_HOST=localhost      // Host name
 DB_DATABASE=larablog   // Database name
 DB_USERNAME=root       // Database User
 DB_PASSWORD=           // Db password(No password here) 
Share:
25,042
Gaetan Sobze
Author by

Gaetan Sobze

Updated on July 15, 2022

Comments

  • Gaetan Sobze
    Gaetan Sobze almost 2 years

    I use the command php artisan migrate to migrate my database connection but I still get the same error and I checked everything, nothing wrong. I used the same connection that I always use in Laravel 4.2

    Here is the message I get on my console:

    exception 'PDOException' with message 'SQLSTATE[HY000] [1045] Access denied for user 'homestead'@'localhost' (using password: YES)' in C:\xampp\htdocs\Projects\blog\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:47