Laravel 5.1 - Homestead MySQL connection. `Connect

2019-06-07 06:21发布

When my .env file is like this:

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

Terminal php artisan migrate works successful but on localhost test, it throws:

PDOException in Connector.php line 50:

SQLSTATE[HY000] [2002] Connection refused

But when my .env file is like this:

 DB_HOST=localhost
 DB_DATABASE=homestead
 DB_USERNAME=homestead
 DB_PASSWORD=secret

On localhost everything (registration) works well however php artisan migrate on terminal throws:

[PDOException] SQLSTATE[HY000] [2002] No such file or directory

My database.php file:

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

2条回答
一纸荒年 Trace。
2楼-- · 2019-06-07 06:52

Inside the VM the sql port is 3306. Outside of the homestead VM the host machine just has a forward to the SQL port on the homestead VM. That is why 33060 points to 3306.

Laravel Homestead Vagrant Box Database Problems

查看更多
小情绪 Triste *
3楼-- · 2019-06-07 06:53

To fix the issue. Do the following:

.env

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

database.php

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

That will get your browsing fixed. Now to run artisan commands, just do vagrant SSH and run them on the VM.

查看更多
登录 后发表回答