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
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 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)
// 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.
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