Nginx – PDOException (1045) SQLSTATE[HY000] [1045] Laravel 4.2 MySQL 5.7

MySQLnginxphp7Ubuntu

thanks for reading my question, I had been working on this error since a week reading questions in ServerFault, Stackoverflow and AskUbuntu but couldn't figure out the problem.

I have

  • Lubuntu 16.04 (ubuntu distro)
  • Nginx
  • Laravel 4.2.17
  • MySQL 5.7

I have a pre-existed project which I cloned from GitHub and everything is going well but when I go to a page which has to connect to database (Like attempt a login) I got this error:

PDOException (1045) HELP

SQLSTATE[HY000] [1045] Access denied for user 'homestead'@'localhost' (using password: YES)

Graphic Description of the Error

FILES INVOLVE

I have a .env file in the project and I had named it .env.local.php this is the file:

<?php
/**
* Variables de entorno .env.php
* Si se requiere cargar otros valores para un entorno diferente al de produccion (local, development, etc.)
* cree un archivo con el nombre asi:
*  - local => .env.local.php
*  - development => .env.development.php
*/
return [

'DATABASE_NAME' => 'DATABASE_NAME',
'DATABASE_USER' => 'DATABASE_USER',
'DATABASE_PASSWORD' => 'DATABASE_PASSWORD',

/*
 * social
 * '$RED$_IDENTIFIER'   => '',
 * '$RED$_SECRET'       => '',
 * '$RED$_CALLBACK_URI' => '',
 */

'GOOGLE_IDENTIFIER'     => 'GOOGLE_IDENTIFIER',
'GOOGLE_SECRET'         => 'GOOGLE_SECRET',
'GOOGLE_CALLBACK_URI'   => 'GOOGLE_CALLBACK_URI',

'FACEBOOK_IDENTIFIER'   => 'FACEBOOK_IDENTIFIER',
'FACEBOOK_SECRET'       => 'FACEBOOK_SECRET',
'FACEBOOK_CALLBACK_URI' => 'FACEBOOK_CALLBACK_URI',

/**
 * PayU configs
 * PAYU_TEST: 1 = Modo prueba, 0 = Modo Produccion
 */
'PAYU_MACRO_ACCOUNT_ID' => 654321,
'PAYU_MICRO_ACCOUNT_ID' => 123456,
'PAYU_MERCHANT_ID' => 123456,
'PAYU_API_KEY' => 'ABCabc123DEFdef456GHIghi78',
'PAYU_API_LOGIN' => '123abc456def789',
'PAYU_TEST' => 1,

];

I have a database.php file in /var/www/kinbuweb/app/config/local/database.php I highlight the /config/local path which I've been reading is the configuration if I have a local enviroment (and I had named my file .env.local.php) the file in the path:

<?php

return [

/*
|--------------------------------------------------------------------------
| Database Connections
|--------------------------------------------------------------------------
|
| Here are each of the database connections setup for your application.
| Of course, examples of configuring each database platform that is
| supported by Laravel is shown below to make development simple.
|
|
| All database work in Laravel is done through the PHP PDO facilities
| so make sure you have the driver for your particular database of
| choice installed on your machine before you begin development.
|
*/

'connections' => [

    'mysql' => [
        'driver'   => 'mysql',
        'host'     => 'localhost',
        'database' => 'homestead',
        'username' => 'homestead',
        'password' => 'secret',
        'charset'  => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'   => '',
    ],

    'pgsql' => [
        'driver'  => 'pgsql',
        'host'    => 'localhost',
        'database' => 'homestead',
        'username' => 'homestead',
        'password' => 'secret',
        'charset' => 'utf8',
        'prefix'  => '',
        'schema'  => 'public',
    ],

],

];

On the other hand I have a file out of the local folder (Path: /var/www/kinbuweb/app/config) which is called database.php too, I think is the general configuration it has:

<?php

