Laravel环境配置未加载,迁移失败-SQLSTATE [HY000] [2002]没有此类文件或目录 [英] Laravel environment config not loading, migration fails - SQLSTATE[HY000] [2002] No such file or directory
问题描述
我的 app/config/database.php 设置如下:
'mysql' => array(
'driver' => 'mysql',
'host' => getenv('DB_HOST'),
'database' => getenv('DB_NAME'),
'username' => getenv('DB_USER'),
'password' => getenv('DB_PASS'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
我已经在 bootstrap/start.php 中更新了我的环境检测代码:
I have updated my env detection code like this in bootstrap/start.php:
$env = $app->detectEnvironment(function() {
return file_exists(dirname(__FILE__) . '/../.env.production.php')
?
'production'
:
'local';
});
现在,我已上传 .env.production.php 并包含以下内容:
Now, I have uploaded .env.production.php with the following:
<?php
return
[
'DB_HOST' => '178.xxx.xxx.xxx',
'DB_NAME' => 'USERNAME',
'DB_USER' => 'PASSWORD',
'DB_PASS' => 'DBNAME',
];
我像这样测试了环境检测:
I tested the environment detection like this:
[www-data@server]$ php artisan env
Current application environment: production
如您所见,它正在按预期方式工作.然后,我像这样进行迁移:
As you can see, it's working as intended. I then proceeded to run the migration like this:
php artisan迁移:安装
执行此操作时,出现以下错误:
When I do, I get the following error:
[www-data@server]$ php artisan migrate:install --env=production
[PDOException]
SQLSTATE[HY000] [2002] No such file or directory
migrate:install [--database[="..."]]
有人知道为什么会这样吗?似乎无法加载 .env.production.php 文件.
Any idea why this might be? it looks like it's failing to load the .env.production.php file.
有人知道如何解决此错误吗?预先感谢.
Does anyone know how to fix this error? Thanks in advance.
推荐答案
在 Laravel 4文档指出它必须在生产服务器上为.env.php
.
In the Laravel 4 docs it states that it needs to be .env.php
on your production server.
现在,在生产服务器上,在项目中创建一个.env.php文件. 包含您生产的相应值的根 环境.与.env.local.php文件类似,生产.env.php 文件绝对不能包含在源代码管理中.
Now, on your production server, create a .env.php file in your project root that contains the corresponding values for your production environment. Like the .env.local.php file, the production .env.php file should never be included in source control.
因此,只需将您的环境检测代码更改为此
So simply change your environment detection code to this
$env = $app->detectEnvironment(function() {
return file_exists(dirname(__FILE__) . '/../.env.php')
?
'production'
:
'local';
});
注意-您仍在开发服务器上使用.env.local.php
.
Note - you still use .env.local.php
on your development server.
这篇关于Laravel环境配置未加载,迁移失败-SQLSTATE [HY000] [2002]没有此类文件或目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!