简体   繁体   中英

Laravel Homestead SQLSTATE[HY000] [2002] Connection refused after server move

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' => 'someone@someone.com',
        'password' => Hash::make('password') 
]);

return 'Done.';
});

When I visit /newuser, however. I am getting the following message.

SQLSTATE[HY000] [2002] Connection refused

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"

I'm at a loss. Does anyone have any pointers?

Now, I know that my database config must be correct, as I received no errors when I ran php artisan migrate

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);

I had same problem and i saw my database server was unresponsive i restarted and it works... sometime it cause when wrong configuration.

I posted this answer if someone will have same issue and mysql unresponsive :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM