My Laravel 5在配置数据库之前运行正常,然后发现此错误:
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known
在做一些研究时,我似乎配置MySQL访问太晚了,所以我应该重新启动服务器以获得正确的环境变量。嗯,我正在使用Dreamhost的共享服务器,但我就是做不到。
我应该如何解决此问题?
感谢
如果你在服务器上运行了php artisan config:cache
,那么你的Laravel应用程序可能会缓存你放在.env
文件中的过时配置设置。
运行php artisan config:clear
来修复此问题。
我知道这是旧的,但对于本地开发人员来说,这就是将事情恢复到production.env文件的原因:
rm bootstrap/cache/config.php
然后
php artisan config:cache
php artisan config:clear
php artisan cache:clear
您的配置变量可能已缓存。验证您的config/app.php
和.env
文件,然后尝试
php artisan cache:clear
在命令行中。
需要明确的是,根据您的情况,可以清除4种类型的缓存。
php artisan cache:clear
当您希望清除应用程序缓存时,可以在控制台中运行上述语句。它的作用是清除storage''framework''cache中的所有缓存。
php artisan route:cache
这将清除您的路由缓存。因此,如果您添加了新路线或更改了路线控制器或操作,则可以使用此控制器或操作重新加载相同的路线。
php artisan config:cache
这将清除env文件的缓存并重新加载
php artisan view:clear
这将清除应用程序的已编译视图文件。
对于共享主机
大多数共享主机提供商不提供对系统的SSH访问。在这种情况下,您需要创建一条路线,并拨打以下线路:
Route::get('/clear-cache', function() {
Artisan::call('cache:clear');
return "All cache cleared";
});
一个简短的解决方案:
use Dotenv;
with(new Dotenv(app()->environmentPath(), app()->environmentFile()))->overload();
with(new LoadConfiguration())->bootstrap(app());
在我的情况下,我需要在以编程方式更改.env后重新建立数据库连接,但它不起作用,如果你遇到这个问题,请尝试这个
app('db')->purge($connection->getName());
在重新加载.env之后,这是因为Laravel App以前可能访问过默认连接,'Illuminate'Database'DatabaseManager
需要重新读取配置参数。
如果有人遇到这个问题,无法重新加载他们的Web服务器(像队列运行器一样长时间运行控制台命令(或需要在请求中途重新加载.env文件,我在laravel 5中找到了一种正确重新加载.ev变量的方法。
use Dotenv;
use InvalidArgumentException;
try {
Dotenv::makeMutable();
Dotenv::load(app()->environmentPath(), app()->environmentFile());
Dotenv::makeImmutable();
} catch (InvalidArgumentException $e) {
//
}
这是我能找到的唯一一个与在现有应用程序实例上重新加载.env
和config($key)
值有关的问题,但其中许多答案可能存在于另一个问题上。
我在Laravel6.x应用程序中获得了以下内容,同时尝试让一个手工命令使用我的柏树环境变量。
对于上下文$this->laravel === app()
。
/**
* Make the application use the cypress environment variables
*
* @return void
*/
protected function enableCypressEnv()
{
// Get all of the original values from config. We need to do this because
// rebuilding config will skip packages.
$old = app(Repository::class)->all();
// Change the applications env file
$this->laravel->loadEnvironmentFrom('.env.cypress');
// Reload the app's environment variables
Dotenv::create(
$this->laravel->environmentPath(),
$this->laravel->environmentFile(),
Env::getFactory()
)->overload();
// Force config to be rebuitl with new env
(new LoadConfiguration())->bootstrap($this->laravel);
// Get all of the new values from buidling with new env
$new = app(Repository::class)->all();
// Merge the new values over the old distinctly
$merged = array_merge_recursive_distinct($old, $new);
// Get the applications configuration instance
$config = config();
// Overwrite all of the values in the container in accordance with the merged values
foreach ($merged as $key => $value) {
$config->set([$key => $value]);
}
}
向上面的答案大喊,因为它们为我指明了正确的方向,但我会在这里包括我的答案,因为它们都不适合我。
在config/database.php
中,我将默认的数据库连接从mysql更改为sqlite。我删除了.env
文件(实际上已将其重命名(,并使用touch storage/database.sqlite
创建了sqlite文件。迁移使用sqlite。
然后,我将config/database.php
的默认DB连接切换回mysql,并恢复了.env
文件。迁移使用mysql。
我想这没有道理。也许是服务器端的东西。