Laravel .env DB_HOST 127.0.0.1和localhost [英] Laravel .env DB_HOST 127.0.0.1 vs localhost

查看:427
本文介绍了Laravel .env DB_HOST 127.0.0.1和localhost的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Laravel环境文件中的DB_HOST变量感到困惑. localhost127.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中选择*,其中email = 电子邮件地址限制1)

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

如果我使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