奇怪的行为Laravel Homestead数据库连接 [英] Strange behaviour Laravel Homestead Database Connection

查看:55
本文介绍了奇怪的行为Laravel Homestead数据库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Laravel,Homestead和MySQL时遇到一个特殊的错误.这是我的.env文件中与数据库相关的部分:

I am experiencing a peculiar error working with Laravel, Homestead and MySQL. This is the part of my .env file related to the database:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=33060
DB_DATABASE=mydatabasename
DB_USERNAME=homestead
DB_PASSWORD=secret

如果将端口设置为3306,则可以从应用程序访问表,但无法从终端执行命令,例如php artisan migration.如果将端口设置为33060,则可以从终端执行命令,但是无法从应用程序访问表.

If I set the port to 3306 I can access the tables from my application but I cannot execute commands from Terminal such as php artisan migrate. If I set the port to 33060 I can execute commands from Terminal but I cannot access the tables from my application.

推荐答案

您的应用程序正在 Homestead.yaml 中提供的IP上运行,因此当localhost相对于您的应用程序时,端口3306可以正常工作.在不通过SSH进入无所事事的vm的情况下运行artisan时,您正在相对于计算机的本地主机而不是vm运行命令,因此,您将尝试在没有数据库的计算机上运行迁移.

Your application is running on the IP provided in Homestead.yaml so when localhost is relative to your application port 3306 works. When running artisan while not SSH into your vagrant vm you are running the command relative to the localhost of your machine, not the vm, so your'e trying to run the migrations against a machine with no database.

端口33060适用于本地计算机的原因是因为Homestead默认将此端口转发到您的无用虚拟机端口3306.但是由于您的 .env 现在指定了无用的虚拟机端口33060现在无法访问端口3306.

The reason that port 33060 works for your local machine is because Homestead by default forwards this port to your vagrant virtual machines port 3306. But due to your .env now specifying port 33060 the vagrant virtual machine now can not reach port 3306.

通过端口3306和SSH通过 vagrant ssh 命令将 DB_HOST 设置为 127.0.0.1 到您的虚拟机vm中,以运行迁移命令.

Leave the DB_HOST set to 127.0.0.1 with port 3306 and SSH into your vagrant vm via the vagrant ssh command to run your migration command.

或者,您可以针对您的各种环境使用多个 .env 文件

Or alternatively you could have multiple .env files for your various environments

这篇关于奇怪的行为Laravel Homestead数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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