问题描述
我将正在处理的应用程序移到了新的开发服务器上,即Mac OSX主机上的Laravel Homestead VagrantBox。这样做之后,我运行了php artisan:migrate来更新我的数据库,并且一切顺利。
I moved an application that I'm working on into a new dev server, a Laravel Homestead VagrantBox on a Mac OSX host. Upon doing so, I ran php artisan:migrate to update my database and this went through without a hitch.
我决定创建一个新用户以继续测试,因此我创建了路线
I decided to create a new user to continue testing, so I created the route
Route::get('/newuser', function()
{
User::create([
'username' => 'someone',
'email' => '[email protected]',
'password' => Hash::make('password')
]);
return 'Done.';
});
但是,当我访问/ newuser时。我收到以下消息。
When I visit /newuser, however. I am getting the following message.
SQLSTATE[HY000] [2002] Connection refused
现在,我知道我的数据库配置必须正确,因为当我运行php artisan migration并成功迁移数据库时没有收到任何错误。但是,为了安全起见,我检查了数据库配置。
Now, I know that my database config must be correct, as I received no errors when I ran php artisan migrate and my database migrated successfully. However, just to be safe, I checked my database config.
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost:33060',
'database' => 'site',
'username' => 'root',
'password' => 'apassword',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
我发现也许定义端口的方式存在问题,所以我添加了参数:
I figured that maybe there was an issue with the way I defined the port I was on, so I added the parameter:
'port' => '33060'
执行此操作后,错误消息从拒绝连接更改为没有此类文件或目录
Upon doing this, the error message changed from "Connection refused" to "No such file or directory"
我很茫然。
推荐答案
当您坚持使用某些东西时,这比金钱赌注还要好您的假设之一就是问题所在。您的Laravel应用程序可能在命令行运行期间正在读取不同的凭据,或者迁移无关,或者由于某些奇怪的PHP原因而导致错误在迁移运行期间得到了抑制。在出现错误的情况下,我会检查Laravel使用的凭据。将以下代码添加到您的 newuser
路由中,以查看Laravel的读物。
It's a better than even money bet when you're stuck on something that it's one of your assumptions that's the problem. It's possible your Laravel application is reading different credentials during a command line run, or that the migration had nothing to do, or for some weird PHP reason the errors were suppressed during your migration run. I'd check the credentials Laravel's using during the context your errors are cropping up. Add the following code to your newuser
route to see what Laravel's reading.
$default = Config::get('database.default');
var_dump($default);
$config = Config::get('database.connections.'.$default);
var_dump($config);
这篇关于Laravel Homestead SQLSTATE [HY000] [2002]服务器移动后连接被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!