Amr
Amr

Reputation: 5159

Laravel 4.2: (Access denied for user 'homestead'@'localhost') error

I get the following error message in the terminal when I try to execute php artisan migrate command

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

I don't use homestead. I have these 2 files in the root directory: .env.php and .env.local.php in which I keep sensitive values and values that differ between environments (development and production). So what's wrong and how to fix the problem?

Upvotes: 2

Views: 2872

Answers (1)

Amr
Amr

Reputation: 5159

  1. Type hostname in your terminal and hit Enter key. This will let you know your hostname (Name of your computer/machine).
  2. In bootstrap/start.php file, use this line 'local' => array('put-your-hostname-here'), instead of this line 'local' => array('homestead'),
  3. In .env.local.php file, save your local database configuration like this:

    <?php
    
    return array(
    
        "DB_NAME"     => "local_database_name",
        "DB_USERNAME" => "local_database_username",
        "DB_PASSWORD" => "local_database_password",
    
        // any other configuration..
    
    );
    
  4. In .env.php file, save your remote database configuration like this:

    <?php
    
    return array(
    
        "DB_NAME"     => "remote_database_name",
        "DB_USERNAME" => "remote_database_username",
        "DB_PASSWORD" => "remote_database_password",
    
        // any other configuration..
    
    );
    
  5. In app/config/database.php file, set your database connection like this:

    'mysql' => array(
        'driver'    => 'mysql',
        'host'      => 'localhost',
    
        'database'  => $_ENV['DB_NAME'],
        'username'  => $_ENV['DB_USERNAME'],
        'password'  => $_ENV['DB_PASSWORD'],
    
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ),
    
  6. Finally, you MUST remove or disable any working code exists in app/config/local/database.php file, otherwise the error you mentioned will be thrown.

Upvotes: 1

Related Questions