Laravel中拒绝使用套接字连接到DB

Chi*_*ion 5 php mysql laravel laravel-5

我知道SO和Google在这个问题上备受关注,但似乎还有一个独特的问题。

错误消息:SQLSTATE [HY000] [2002]连接被拒绝

组态

    'mysql' => array(
        'driver'    => 'mysql',
        "host"      => "localhost:/var/run/mysqld4.sock",
        'port'      => '3304',
        "username"  => "admin",
        "password"  => "admin",
        "database"  => "frontend_generic",
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => 'pre_',
    ),
Run Code Online (Sandbox Code Playgroud)

我确认数据库在端口3304上运行,我的前缀正确,用户,数据库和密码也正确。

对于主机,我尝试了“ localhost”,“ 127.0.0.1”,“ http://127.0.0.1 ”,甚至是服务器的实际IP地址。

仍然没有运气或变化。我尝试在本地database.php文件中使用完全相同的配置,但是正如预期的那样,没有任何变化。

出于选择,我在这里想念什么?

更新:

这是来自与此配置兼容的另一个应用程序的代码。这是Kohana,不是Laravel,但可以。

 "general" => array
        (
            "type"       => "mysql",
            "connection" => array
            (
                "hostname"   => "localhost:/var/run/mysqld4.sock",
                "username"   => "admin",
                "password"   => "admin",
                "persistent" => FALSE,
                "database"   => "frontend_generic",
            ),
            "table_prefix" => "pre_",
            "charset"      => "utf8",
            "caching"      => FALSE,
            "profiling"    => TRUE,
        ),
Run Code Online (Sandbox Code Playgroud)

Thi*_*Six 6

当使用带有laravel 5的套接字时,请使用unix_socketconfig而不是hostand port

虽然更改为IP地址确实可行,但是当数据库服务器位于同一台计算机上时,使用套接字可能会有好处。

更正最初发布的配置:

    'mysql' => array(
        'driver'           => 'mysql',
        "unix_socket"      => "/var/run/mysqld4.sock",
        "username"         => "admin",
        "password"         => "admin",
        "database"         => "frontend_generic",
        'charset'          => 'utf8',
        'collation'        => 'utf8_unicode_ci',
        'prefix'           => 'pre_',
    ),
Run Code Online (Sandbox Code Playgroud)

  • 这个答案应该绝对位于顶部。 (2认同)

小智 -12

您应该将“host”更改为实际的服务器 IP 地址,而不是其他任何内容。所以,摆脱套接字的东西。

  • 这并没有回答有关“使用套接字连接到数据库”的问题。下面的答案确实如此。 (4认同)