Mysql Connection with Laravel 5.0

1

I am making the connection to the database mysql with laravel using the following code in the file database.php

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

File .env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=estoque_laravel
DB_USERNAME=root
DB_PASSWORD=123456

But when I make a select in the database, I get the following error

PDOException in Connector.php line 47:
SQLSTATE[HY000] [1045] Access denied for user 'homestead'@'localhost' (using password: YES)
    
asked by anonymous 04.08.2017 / 16:47

1 answer

2

You need to put the bank connection information in the file .env ;

In the way you are doing 'database' => env('DB_DATABASE', 'estoque_laravel') laravel is interpreting this way:

  

Go to the file .env , search for the DB_DATABASE variable, and place the   value of it in database . If it does not find estoque_laravel .

If the variable is not informed, it will empty field for the variable database , because the validation is if it exists and there is no information in it. So, put the connection information in the variables in the file .env :

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=estoque_laravel
DB_USERNAME=root
DB_PASSWORD=123456

Here's the real example I'm using. In my case locally I use the user homestead and password secret :

file .env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

file config/database.php

'mysql' => [
    'driver' => 'mysql',
    'host' => env('DB_HOST', '127.0.0.1'),
    'port' => env('DB_PORT', '3306'),
    'database' => env('DB_DATABASE', 'forge'),
    'username' => env('DB_USERNAME', 'forge'),
    'password' => env('DB_PASSWORD', ''),
    'unix_socket' => env('DB_SOCKET', ''),
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'prefix' => '',
    'strict' => true,
    'engine' => null,
],

If your configuration is exactly like this, check how you set the configuration in your bank, because everything is set up correctly by laravel.

    
04.08.2017 / 17:01