Laravel Homestead无法连接到db

时间:2014-06-07 08:56:41

标签: php mysql database laravel laravel-4

使用Laravel Homestead时,我无法连接到数据库。我真的不明白为什么它不起作用我已经尝试过该死的一切我猜。现在我陷入困境,无法尝试更多,我开始与更多人互动。 stackoverflow有什么好的答案或建议给我? :)

这是我的设置

'mysql' => array(
    'driver'    => 'mysql',
    'host'      => $_ENV['DB_HOST'],
    'database'  => $_ENV['DB_NAME'],
    'username'  => $_ENV['DB_USERNAME'],
    'password'  => $_ENV['DB_PASSWORD'],
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
    'port'      => '33060'
)

我也尝试过同样的回应。

'mysql' => array(
    'driver'    => 'mysql',
    'host'      => $_ENV['DB_HOST'],
    'database'  => $_ENV['DB_NAME'],
    'username'  => $_ENV['DB_USERNAME'],
    'password'  => $_ENV['DB_PASSWORD'],
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
    'port'      => '3306'
)

这是我的ENV变量。

{
host: "127.0.0.1",
database: "homestead",
username: "homestead",
password: "secret"
}

回报是:

PDOException
SQLSTATE[HY000] [2003] Can't connect to MySQL server on '127.0.0.1' (111)

在两个迁移中作为网站。

它可以与Sequel Pro连接,它在Sequel Pro中看起来像这样。 enter image description here

编辑:我的VagrantBox的IP和MAMP之间是否有可能发生某种冲突?我已经停止了我正在运行的所有MAMP但仍然存在同样的问题,只是想知道我是否需要在其他地方停止它?

2 个答案:

答案 0 :(得分:7)

您根本不需要指定端口,我会删除该行。对于主持人,请尝试使用localhost。还要确保Homestead使用您设置数据库的环境,很可能是local

MAMP不能与它发生冲突,除非您在某个时候手动将MAMP MySQL端口设置为33060,我非常怀疑。

这是一个适合我的默认文件:

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

Lycka直到!

答案 1 :(得分:0)

通过在项目的根目录中键入php artisan tinker,然后print_r(Config::get('database.connections')['mysql']);打开PHP REPL,您有什么?

你应该有这个:

Array
(
    [driver] => mysql
    [host] => localhost
    [database] => homestead
    [username] => homestead
    [password] => secret
    [charset] => utf8
    [collation] => utf8_unicode_ci
    [prefix] => 
    [port] => 33060
)

如果您有不同的环境变量,或者您没有检测到本地环境。