Artisan迁移在开发环境中失败,因为生产配置中缺少环境变量 [英] Artisan migrate fails in development environment because of missing environment variable in production config

查看:64
本文介绍了Artisan迁移在开发环境中失败,因为生产配置中缺少环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数据库配置,一个用于生产,一个用于开发:

I have two database configs, one for production and one for development:

// app/config/database.php
'connections' => array(
    'mysql' => array(
        'driver'    => 'mysql',
        'host'      => $_SERVER['RDS_HOSTNAME'],
        'database'  => $_SERVER['RDS_DB_NAME'],
        'username'  => $_SERVER['RDS_USERNAME'],
        'password'  => $_SERVER['RDS_PASSWORD'],
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    )
)

// app/config/development/database.php
'connections' => array(
    'mysql' => array(
        'driver'    => 'mysql',
        'host'      => $_SERVER['MYSQL_PORT_3306_TCP_ADDR'],
        'database'  => $_SERVER['MYSQL_ENV_MYSQL_DATABASE'],
        'username'  => $_SERVER['MYSQL_ENV_MYSQL_USER'],
        'password'  => $_SERVER['MYSQL_ENV_MYSQL_PASSWORD'],
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    )
)

存在相关的数据库环境变量(以 MYSQL _ 开头的变量),并且在运行migration命令时:

The relevant database environment variables exist (the ones beginning with MYSQL_), and when running the migrate command:

php artisan migrate --env=development

抛出以下异常:

{
    "error":{
        "type":"ErrorException",
        "message":"Undefined index: RDS_HOSTNAME",
        "file":"/var/www/app/config/database.php",
        "line":50
    }
}

当我甚至不想使用生产配置时,为什么Laravel关心生产配置中的环境变量是否不存在?我该如何解决?

Why does Laravel care if the environment variable in my production config doesn't exist when I don't even want to use the production configuration? How do I get around this?

推荐答案

在您的 .env 文件中具有以下内容

This problem can happen when in your .env file you have the following

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=test_db
DB_USERNAME=root
DB_PASSWORD=

相反,您应该拥有

RDS_CONNECTION=mysql
RDS_HOSTNAME=localhost
RDS_PORT=3306
RDS_DB_NAME=test_db
RDS_USERNAME=root
RDS_PASSWORD=

除此之外,如果问题与bash和AWS有关,则这可能很有趣.

In addition to that, this might be of interest in case that problem is related with bash and AWS.

这篇关于Artisan迁移在开发环境中失败,因为生产配置中缺少环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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