PDO连接被laravel拒绝,但没有使用$ con = new PDO()

I've configured mysql for my laravel 5.0-dev project like so:

'mysql' => [
    'driver'    => 'mysql',
    'host'      => env('DB_HOST') ?: 'localhost',
    'database'  => env('DB_DATABASE') ?: 'homestead',
    'username'  => env('DB_USERNAME') ?: 'homestead',
    'password'  => env('DB_PASSWORD') ?: 'secret',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
    'strict'    => false,
],

However, I get connection refused ('PDOException' with message 'SQLSTATE[HY000] [2002]') whenever I try to interact with the database using either php artisan, or the laravel app itself.

The weird thing is, when I run the code below, the connection doesn't get refused.

<?php
$dsn = 'mysql:dbname=homestead;host=localhost';
$user = 'homestead';
$password = 'secret';

try
{
    $dbh = new PDO($dsn, $user, $password);
}
catch (PDOException $e)
{
    echo 'Connection failed: ' . $e->getMessage();
}

I've been googling for 3 hours now and I haven't found a single solution which actually works. So any help is greatly appreciated.

The problem is fixed. First off, if you're using a VM, like I am, you can't set your host to localhost, it has to be set to 127.0.0.1. Secondly, the port through which the connection is made needs to be explicitly defined (default for mysql is 33060). So in my case the connection configuration is defined as follows:

    'mysql' => array(
        'driver'    => 'mysql',
        'host'      => '127.0.0.1',
        'port'      => '33060',
        'database'  => 'homestead',
        'username'  => 'homestead',
        'password'  => 'secret',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ),

Lastly, you need to modify the .env file in your project:

DB_HOST=127.0.0.1
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret