Laravel SQLSTATE[HY000] [2002] 连接被拒绝但修补工作

Laravel SQLSTATE[HY000] [2002] Connection refused but tinker works

我有一个使用 Amazon 服务器数据库的生产站点,我在同一台服务器上还有两个站点,它们都可以正常连接。

在这个特定站点上,与 mysql 的连接不起作用,我认为 env 文件丢失或未被 laravel 应用程序看到,但事实并非如此,当我 运行 php artisan tinker 并尝试任何有效的命令:

DB::table('categories')->get();
=> Illuminate\Support\Collection {#842
     all: [
       {#828
         +"id": 1,
         +"parent_id": null,
         +"order": 1,
         +"name": "Noticias",
         +"slug": "noticias",
         +"created_at": "2018-01-09 19:26:32",
         +"updated_at": "2018-01-09 19:26:32",
       },
     ],
   }

App::environment() 方法也有效:

>>> App::environment();
=> "production"

env 文件清楚地写着 APP_DEBUG=false 但它返回了很多调试,就像网络服务器除了 artisan 之外没有读取 env 文件。

这让我抓狂,因为它开始自己失败,服务器或代码上没有任何更改,这是唯一失败的项目。

我还能检查什么?

[2018-08-25 12:22:04] production.ERROR: SQLSTATE[HY000] [2002] Connection refused {"exception":"[object] (PDOException(code: 2002): SQLSTATE[HY000] [2002] Connection refused at /var/app/html/releases/20180824022105/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:67)
[stacktrace]
#0 /var/app/html/releases/20180824022105/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php(67): PDO->__construct('mysql:host=127....', 'root', 'C4f2469dcF!', Array)
#1 /var/app/html/releases/20180824022105/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php(43): Illuminate\Database\Connectors\Connector->createPdoConnection('mysql:host=127....', 'root', 'C4f2469dcF!', Array)
#2 /var/app/html/releases/20180824022105/vendor/laravel/framework/src/Illuminate/Database/Connectors/MySqlConnector.php(24): Illuminate\Database\Connectors\Connector->createConnection('mysql:host=127....', Array, Array)
#3 /var/app/html/releases/20180824022105/vendor/laravel/framework/src/Illuminate/Database/Connectors/ConnectionFactory.php(183): Illuminate\Database\Connectors\MySqlConnector->connect(Array)
#4 [internal function]: Illuminate\Database\Connectors\ConnectionFactory->Illuminate\Database\Connectors\{closure}()
#5 /var/app/html/releases/20180824022105/vendor/laravel/framework/src/Illuminate/Database/Connection.php(915): call_user_func(Object(Closure))
#6 /var/app/html/releases/20180824022105/vendor/laravel/framework/src/Illuminate/Database/Connection.php(940): Illuminate\Database\Connection->getPdo()
#7 /var/app/html/releases/20180824022105/vendor/laravel/framework/src/Illuminate/Database/Connection.php(399): Illuminate\Database\Connection->getReadPdo()
#8 /var/app/html/releases/20180824022105/vendor/laravel/framework/src/Illuminate/Database/Connection.php(325): Illuminate\Database\Connection->getPdoForSelect(true)
#9 /var/app/html/releases/20180824022105/vendor/laravel/framework/src/Illuminate/Database/Connection.php(657): Illuminate\Database\Connection->Illuminate\Database\{closure}('select * from `...', Array)
#10 /var/app/html/releases/20180824022105/vendor/laravel/framework/src/Illuminate/Database/Connection.php(624): Illuminate\Database\Connection->runQueryCallback('select * from `...', Array, Object(Closure))
#11 /var/app/html/releases/20180824022105/vendor/laravel/framework/src/Illuminate/Database/Connection.php(333): Illuminate\Database\Connection->run('select * from
`...', Array, Object(Closure))
#12 /var/app/html/releases/20180824022105/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(1719): Illuminate\Database\Connection->select('select
* from `...', Array, true)
#13 /var/app/html/releases/20180824022105/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(1704): Illuminate\Database\Query\Builder->runSelect()
#14 /var/app/html/releases/20180824022105/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(483): Illuminate\Database\Query\Builder->get(Array)
#15 /var/app/html/releases/20180824022105/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(467): Illuminate\Database\Eloquent\Builder->getModels(Array)
#16 /var/app/html/releases/20180824022105/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(360): Illuminate\Database\Eloquent\Builder->get(Array)
#17 /var/app/html/releases/20180824022105/app/Category.php(13): Illuminate\Database\Eloquent\Model::all()
#18 /var/app/html/releases/20180824022105/vendor/laravel/framework/src/Illuminate/Cache/Repository.php(327): App\Category::App\{closure}()
#19 /var/app/html/releases/20180824022105/vendor/laravel/framework/src/Illuminate/Cache/CacheManager.php(304): Illuminate\Cache\Repository->remember('list-categories', NULL, Object(Closure))
#20 /var/app/html/releases/20180824022105/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php(221): Illuminate\Cache\CacheManager->__call('remember', Array)
#21 /var/app/html/releases/20180824022105/app/Category.php(20): Illuminate\Support\Facades\Facade::__callStatic('remember', Array)
#22 /var/app/html/releases/20180824022105/routes/web.php(150): App\Category::getCategories()
#23 /var/app/html/releases/20180824022105/vendor/laravel/framework/src/Illuminate/Routing/Router.php(389): Illuminate\Routing\Router->{closure}(Object(Illuminate\Routing\Router))
#24 /var/app/html/releases/20180824022105/vendor/laravel/framework/src/Illuminate/Routing/Router.php(349): Illuminate\Routing\Router->loadRoutes(Object(Closure))
#25 /var/app/html/releases/20180824022105/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php(221): Illuminate\Routing\Router->group(Array, Object(Closure))
#26 /var/app/html/releases/20180824022105/routes/web.php(162): Illuminate\Support\Facades\Facade::__callStatic('group', Array)
#27 /var/app/html/releases/20180824022105/vendor/laravel/framework/src/Illuminate/Routing/Router.php(393): require('/var/www/turico...')
#28 /var/app/html/releases/20180824022105/vendor/laravel/framework/src/Illuminate/Routing/Router.php(349): Illuminate\Routing\Router->loadRoutes('/var/www/turico...')
#29 /var/app/html/releases/20180824022105/vendor/laravel/framework/src/Illuminate/Routing/RouteRegistrar.php(105): Illuminate\Routing\Router->group(Array, '/var/www/turico...')
#30 /var/app/html/releases/20180824022105/app/Providers/RouteServiceProvider.php(56): Illuminate\Routing\RouteRegistrar->group('/var/www/turico...')
#31 /var/app/html/releases/20180824022105/app/Providers/RouteServiceProvider.php(40): App\Providers\RouteServiceProvider->mapWebRoutes()
#32 [internal function]: App\Providers\RouteServiceProvider->map()
#33 /var/app/html/releases/20180824022105/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(29): call_user_func_array(Array, Array)
#34 /var/app/html/releases/20180824022105/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(87): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#35 /var/app/html/releases/20180824022105/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(31): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))
#36 /var/app/html/releases/20180824022105/vendor/laravel/framework/src/Illuminate/Container/Container.php(549): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)
#37 /var/app/html/releases/20180824022105/vendor/laravel/framework/src/Illuminate/Foundation/Support/Providers/RouteServiceProvider.php(74): Illuminate\Container\Container->call(Array)
#38 /var/app/html/releases/20180824022105/vendor/laravel/framework/src/Illuminate/Foundation/Support/Providers/RouteServiceProvider.php(33): Illuminate\Foundation\Support\Providers\RouteServiceProvider->loadRoutes()
#39 /var/app/html/releases/20180824022105/app/Providers/RouteServiceProvider.php(28): Illuminate\Foundation\Support\Providers\RouteServiceProvider->boot()
#40 [internal function]: App\Providers\RouteServiceProvider->boot()
#41 /var/app/html/releases/20180824022105/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(29): call_user_func_array(Array, Array)
#42 /var/app/html/releases/20180824022105/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(87): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#43 /var/app/html/releases/20180824022105/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(31): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))
#44 /var/app/html/releases/20180824022105/vendor/laravel/framework/src/Illuminate/Container/Container.php(549): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)
#45 /var/app/html/releases/20180824022105/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(792): Illuminate\Container\Container->call(Array)
#46 /var/app/html/releases/20180824022105/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(775): Illuminate\Foundation\Application->bootProvider(Object(App\Providers\RouteServiceProvider))
#47 [internal function]: Illuminate\Foundation\Application->Illuminate\Foundation\{closure}(Object(App\Providers\RouteServiceProvider), 26)
#48 /var/app/html/releases/20180824022105/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(776): array_walk(Array, Object(Closure))
#49 /var/app/html/releases/20180824022105/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/BootProviders.php(17): Illuminate\Foundation\Application->boot()
#50 /var/app/html/releases/20180824022105/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(213): Illuminate\Foundation\Bootstrap\BootProviders->bootstrap(Object(Illuminate\Foundation\Application))
#51 /var/app/html/releases/20180824022105/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(162): Illuminate\Foundation\Application->bootstrapWith(Array)
#52 /var/app/html/releases/20180824022105/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(146): Illuminate\Foundation\Http\Kernel->bootstrap()
#53 /var/app/html/releases/20180824022105/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(116): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#54 /var/app/html/releases/20180824022105/public/index.php(55): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#55 {main}
"}

您应该验证您是否有 bootstrap/cache/config.php 文件。如果是,则表示您的配置已缓存。比你可以 运行:

php artisan config:cache

刷新您在.env

中设置的配置和同步数据
php artisan config:clear

如果您不想使用缓存配置(请记住,建议在生产环境中缓存配置)

我遇到了类似的问题,我在安装其他所有东西后安装了 php7.2-mysql。 Tinker 运行良好,但我的应用程序无法连接。

我花了 2 个小时尝试调试,直到简单的 apache 重启就成功了。

我的解决方案是重新启动 laravel 开发服务器,即:再次 运行 php artisan serve

我最初 运行 开发服务器后更改了数据库配置。