Adam McKenna
Adam McKenna

Reputation: 2445

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.

Upvotes: 4

Views: 9443

Answers (1)

user320487
user320487

Reputation:

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.

Upvotes: 3

Related Questions