dongsicheng5262 2016-12-25 12:27
浏览 118
已采纳

奇怪的行为Laravel Homestead数据库连接

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

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.

  • 写回答

1条回答 默认 最新

  • doutan5844 2016-12-26 00:58
    关注

    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.

    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.

    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.

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?