今天开始使用laradock,但是项目一运行,就显示数据库连接不上。
Illuminate\Database\QueryException : SQLSTATE[HY000] [2002] Connection refused (SQL:
这不应该啊。尝试手动连接数据库,没有问题。那么就看看连接信息把。
array:15 [ "driver" => "mysql" "host" => "127.0.0.1" "port" => "3306" "database" => "ss" "username" => "homestead" "password" => "secret" "unix_socket" => "" "charset" => "utf8mb4" "collation" => "utf8mb4_unicode_ci" "prefix" => "table_" "prefix_indexes" => true "strict" => true "engine" => null "options" => [] "name" => "mysql" ]
果然,连接信息还是旧的呢。那么为什么会是如此呢?明明env文件已经更新了。读取出来内容已经变更了。
env文件读取是在kernel.php完成的
if (! $this->app->hasBeenBootstrapped()) { $this->app->bootstrapWith($this->bootstrappers()); }
依次要执行如下初始化操作
protected $bootstrappers = [ \Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables::class, \Illuminate\Foundation\Bootstrap\LoadConfiguration::class, \Illuminate\Foundation\Bootstrap\HandleExceptions::class, \Illuminate\Foundation\Bootstrap\RegisterFacades::class, \Illuminate\Foundation\Bootstrap\SetRequestForConsole::class, \Illuminate\Foundation\Bootstrap\RegisterProviders::class, \Illuminate\Foundation\Bootstrap\BootProviders::class, ];
而关键在于 LoadEnvironmentVariables这个class
我们可以看到,再bootstrap的时候他首先有一个判断
if ($app->configurationIsCached()) { return; }
那么,问题的答案就很明显了。。。
php artisan config:clear
Configuration cache cleared!
收工