.env文件中默认有两个DB_HOST,其中一个被注释掉了,则两个有啥区别?
.env
文件下:
将127.0.0.1
的注释掉,保留mysql
的,可以连接上数据库,也可以对数据库进行 CURD,但是php artisan migrate
会报错:
vagrant@homestead:~/projects/demo$ php artisan migrate
Illuminate\Database\QueryException
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution (SQL: select * from information_schema.tables where table_schema = demo and table_name
= migrations and table_type = 'BASE TABLE')
at vendor/laravel/framework/src/Illuminate/Database/Connection.php:678
674▕ // If an exception occurs when attempting to run a query, we'll format the error
675▕ // message to include the bindings with SQL, which will make this exception a
676▕ // lot more helpful to the developer instead of just the database's errors.
677▕ catch (Exception $e) {
➜ 678▕ throw new QueryException(
679▕ $query, $this->prepareBindings($bindings), $e
680▕ );
681▕ }
682▕
+33 vendor frames
34 artisan:37
Illuminate\Foundation\Console\Kernel::handle()
而把mysql
的注释掉,保留127.0.0.1
的,就不会出现问题。
这两者有啥区别吗?为什么会出现这种情况?
有点像部署在 Docker 里的样
注释掉就是字面意思注释掉了,只有未注释的才会被使用
一个是通过hostname连接,一个是通过ip连接,而你执行DDL时是否对配置权限进行了控制
docker 内映射的hosts,
在Docker中,连接数据库 DB_host 要设置成服务名
应该是用的 docker 环境吧, 运行迁移命令报错是因为你是在宿主机运行的
@Epona 没理解人家意思啊
一般来说是根据环境解析的,例如docker,它自动将mysql 解析成mysql 所在容器的ip。但是如果你的环境全部部署在本机,那么这个配置就失效了,就应该换成 127.0.0.1
env 里面的配置都是和 config/*.php 里面的有关,比如 DB_HOST,可以看看 config/database.php 里面是如何使用这个环境变量的
mysql 部署在 docker 中一般会这么使用,我之前用的时候也有这个问题,执行数据库迁移命令,但是忘了怎么处理了,后面没用过了😭