Laravel 5:在服务器环境而不是本地运行迁移
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32094930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5: run migrations on server environment, not local
提问by Leon
I have a simple set of database migrations created within my Laravel 5 application, and they run nicely on my local development environment.
我在 Laravel 5 应用程序中创建了一组简单的数据库迁移,它们在我的本地开发环境中运行良好。
Now its time to run to run the migration on my new production server environment. I have configured the DB connection and deployed the app, and the app sees the database, but there are no tables - so migrations need to be run.
现在是时候在我的新生产服务器环境上运行迁移了。我已经配置了数据库连接并部署了应用程序,应用程序看到了数据库,但没有表 - 所以需要运行迁移。
The following command, I believe, should run the migrations using the "production" environment, which is set up with the remote DB connection details:
我相信以下命令应该使用“生产”环境运行迁移,该环境使用远程数据库连接详细信息进行设置:
php artisan --env=production migrate
The migration works, but it runs on the local environment! Here is the environment file for my production environment(using amazon elastic beanstalk service) :
迁移有效,但它在本地环境中运行!这是我的生产环境的环境文件(使用 amazon elastic beanstalk 服务):
.elasticbeanstalk.env
.elasticbeanstalk.env
APP_ENV=production
APP_DEBUG=true
APP_URL=<myappname.elasticbeanstalk.com>
DB_HOST=<myapp.amazonserveraddress.amazonaws.com:3306>
DB_DATABASE=<mydbname>
DB_USERNAME=<mydbusername>
DB_PASSWORD=<mydbpassword>
So either my environment file is not configured correctly, or artisan is not able to switch to that environment. I could change my .env file (local development environment, named "local") to connect to the remote, production DB, but I want to properly use Laravels environments.
所以要么我的环境文件配置不正确,要么 artisan 无法切换到该环境。我可以更改我的 .env 文件(本地开发环境,名为“local”)以连接到远程生产数据库,但我想正确使用 Laravel 环境。
What am I missing? Why does the migration always run on "local"?
我错过了什么?为什么迁移总是在“本地”上运行?
Thanks.
谢谢。
回答by alexrussell
You can't run any remote commands on your local artisan
. Anything you run there will only work locally (even if you set the ENV
variable).
您无法在本地运行任何远程命令artisan
。您在那里运行的任何内容都只能在本地运行(即使您设置了ENV
变量)。
Setting the ENV
variable is just to tell the application to behave as if it is inthat environment. But doesn't tell artisan to use the remote production environment.
设置ENV
变量只是为了告诉应用程序的行为就像它在该环境中一样。但并没有告诉工匠使用远程生产环境。
If you want to run commands on your production server, I suggest you look into Envoy. It is a completely standalone project (and does not have to be used only with Laravel projects) and is specifically for deployment.
如果你想在你的生产服务器上运行命令,我建议你查看Envoy。它是一个完全独立的项目(不必只与 Laravel 项目一起使用)并且专门用于部署。
It is basically a thin wrapper around SSHing into your remote server and then running commands. An example Envoy.blade.php
file on my sites might look like this:
它基本上是将 SSH 连接到远程服务器然后运行命令的瘦包装器。Envoy.blade.php
我网站上的示例文件可能如下所示:
@servers(['web' => 'account@server'])
@task('deploy')
cd ~/src
php artisan down
git pull origin master
composer install --no-dev --no-progress --prefer-dist
php artisan migrate --force --no-interaction
php artisan optimize
php artisan up
@endtask
This SSHes in, puts the application in maintenance mode, pulls the new code, does the various 'new code' setups like composer install, migrate, etc. and then pulls the application out of maintenance mode.
此 SSH 进入,将应用程序置于维护模式,提取新代码,执行各种“新代码”设置,例如 composer install、migrate 等,然后将应用程序从维护模式中拉出。