Mysql connection to Laravel 5.0

Asked

Viewed 195 times

1

I’m making the connection to the database mysql with laravel using the following code in the archive 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,
        ],

Filing cabinet .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 go to make a select in the bank, I have the following mistake

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

1 answer

2


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

In the way you’re doing 'database' => env('DB_DATABASE', 'estoque_laravel') the Portable is interpreting so:

Go to the file .env, look for the variable DB_DATABASE and put the value of it in database. If not find place 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

Follow real example I’m using. In my case locally I use the user homestead and password secret:

filing cabinet .env

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

filing cabinet 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 on the part of the Laravel everything is configured correctly.

  • I made this change, but the error persists :/

  • Is the error exactly the same? With the same information: Access denied for user 'homestead'@'localhost'. Was set the user root

  • yes, exactly the same

  • I updated the answer. Check the end of it and do some tests in the access configuration in your bank if it is configured according to the Standard

  • Those settings are from Laravel 5.0 ? because I’m using 5.0

  • Yes. This setting is compatible with the 5.0 Standard

  • 1

    So, the settings were being cached by the server, I cleared the cache and everything worked normally

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.