Framework: php artisan migrate doesn't read .env file for database infomation

Created on 30 Jul 2019  路  5Comments  路  Source: laravel/framework

  • Laravel Version: 5.8.17
  • PHP Version: 7.2.10
  • Database Driver & Version:

Description:

  • When I do php artisan migrate, it does't read database information from my .env file. It give error.

    Illuminate\Database\QueryException : SQLSTATE[HY000] [1045] Access denied for user 'homestead'@'localhost' (using password: YES) (SQL: select * from information_schema.tables where table_schema = homestead and table_name = migrations and table_type = 'BASE TABLE')

    at /Users/supanida/http/utrack/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664
    660| // If an exception occurs when attempting to run a query, we'll format the error
    661| // message to include the bindings with SQL, which will make this exception a
    662| // lot more helpful to the developer instead of just the database's errors.
    663| catch (Exception $e) {

    664| throw new QueryException(
    665| $query, $this->prepareBindings($bindings), $e
    666| );
    667| }
    668|

    Exception trace:

    1 PDOException::("SQLSTATE[HY000] [1045] Access denied for user 'homestead'@'localhost' (using password: YES)")
    /Users/supanida/http/utrack/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70

    2 PDO::__construct("mysql:host=127.0.0.1;port=3306;dbname=homestead", "homestead", "secret", [])
    /Users/supanida/http/utrack/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70

    Please use the argument -v to see more details.

  • But when I do try it connect database via eloquent, it can connect normally

Steps To Reproduce:

  1. composer create-project ......
  2. create database
  3. setup .env file
  4. run php artisan migrate

Most helpful comment

The above 2 responses are correct, try those; however... Reviewing this error there is another possible solution for you.

Access denied for user 'homestead'@'localhost'

I'd be willing to bet you are trying to use an external database without remote connections enables. Please see this:

https://www.digitalocean.com/community/tutorials/how-to-allow-remote-access-to-mysql

All 5 comments

The credentials showing up in your stack trace are the Laravel defaults. Can you verify if you actually have a database called homestead on localhost? Easiest way to check this is using a query browser like Sequel Pro, or directly in a terminal prompt with mysql -u homestead -D homestead -psecret

  • Check if you're using the correct port ( i had the same issue cuz i used mariadb which connects at 3307)
  • Check if you're using the correct artisan file and not calling an other project's artisan (silly, but oh well)

  • Check if you have your config cached (php artisan config:clear to clear that, because)

  • Get the .env file path and check if it's correct
// Put something like this in AppServiceProvider boot() method
// Then run your php artisan command, this should break the artisan output
// And display the .env variable instead
// If this command didn't break the artisan then this artisan doesnt belong to this project (the same silly thing)
// And if it did break, then you will have a clear filepath of the .env file

public function boot()  // <-- AppServiceProvider's Boot method.
    {   
         // 
        dd(app()->environmentFilePath(), // <-- returns the .env path
            env('DB_PORT'), // <-- returns db port
            env('DB_DATABASE'), // <-- returns database name
            env('DB_PASSWORD'), // <-- returns db password
            env('DB_HOST'), // <-- returns db host
        );

When done, run any php artisan ... command, or simply run php artisan without anything after it.
These dd's should display your env path along with database configs, you can debug from there.


Let me know how it goes.

The above 2 responses are correct, try those; however... Reviewing this error there is another possible solution for you.

Access denied for user 'homestead'@'localhost'

I'd be willing to bet you are trying to use an external database without remote connections enables. Please see this:

https://www.digitalocean.com/community/tutorials/how-to-allow-remote-access-to-mysql

@yordadev
'localhost' will resolve to 127.0.0.1 which is a local connection not a remote connection.

Thank you for all of your replies. It works normally when I connect to database using eloquent models, but it just didn't work on the command line "php artisan migrate"

I did check the port as @emadha suggested. My environment uses mysql on localhost (So @yordadev , nope I didn't use external database) which uses port 3306 as is set in the .env file

Well, I have tried everything you guys suggested, but it still didn't work. So the last choice I have was to reboot the machine. After I reboot the machine, everything works as normal without any changes.

Thank you again for all your helps.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

PhiloNL picture PhiloNL  路  3Comments

kerbylav picture kerbylav  路  3Comments

iivanov2 picture iivanov2  路  3Comments

YannPl picture YannPl  路  3Comments

klimentLambevski picture klimentLambevski  路  3Comments