return [

/*
|--------------------------------------------------------------------------
| PDO Fetch Style
|--------------------------------------------------------------------------
|
| By default, database results will be returned as instances of the PHP
| stdClass object; however, you may desire to retrieve records in an
| array format for simplicity. Here you can tweak the fetch style.
|
*/

'fetch'       => PDO::FETCH_CLASS,

/*
|--------------------------------------------------------------------------
| Default Database Connection Name
|--------------------------------------------------------------------------
|
| Here you may specify which of the database connections below you wish
| to use as your default connection for all database work. Of course
| you may use many connections at once using the Database library.
|
*/

'default'     => 'mysql',

/*
|--------------------------------------------------------------------------
| Database Connections
|--------------------------------------------------------------------------
|
| Here are each of the database connections setup for your application.
| Of course, examples of configuring each database platform that is
| supported by Laravel is shown below to make development simple.
|
|
| All database work in Laravel is done through the PHP PDO facilities
| so make sure you have the driver for your particular database of
| choice installed on your machine before you begin development.
|
*/

'connections' => [

    'sqlite' => [
        'driver' => 'sqlite',
        'database' => __DIR__ . '/../database/production.sqlite',
        'prefix' => '',
    ],

    'mysql'  => [
        'driver'   => 'mysql',
        'host'     => 'localhost',
        'database' => $_ENV['DATABASE_NAME'],
        'username' => $_ENV['DATABASE_USER'],
        'password' => $_ENV['DATABASE_PASSWORD'],
        'charset'  => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'   => '',
    ],

    'pgsql'  => [
        'driver'  => 'pgsql',
        'host'    => 'localhost',
        'database' => 'forge',
        'username' => 'forge',
        'password' => '',
        'charset' => 'utf8',
        'prefix'  => '',
        'schema'  => 'public',
    ],

    'sqlsrv' => [
        'driver' => 'sqlsrv',
        'host'   => 'localhost',
        'database' => 'database',
        'username' => 'root',
        'password' => '',
        'prefix' => '',
    ],

],

/*
|--------------------------------------------------------------------------
| Migration Repository Table
|--------------------------------------------------------------------------
|
| This table keeps track of all the migrations that have already run for
| your application. Using this information, we can determine which of
| the migrations on disk haven't actually been run in the database.
|
*/

'migrations'  => 'migrations',

/*
|--------------------------------------------------------------------------
| Redis Databases
|--------------------------------------------------------------------------
|
| Redis is an open source, fast, and advanced key-value store that also
| provides a richer set of commands than a typical key-value systems
| such as APC or Memcached. Laravel makes it easy to dig right in.
|
*/

'redis'       => [

    'cluster' => false,

    'default' => [
        'host' => '127.0.0.1',
        'port' => 6379,
        'database' => 0,
    ],

],

];

What I had tried

  1. Use the credentials in the local (/var/www/kinbuweb/app/config/local/database.php) database.php configuration like this:

    <?php
    /**
    * Variables de entorno .env.php
    * Si se requiere cargar otros valores para un entorno diferente al de produccion (local, development, etc.)
    * cree un archivo con el nombre asi:
    *  - local => .env.local.php
    *  - development => .env.development.php
    */
    return [
    
    'DATABASE_NAME' => 'homestead',
    'DATABASE_USER' => 'homestead',
    'DATABASE_PASSWORD' => 'secret',
    
    
    
    'GOOGLE_IDENTIFIER'     => 'GOOGLE_IDENTIFIER',
    'GOOGLE_SECRET'         => 'GOOGLE_SECRET',
    'GOOGLE_CALLBACK_URI'   => 'GOOGLE_CALLBACK_URI',
    
    'FACEBOOK_IDENTIFIER'   => 'FACEBOOK_IDENTIFIER',
    'FACEBOOK_SECRET'       => 'FACEBOOK_SECRET',
    'FACEBOOK_CALLBACK_URI' => 'FACEBOOK_CALLBACK_URI',
    
    
    'PAYU_MACRO_ACCOUNT_ID' => 654321,
    'PAYU_MICRO_ACCOUNT_ID' => 123456,
    'PAYU_MERCHANT_ID' => 123456,
    'PAYU_API_KEY' => 'ABCabc123DEFdef456GHIghi78',
    'PAYU_API_LOGIN' => '123abc456def789',
    'PAYU_TEST' => 1,
    
    ];
    
  2. Aggregate the port to the database configuration in the local folder like this:

    <?php
    
    return [
    
    'connections' => [
    
    'mysql' => [
        'driver'   => 'mysql',
        'host'     => 'localhost:3306',
        'database' => 'homestead',
        'username' => 'homestead',
        'password' => 'secret',
        'charset'  => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'   => '',
        ],
    
    'pgsql' => [
        'driver'  => 'pgsql',
        'host'    => 'localhost',
        'database' => 'homestead',
        'username' => 'homestead',
        'password' => 'secret',
        'charset' => 'utf8',
        'prefix'  => '',
        'schema'  => 'public',
       ],
    
    ],
    
    ];
    

And while I got the same error:
PDOException (1045) HELP

SQLSTATE[HY000] [1045] Access denied for user 'homestead'@'localhost' (using password: YES)

Any ideas with I could solve this? Thanks

Best Answer

Try do add privilege for your user 'homestead' in your mysql for you database.

Log into mysql and add privilege like this:

mysql -u root -p

Enter your password of MySQL

mysql > grant all privileges on homestead *.* to 'homestead'@'localhost' identified by 'password'; 

'password' here is in your database.php

If you are in local enviroment the path for database.php is:

yourporjectfolder/app/config/local

mysql > FLUSH PRIVILEGES;