php SQLSTATE[HY000] [2003] 无法在“127.0.0.1”(61)错误 Laravel 4.1 上连接到 MySQL 服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23532323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:47:21  来源:igfitidea点击:

SQLSTATE[HY000] [2003] Can't connect to MySQL server on '127.0.0.1' (61) error Laravel 4.1

phpmysqllaravellaravel-4

提问by user1072337

I am receiving the following error on my localhost for Laravel 4.1 (using MAMP)

我在 Laravel 4.1 的本地主机上收到以下错误(使用 MAMP)

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

It points to:

它指出:

/Applications/MAMP/htdocs/crowdsets/laravel-master/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php

This is the function it is pointing to:

这是它指向的函数:

public function createConnection($dsn, array $config, array $options)
    {
        $username = array_get($config, 'username');

        $password = array_get($config, 'password');

        return new PDO($dsn, $username, $password, $options);
    }

Up to this point, I had not received this error.

到目前为止,我还没有收到此错误。

I have a local environment and production environment set up (the default).

我设置了本地环境和生产环境(默认)。

in config/local/database.php I have:

在 config/local/database.php 我有:

'mysql' => array(
            'driver'    => 'mysql',
            'host'      => '127.0.0.1',
            'database'  => 'database',
            'username'  => 'root',
            'password'  => 'root',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),

回答by JakeGould

An error like that means the server itself is not even reachable. Did you start MySQL in MAMP?

这样的错误意味着服务器本身甚至无法访问。你是在 MAMP 中启动 MySQL 的吗?

Also, how have you started MAMP? With the standard MySQL 3306port? Or the alternative port MAMP uses for non-admins: 8889?

另外,你是如何开始MAMP的?使用标准的 MySQL3306端口?或备用端口甲基苯丙胺用途非管理员:8889

I bet your server is running, but is attempting to connect to 3306so you need to set the port to 8889. Change your config to be like this; note the addition of the portparam:

我敢打赌您的服务器正在运行,但正在尝试连接,3306因此您需要将端口设置为8889. 把你的配置改成这样;注意port参数的添加:

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

EDIT:I just found this question threadthat addresses the issue of connecting Laravel to MAMP via port 8889.

编辑:我刚刚发现这个问题线程解决了通过 port 将 Laravel 连接到 MAMP 的问题8889