Laravel .env DB_HOST 127.0.0.1和localhost [英] Laravel .env DB_HOST 127.0.0.1 vs localhost
问题描述
我对Laravel环境文件中的DB_HOST
变量感到困惑. localhost
或127.0.0.1
似乎都不能完全正常工作.
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.
如果主机设置为127.0.0.1
,当我尝试在Laravel中运行查询时,它将返回错误:
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]连接被拒绝(SQL:从
users
中选择*,其中
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from
users
where
如果我使用localhost
,则连接正常.
Whereas, if I use localhost
, it connects fine.
但是,当DB_HOST
变量设置为localhost
时,当我尝试运行迁移或种子时,出现以下错误:
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]连接被拒绝(SQL:从information_schema.tables中选择*,其中table_schema = projectmanage和table_name =迁移)
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = projectmanage and table_name = migrations)
如果我使用DB_HOST 127.0.0.1
,它可以很好地迁移/播种.
Whereas, if I use DB_HOST 127.0.0.1
, it migrates/seeds fine.
这是怎么回事?
我目前正在安装Laravel 5.5.
I am currently working with an installation of Laravel 5.5.
推荐答案
您可能有一个数据库用户,该用户可以使用localhost而不是127.0.0.1进行登录.检查mysql.users表以进行验证.您可以为127.0.0.1添加一个条目以与其中任何一个建立连接.
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.
这篇关于Laravel .env DB_HOST 127.0.0.1和localhost的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!