Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel .env DB_HOST 127.0.0.1 vs localhost

I am confused about the DB_HOST variable in my Laravel environment file. Neither localhost or 127.0.0.1 appear to be working completely.

If the host is set to 127.0.0.1, when I attempt to run a query within Laravel it returns the error:

SQLSTATE[HY000] [2002] Connection refused (SQL: select * from users where email = email address limit 1)

Whereas, if I use localhost, it connects fine.

However, when the DB_HOST variable is set to localhost, and I attempt to run migrations or seeds, I am presented with the error:

SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = projectmanage and table_name = migrations)

Whereas, if I use DB_HOST 127.0.0.1, it migrates/seeds fine.

What is going on?

I am currently working with an installation of Laravel 5.5.

like image 983
AdamMcquiff Avatar asked Sep 16 '25 06:09

AdamMcquiff


1 Answers

You probably have a database user that can sign in using localhost and not 127.0.0.1. Check the mysql.users table to verify. You can add an entry for 127.0.0.1 to connect with either.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!