Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

pritam1605's avatar

localhost vs 127.0.0.1 during migration and using models

I am very new to Laravel. I am using Homestead. When I try to any migration using php artisan migrate command I have to use DB_HOST=127.0.0.1 in the .env file. If I use localhost instead of 127.0.0.1 if get the following error: [PDOException] SQLSTATE[HY000] [1045] Access denied for user 'homestead'@'localhost' (using password: YES)

Whereas, when I am using eloquent functionality in models and trying to populate the database I have to change DB_HOST in .env file to localhost i.e. DB_HOST=localhost, if not then I get the following error: PDOException in Connector.php line 55: SQLSTATE[HY000] [2002] Connection refused

I have no idea about this. I am all confused. Where am I going wrong??

0 likes
2 replies
Mittensoff's avatar

Maybe you'll solve it by putting localhost instead of 127.0.0.1 in your MySQL server.

tykus's avatar

Homestead uses 192.168.10.10 - if you use this then you can run migrations / tinker from the host as well.

Please or to participate in this conversation.